1、Mybatis 总结
1、JDBC
public class JDBCTest {
public static void main(String[] args) throws Exception {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
String url = "jdbc:mysql://127.0.0.1:3306/ssmdemo";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
// 获取 statement, preparedStatement
String sql = "select * from tb_user where id = ?";
prepareStatement = connection.prepareStatement(sql);
// 设置参数
prepareStatement.setLong(1, 1L);
// 执行查询
rs = prepareStatement.executeQuery();
// 处理结果集
while (rs.next()) {
System.out.println(rs.getString("user_name"));
System.out.println(rs.getString("name"));
System.out.println(rs.getInt("age"));
System.out.println(rs.getDate("birthday"));
}
} finally {
// 关闭连接, 释放资源
if (rs != null) rs.close();
if (prepareStatement != null) prepareStatement.close();
if (connection != null) connection.close();
}
try {
connection.setAutoCommit(false); // 开启事务
// 业务执行 ...
connection.commit(); // 提交事务
} catch (Exception e) {
connection.rollback(); // 回滚事务
} finally {
connection.close(); // 关闭事务
}
}
}
2、结构
3、API
* Resources
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
将配置文件读取成一个输入流
* SqlSessionFactoryBuilder
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
用于创建 SqlSessionFactory
* SqlSessionFactory
生命周期: 项目创建,它就创建,项目停止,它就销毁
SqlSession sqlSession = sqlSessionFactory.openSession(true);
用于获取 SqlSession,参数 true 设置事务自动提交
* SqlSession
生命周期: 用时就创建,用完就销毁
UserDao userDao = sqlSession.getMapper(UserDao.class);
获取代理对象
4、分页
PageHelper 是一款优秀的开源 mybatis 分页插件,它支持常用的数据库,例如 mysql、oracle、mariaDB、DB2、SQLite、Hsqldb 等
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.3</version>
</dependency>
<!--applicationContext.xml 中的配置-->
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--mybatis-config.xml 中的配置-->
<!-- 分页助手的插件 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言 -->
<property name="dialect" value="mysql"/>
</plugin>
// service 层代码
public PageInfo<Student> findByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Student> studentList = studentMapper.findAll();
return new PageInfo<>(studentList);
}
@Test
public void testPageHelper(){
// 设置分页参数
PageHelper.startPage(1, 2);
List<User> select = userMapper.select(null);
for(User user : select){
System.out.println(user);
}
// 其它分页的数据
PageInfo<User> pageInfo = new PageInfo<User>(select);
System.out.println("总条数: " + pageInfo.getTotal());
System.out.println("总页数: " + pageInfo.getPages());
System.out.println("当前页: " + pageInfo.getPageNum());
System.out.println("每页显示长度: " + pageInfo.getPageSize());
System.out.println("是否第一页: " + pageInfo.isIsFirstPage());
System.out.println("是否最后一页: " + pageInfo.isIsLastPage());
System.out.println("查询结果: " + pageInfo.getList());
}
5、问题
5.1、${} 和 #{}
${} 和 #{} 都代表参数占位
- &{} Statement 对象,参数字符串拼接,不能防止 sql 注入
- #{} PreparedStatement 对象,参数用 ? 作为占位符处理
5.2、缓存
一级缓存
- 默认开启
- 不要求实现序列化接口
- 针对同一个 sqlSession
二级缓存
- 默认不开启
- 必须实现序列化接口
- 针对同一个 namespace
- sqlSession 提交后才会放入二级缓存
- insert、update、delete 在操作时会清空整个 namespace 的缓存
1. mybatis 的缓存都不需要我们手动存储和获取数据,mybatis 自动维护的
2. mybatis 开启了二级缓存后,那么查询顺序: 二级缓存 -> 一级缓存 -> 数据库
2. 注意: mybatis 的二级缓存会存在脏读问题,需要使用第三方的缓存技术解决问题
本文来自博客园,作者:lidongdongdong~,转载请注明原文链接:https://www.cnblogs.com/lidong422339/p/17544848.html