Mybatis框架很普及,我想除了它能 解决实际问题,应该还有一个特点:简单易用!
如果你看了第1天的调用代码,是不是会感觉有点繁琐,可能会问:每次都要写这么长的代码吗?
答案肯定不是的! 尤其是经过SpringBoot集成后,可以直接注入接口,非常方便!
那么,原生Mybatis,有没有简单的方法,立竿见影,就能让使用更简洁呢?
答案肯定是有的! 接下来,请随我一边熟悉Mybaits,一边来编写一个Mybatis工具类吧!
学一个框架最难的是从0到1,剩下的从1到10也只是时间问题!继续加油第2天!
稳稳的来,我们先读懂代码,再尝试封装,那么请认真观察 第1天 编写的调用代码:
// 1. 得到SqlSessionFactory
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 2. 得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
// 3. 得到具体Mapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 4. 执行业务代码:各种的增删改查
User user = mapper.selectById(1);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
Mybatis提供的类有3个,都是以SqlSession开头,我以上帝视角透露给大家,大致的流程如下:
注意认真听哦,大白话说一下哈:通过builder 去读取 全局配置类mybatis-config.xml,xml里包括数据库连接啥的、各种配置啊,然后构建出一个全局唯一的sqlSessionFactory,工厂模式嘛,然后这个工厂就可以无限生产每个会话SqlSession喽~
所以,不妨透露一下官方推荐的最佳作用域:
一旦创建了 SqlSessionFactory,就不再需要它了。
SqlSessionFactory 一旦被创建,就应该在应用的运行期间一直存在。
每个线程都应该有它自己的 SqlSession 实例。关键是关闭操作很重要,官方推荐 SqlSession 关闭的标准模式:
try (SqlSession session = sqlSessionFactory.openSession()) {
// 你的应用逻辑代码
}
可以看出来,第一次调用,这些代码都是必须的!
但从第二次开始,SqlSessionFactory就已经一直存在了,所以 使用的地方 开头 open SqlSession,结尾close SqlSession!
经过上面的分析,SqlSessionFactory肯定是一个全局变量,只创建一次,那么推荐使用设计模式中的 单例模式:
public class MybatisUtil {
private static volatile SqlSessionFactory sqlSessionFactory = null;
public static SqlSessionFactory getSqlSessionFactory() {
// double check 双重检查, 保证SqlSessionFactory全局只创建一次
if (sqlSessionFactory == null) {
synchronized (MybatisUtil.class) {
if (sqlSessionFactory == null) {
String resource = "mybatis-config.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return sqlSessionFactory;
}
public static SqlSession openSession() {
return getSqlSessionFactory().openSession();
}
}
这里采用的是 单例模式 的经典实现之一的 Double Check Lock:也就是DCL,确保一个JVM只创建一个实例,另外还注意volatile关键字,在单例模式中使用 volatile,主要是使用 volatile 可以禁止指令重排序,从而保证程序的正常运行!
有了单例的SqlSessionFactory,另外还提供了openSession方法供每次使用时获取SqlSession!
用MybatisUtil调用来瞧瞧看,代码简洁多了吧?
try (SqlSession sqlSession = MybatisUtil.openSession()) {
// 1. 得到UserMapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 2. 执行业务代码:各种的增删改查
User user = mapper.selectById(1);
System.out.println(user);
}
这还没完哈,在此基础上,还可以通过泛型方法,再次封装以屏蔽SqlSession,查询的代码封装如下:
public static void select(Class clazz, Consumer consumer) {
try (SqlSession sqlSession = MybatisUtil.openSession()) {
// 1. 得到UserMapper
T mapper = sqlSession.getMapper(clazz);
// 2. 执行业务代码:各种的增删改查
consumer.accept(mapper);
}
}
再来瞧瞧,是不是更简洁了?
MybatisUtil.select(UserMapper.class, (userMapper -> {
User user = userMapper.selectById(1);
System.out.println(user);
}));
如果对你有收获,敢不敢关注我?一起共同成长?
天罡gg:一个全网才5万多粉,竟然干了十多年的架构师,后面还有更多干货等着你,加油!
页面更新:2024-02-26
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号