Mybatis执行流程如下:
步骤为:
-
读取MyBatis 配置文件mybatis-config.xml,加载数据源、事务等,管理映射文件
-
加载映射文件mapper.xml,用于映射表中列和实体属性的关系
-
定义SQL语句,在上一步的文件中加载。
-
创建会话工厂。(SqlSessionFactory),数据库连接池
-
创建会话(SqlSession),连接对象
-
通过Executor 操作数据库
-
输入参数和输出结果
下面我们用具体代码来了解Mybatis
1.导入mybatis的依赖,mysql驱动,junit
<dependencies>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
2.在src/main/resources目录创建两类配置文件
-
局配置文件: mybatis-config.xml 对应的类:org.apache.ibatis.session.Configuration#Configuration()
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--环境配置:default:默认使用那个数据源-->
<environments default="mysql">
<!--数据源环境-->
<environment id="mysql">
<!--transactionManager:事务管理器
JDBC:手动提交事务
MANAGED:交给第三方框架管理
-->
<transactionManager type="JDBC"/>
<!--数据源:
type:数据源类型
1. POOLED:带连接池的数据库,性能高
2.JNDI:外部数据源
3.UNPOOLED:不带连接池的数据源
-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!--加载mapper映射文件-->
<!-- <mappers>-->
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<!-- </mappers>-->
</configuration>
mapper映射文件:一张表对应一个mapper映射文件
事务: 在同一个事务下,一组sql,要就全部成功,要就全部失败
事务四大特征:ACID
-
原子性
-
隔离性
-
持久性
-
一致性
3.为tb_user表创建对应的实体类User
package com.fs.entity;
import java.util.Date;
public class User {
/**
* 实体类属性的数据类型使用包装类
*/
private Integer id;
private String username;
private String password;
private String sex;
//日期类型:java.util.Date
private Date brithday;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", brithday=" + brithday +
", address='" + address + '\'' +
'}';
}
public User() {
}
public User(Integer id, String username, String password, String sex, Date brithday, String address) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.brithday = brithday;
this.address = address;
}
public User(String username, String password, String sex, Date brithday, String address) {
this.username = username;
this.password = password;
this.sex = sex;
this.brithday = brithday;
this.address = address;
}
}
4.对应表mapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
namespace:命名空间,现在随便写
后期专门写法
-->
<mapper namespace="xxx">
<!--sql语句:每一个sql称为一个statement-->
<!--查询statement:select
属性:
id:唯一标识符
parameterType:输入参数类型,如果输入参数简单数据类型,可以省略
自定义类类型,一定不能省略:全限定名(包.类)
resultType: 结果集类型:查询一条记录转换那个对象类型:
自定义类类型,全限定名(包.类)
sql语句占位符: #{名字},${名字}
如果parameterType是简单数据类型,名字随便写
如果parameterType是自定义类类型, 名字写的是类对应的属性名
-->
<select id="queryById" parameterType="int" resultType="com.fs.entity.User">
select * from tb_user where id=#{uid}
</select>
</mapper>
package com.fs;
import com.fs.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
/**
* 测试类
*/
public class UserDaoTest {
@Test
public void testQueryById() throws IOException {
//使用mybatis操作数据库
/*
1.加载mybatis全局配置文件
2.创建SqlSessionFactory对象(构建者设计模式:SqlSessionFactoryBuilder)
3.根据SqlSessionFactory对象创建sqlSession对象
4.调用sqlSession对象的方法,执行sql语句
5.关闭sqlSession
*/
//1.加载mybatis全局配置文件
InputStream in = Resources.getResourceAsStream(" mybatis-config.xml");
//2.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
//3.创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = builder.build(in);
//4.创建sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5.执行sql语句 statementID:namespace.statementId
User user=sqlSession.selectOne("xxx.queryById",1);
System.out.println(user);
//6.关闭sqlSession
sqlSession.close();
}
}
可能会出现问题:
xxx.queryById找不到:
原因:
1.mapper映射文件没有加载
2.statement字符串写错,一定复制粘贴