mybatis connection factory classes implement java

EDITORIAL

A long time ago to write notes in the proper way cloud notes, ready to give it up, migrate notes to be here. Article may have many shortcomings, please understand, welcome Gangster comments.

As used herein, the thing

  1. mybatis
  2. java

1. text

package com.etc.util;

import java.io.InputStream;
import java.sql.Connection;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;

//实现获取、释放mybatis数据库连接的工具类
public class MyBatisSessionFactory {
	//定义常量
	private static String CONFIG_FILE_LOCATION="mybatis-config.xml";
	
	//考虑到该工具类允许被多线程执行。因为封装1个线程池,让每个线程从线程池中获取1个连接。让1个线程对应
	//1条数据库连接,这样更安全
	//ThreadLocal的作用:让"线程"绑定"资源",这样就不会出现多个线程同享资源的情况。更安全。牺牲内存,换取”安全“
	private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
	//Map<ThreadId,SqlSession> sessions;
	private static InputStream is; //用于读取配置文件的流对象
	
	private static SqlSessionFactory fac;//用于管理多个连接的工厂。一个工厂对应1个数据库。
	
	//在该类的静态段中加载配置文件,这样可以确保只执行1次。
	static
	{
		try {
			is = Resources.getResourceAsStream(CONFIG_FILE_LOCATION);//读取配置文件
			fac = new SqlSessionFactoryBuilder().build(is);//通过配置文件创建1个连接工厂
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
		
	}
	//获取1条连接
	public static SqlSession getSession()
	{
		SqlSession s  = threadLocal.get(); //找线程池要1条连接
		if(s==null) //第1次时,拿不到,则由工厂获取1条连接并放入线程池
		{
			s = fac.openSession();//由工厂获取1条连接并放入线程池
			threadLocal.set(s);//放入线程池
		}
		return s;
	}
	//关闭连接
	public static void closeSession()
	{
		SqlSession s  = threadLocal.get();//找线程池要本线程对应的连接
		threadLocal.set(null);//将该连接从线程池中清除
		if(s!=null)
			s.close();//物理关闭连接
	}
}

		//如何获取连接?
		SqlSession session = MyBatisSessionFactory.getSession();
		//如何获取dao的实现类?
		UserinfoDao dao = session.getMapper(UserinfoDao.class);
		//按接口dao类的格式进行查询
		List<Userinfo> list = dao.findAll();
		//如果修改了数据库,要提交修改
		session.commit();
		//关闭连接
		MyBatisSessionFactory.closeSession();		

2. summary

A long time ago to write notes in the proper way cloud notes, ready to give it up, migrate notes to be here. There are ambiguities Comments welcome, I would see the reply. This article is over, what are deficiencies please feel free to correct me.

Published 38 original articles · won praise 17 · views 1232

Guess you like

Origin blog.csdn.net/nineya_com/article/details/103533424