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>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3.3 xml配置
连接mysq数据库(mybatis-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
3.4 编写Mybatis工具类
- 拿到Sqlsession对象(utils)
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//通过SqlsessionFactory来获取Sqlsession,Sqlsession完全包含了面向数据库执行SQL命令的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
- 数据实体类(pojo)
public class User {
private int id;
private String name;
private String pwd;
//getter and setter...
//toString()...
}
3.5 Dao层
- 接口(UserDao)
public interface UserDao {
List<User> getUserList();
}
- 接口实现(UserMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:绑定一个对应的Mapper(Dao)接口-->
<mapper namespace="com.ming.dao.UserDao">
<select id="getUserList" resultType="com.ming.pojo.User">
select * from mybatis.user
</select>
</mapper>
3.6 测试
- 测试代码
@Test
public void test(){
//第一步:获得SqLsession对象
SqlSession sqlSession=MybatisUtils.getSqlSession();
//执行sql:getMapper
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> userList=userDao.getUserList();
for(User user:userList){
System.out.println(user);
}
sqlSession.close();
}
- 运行结果
3.7 常见错误
- 空指针异常
原因:MybatisUtils类中SqlsessionFactory 已在开头声明过,
try{}中就不用再声明了。
解决:将
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
改为:
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- 绑定异常
原因:mapper没有注册
解决:在mybatis核心配置文件(mybatis-config.xml)中注册
<mappers>
<mapper resource="com/ming/dao/UserMapper.xml"/>
</mappers>
- 初始化异常
原因:UserMapper.xml没有配置成功(默认配置resource路径下的xml)
解决:在项目的pom.xml中将UserMapper.xml配置到resource路径下
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>