zoukankan      html  css  js  c++  java
  • MyBatis

     
    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/

  • 相关阅读:
    BZOJ3573: [Hnoi2014]米特运输
    BZOJ3531: [Sdoi2014]旅行
    BZOJ3505: [Cqoi2014]数三角形
    BZOJ3309: DZY Loves Math
    BZOJ3260: 跳
    BZOJ3252: 攻略
    BZOJ3226: [Sdoi2008]校门外的区间
    BZOJ3155: Preprefix sum
    BZOJ2843: 极地旅行社
    BZOJ2671: Calc
  • 原文地址:https://www.cnblogs.com/HathawayLee/p/9600544.html
Copyright © 2011-2022 走看看