作者:姥姥的妹妹的儿子跟我没关系 | 来源:互联网 | 2023-06-06 11:40
1.Mybatis简介MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结
1.Mybatis简介
- MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。
- MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
- MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
2.持久层
数据持久化:将程序的数据在持久状态和瞬间状态转化的过程。
持久层:完成持久化工作的代码块。
3.创建一个Mybatis项目
3.1 mysql数据库建表
3.2 搭建环境
idea导入maven依赖(pom.xml)
<dependencies><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId><version>5.1.47version>dependency><dependency><groupId>org.mybatisgroupId><artifactId>mybatisartifactId><version>3.5.2version>dependency><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>4.12version>dependency>dependencies>
3.3 xml配置
连接mysq数据库(mybatis-config.xml)
<configuration><environments default&#61;"development"><environment id&#61;"development"><transactionManager type&#61;"JDBC"/><dataSource type&#61;"POOLED"><property name&#61;"driver" value&#61;"com.mysql.jdbc.Driver"/><property name&#61;"url" value&#61;"jdbc:mysql://localhost:3306/mybatis?useSSL&#61;false&useUnicode&#61;true&characterEncoding&#61;UTF-8"/><property name&#61;"username" value&#61;"root"/><property name&#61;"password" value&#61;"123456"/>dataSource>environment>environments>
3.4 编写Mybatis工具类
public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static {try {String resource &#61; "mybatis-config.xml";InputStream inputStream&#61; Resources.getResourceAsStream(resource);sqlSessionFactory &#61; new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}
}
public class User {private int id;private String name;private String pwd;
}
3.5 Dao层
- 接口&#xff08;UserDao&#xff09;
public interface UserDao {List<User> getUserList();
}
- 接口实现&#xff08;UserMapper.xml&#xff09;
<mapper namespace&#61;"com.ming.dao.UserDao"><select id&#61;"getUserList" resultType&#61;"com.ming.pojo.User">select * from mybatis.userselect>
mapper>
3.6 测试
&#64;Testpublic void test(){SqlSession sqlSession&#61;MybatisUtils.getSqlSession();UserDao userDao&#61;sqlSession.getMapper(UserDao.class);List<User> userList&#61;userDao.getUserList();for(User user:userList){System.out.println(user);}sqlSession.close();}
3.7 常见错误
原因&#xff1a;MybatisUtils类中SqlsessionFactory 已在开头声明过&#xff0c;
try{}中就不用再声明了。
解决&#xff1a;将
SqlSessionFactory sqlSessionFactory &#61; new SqlSessionFactoryBuilder().build(inputStream);
改为&#xff1a;
sqlSessionFactory &#61; new SqlSessionFactoryBuilder().build(inputStream);
原因&#xff1a;mapper没有注册
解决&#xff1a;在mybatis核心配置文件(mybatis-config.xml)中注册
原因&#xff1a;UserMapper.xml没有配置成功&#xff08;默认配置resource路径下的xml&#xff09;
解决&#xff1a;在项目的pom.xml中将UserMapper.xml配置到resource路径下
<build><resources><resource><directory>src/main/javadirectory><includes><include>**/*.xmlinclude><include>**/*.propertiesinclude>includes><filtering>falsefiltering>resource><resource><directory>src/main/resourcesdirectory><includes><include>**/*.xmlinclude><include>**/*.propertiesinclude>includes><filtering>falsefiltering>resource>resources>build>