1. Myabtis简单介绍
- Mybatis是一个半自动化的持久化层的框架
- Mybatis是支持定制化sql,存储过程和高级映射的优秀的持久层框架
- Mybatis避免了几乎所有的jdbc代码和手动设置参数以及获取结果集
- Mybatis可以使用简单的xml或者注解用于配置和原始映射,将接口和java的pojo映射成数据库中的记录
2. 几个网址
- mybatis官网:The MyBatis Blog
- mybatis官网教程:https://mybatis.org/mybatis-3/index.html
3. 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" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 将sql映射文件注册到全局配置文件-->
<mappers>
<mapper resource="mapper.xml" />
</mappers>
</configuration>
4. mapper.xml:SQL映射文件
<?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:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);方法名与标签id同名,绑定接口mapper中的接口方法。
-->
<mapper namespace="com.my.mybatis.dao.EmployeeMapper">
<select id="getEmpById" resultType="com.my.mybatis.bean.Employee">
select id,last_name lastName,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
5. Sqlsession的实例
- 不是线程安全的,因此不能被共享
- 每次使用完毕后必须正确的手动关闭
6. SqlSession两种操作数据库的方法
- sqlSession实例直接执行已经映射的sql语句
@Test
public void test() throws IOException {
// 1、获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// 2、获取sqlSession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
Employee employee = openSession.selectOne(
"com.my.mybatis.EmployeeMapper.selectEmp", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}
- sqlSession实例获取SQL映射器操作
@Test
public void test01() throws IOException {
// 1、获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// 2、获取sqlSession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3、获取接口的实现类对象
//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(mapper.getClass());
System.out.println(employee);
} finally {
openSession.close();
}
}
7. openSession() 方法
- SqlSession openSession()
- SqlSession openSession(boolean autoCommit)
- SqlSession openSession(Connection connection)
- SqlSession openSession(TransactionIsolationLevel level)
- SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level)
- SqlSession openSession(ExecutorType execType)
- SqlSession openSession(ExecutorType execType, boolean autoCommit)
- SqlSession openSession(ExecutorType execType, Connection connection)
8. 默认的 openSession() 方法没有参数
- 事务作用域将会开启(也就是不自动提交)。
- 将由当前环境配置的 DataSource 实例中获取 Connection 对象。
- 事务隔离级别将会使用驱动或数据源的默认设置。
- 预处理语句不会被复用,也不会批量处理更新。
9. ExecutorType:枚举类型
- ExecutorType.SIMPLE:该类型的执行器没有特别的行为。它为每个语句的执行创建一个新的预处理语句。
- ExecutorType.REUSE:该类型的执行器会复用预处理语句。
- ExecutorType.BATCH:该类型的执行器会批量执行所有更新语句,如果 SELECT 在多个更新中间执行,将在必要时将多条更新语句分隔开来