Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
package cn.bdqn.mhouse.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * * * 项目名称:mhouse * 类名称:MybatisUtil * 类描述: 工具类 * 创建人:Mu Xiongxiong * 创建时间:2017-3-15 下午1:31:54 * 修改人:Mu Xiongxiong * 修改时间:2017-3-15 下午1:31:54 * 修改备注: * @version * */ public class MybatisUtil { private static SqlSessionFactory factory; private static ThreadLocal<SqlSession> sl=new ThreadLocal<SqlSession>(); private static final String RESOURCE="mybatis-config.xml"; /** * 静态初始化 */ static{ try { Reader reader=Resources.getResourceAsReader(RESOURCE); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); factory=builder.build(reader); } catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } } /** * * @Title: getSession * @Description: 该方法的主要作用:打开session * @param @return 设定文件 * @return 返回类型:SqlSession * @throws */ public static SqlSession getSession(){ SqlSession session=sl.get(); if(session==null){ session=factory.openSession(); sl.set(session); } return session; } /** * * @Title: closeSession * @Description: 该方法的主要作用:关闭session * @param 设定文件 * @return 返回类型:void * @throws */ public static void closeSession(){ SqlSession session=sl.get(); sl.set(null); if(session!=null){ session.close(); } } }
相关推荐
minerd 2020-10-28
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31