冒泡泡de可乐
代码都是思想和概念的体现~每多学一点知识,就能少写一行代码~
 
MyBatis工作流程:
1、通过Reader对象读取src目录下的mybatis.xml配置文件
2、通过SqlSessionFactoryBuilder对象创建SqlSessionFactory对象
3、从当前线程中获取SqlSession对象
4、事务开始,在mybatis中默认
5、通过SqlSession对象读取mapper.xml中的id,从而读取sql语句
6、事务提交,,在mybatis中默认
7、关闭SqlSession对象,并且分开当前线程与SqlSession对象,让GC尽早回收
 
 1 //得到SqlSessionFactory 对象
 2 private SqlSessionFactory getSqlSessionFactory(){
 3   if (sessionFactory == null) {
 4     String resource =  Play.configuration.getProperty("mybatis.configuration");     //得到mybatis.xml的地址
 5     
 6     Reader   reader = Resources.getResourceAsReader(resource);    //通过Reader读取配置文件
 7  
 8     Properties prop = new Properties();
 9     
10     prop.put("url", Play.configuration.getProperty("db.mct.url"));
11     prop.put("user", Play.configuration.getProperty("db.mct.user"));
12     prop.put("pass", Play.configuration.getProperty("db.mct.pass"));
13     prop.put("driver", Play.configuration.getProperty("db.mct.driver"));
14 
15     sessionFactory = new SqlSessionFactoryBuilder().build(reader, prop);   //通过SqlSessionFactoryBuilder对象创建SqlSessionFactory对象
16 
17   }
18   
19   return sessionFactory;
20 }
 1 public List<String> getScreenShotPackageNames(Long taskId, String sn)  throws Exception {
 2         SqlSession sqlSession =  IbatisSessionFactoryMctDB.get().openSession();      //获取SqlSession对象
 3         try {
 4             DfxTaskMapper mapper =  sqlSession.getMapper(DfxTaskMapper.class);   //通过SqlSession对象读取DfxTaskMapper文件
 5             Map<String, Object> mmap = new HashMap<String, Object>();
 6             mmap.put("taskId",  taskId );
 7             mmap.put("sn",  sn );
 8           List<String> pkgNames= mapper.getScreenShotPackageNames(mmap);   //调用mapper中的方法
 9             return pkgNames;
10         }catch (Exception e){
11             throw e;
12         }finally {
13             if(null!=sqlSession){
14                 sqlSession.close();      //关闭sqlSession对象
15             }
16         }
17     }
18 }

https://www.cnblogs.com/ysocean/tag/MyBatis%E8%AF%A6%E8%A7%A3%E7%B3%BB%E5%88%97/

posted on 2018-09-06 19:56  HathawayLee  阅读(151)  评论(0编辑  收藏  举报