力所能及之关于Mybatis SqlSession工具类
来源:互联网 发布:青海干部网络教育平台 编辑:程序博客网 时间:2024/05/17 03:08
小狼最近在做mybatis的CURD,发现一个小狼不能接受的问题,当然,小狼是比较懒的,见不得冗余代码
每一个CURD操作,小狼都得写一遍下面的代码,一口老血吐出来了...
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();InputStream config = Resources.getResourceAsStream("mybatities-config.xml");SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(config);SqlSession sqlSession = sqlSessionFactory.openSession();
为了小狼后半生幸福,决定了,写一个工具类
public class GetSqlSession {private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>();synchronized public static SqlSessionFactory getSqlSessionFactory() {try {InputStream isRef=GetSqlSession.class.getResourceAsStream("/DBMysql.properties");Properties properties=new Properties();properties.load(isRef);//Log.getLog().info("properties="+properties);if (sqlSessionFactory == null) {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties);} else {}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return sqlSessionFactory;}public static SqlSession getSqlSession() {SqlSession sqlSession = tl.get();if (sqlSession == null) {sqlSession = getSqlSessionFactory().openSession();tl.set(sqlSession);} else {}//System.out.println("获得的sqlSession对象的hashCode:" + sqlSession.hashCode());return sqlSession;}public static void commit() {if (tl.get() != null) {tl.get().commit();tl.get().close();tl.set(null);//System.out.println("提交了");}}public static void rollback() {if (tl.get() != null) {tl.get().rollback();tl.get().close();tl.set(null);//System.out.println("回滚了");}}}
小狼上一次说了,sqlsessionFactory的生命周期是整个项目运行期间,他都会存在。但是我们采用单例懒汉模式,比较人性化。每一个线程都得使用一个sqlsession,并且sqlsession是不能共享的,它是线程不安全的,所以这就涉及到多线程的问题。我们采用ThreadLocal<SqlSession>,为每一个线程创建一个sqlsession副本,副本跟副本之间互相不影响。这样我们获得sqlsession对象的时候,直接这么写
SqlSession sqlSession = GetSqlSession.getSqlSession();大家可能发现一个问题,小狼这里写了一句
synchronized public static SqlSessionFactory getSqlSessionFactory() {
try {
InputStream isRef=GetSqlSession.class.getResourceAsStream("/DBMysql.properties");
Properties properties=new Properties();
properties.load(isRef);
//Log.getLog().info("properties="+properties);
if (sqlSessionFactory == null) {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources
.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream,properties);
} else {
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sqlSessionFactory;
}
这个小狼可以解释一下,小狼用的property,属性配置mybatis-config.xml,里面的参数,所以得读出property的文件内容,并与已经读出的mybatis-config.xml文件内容匹配,得到值,注入
写完了这么多,一口老血又咽回去了。。。
- 力所能及之关于Mybatis SqlSession工具类
- MyBatis的工具类SqlSession
- 【SSM】Mybatis之SqlSession
- Mybatis 之 SqlSession
- mybatis sqlSession管理工具类
- myBatis源码学习之SqlSession
- mybatis使用之SqlSession获取
- 力所能及之Mybatis 范围和生命周期
- SqlSession的工具类
- 力所能及之关于json配置问题
- 关于MyBatis sqlSession的一点整理
- 关于MyBatis sqlSession的一点整理
- Mybatis SqlSession
- mybatis sqlsession
- MyBatis sqlSession
- mybatis-spring深入学习之SqlSession
- (四)MyBatis源码解析之SqlSession
- MyBatis集合Spring(二)之SqlSession
- android开发环境安装
- 了解Cocos2d-x整个引擎框架——helloworld
- 做个只有登录功能的系统
- Ajax的初步认识和封装
- Linux下which、whereis、locate、find 命令的区别
- 力所能及之关于Mybatis SqlSession工具类
- Linux 文件权限
- php 数组遍历和排序
- Binary Tree Right Side View--LeetCode
- 宝葫芦
- AFN请求方式-post请求
- Android 4.0 Launcher2源码分析——主布局文件
- Java优化的基本问题
- 安卓各版本占比