MyBatis SqlSessionFactory的几种常见创建方式

原文链接:https://blog.csdn.net/jimolangge123/article/details/49228255

MyBatis框架主要是围绕着SqlSessionFactory这个类进行的,这个的创建过程如下:

  1. 定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings
  2. 通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象
  3. 通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。
  4. SqlSessionFactory 的实例可以获得操作数据的SqlSession实例,通过这个实例对数据库进行操作
具体看看SqlSessionFactory的创建
一、通过Configuration.xml配置文件进行创建
  1.配置文件:
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="ssm/jdbc.properties"></properties>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		</environment>
	</environments>	
	
	<mappers>
		<mapper resource="ssm/BlogMapper.xml"/>
	</mappers>
</configuration>

2.读取配置文件:

package ssm;

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;

import com.sm.model.User;

public class GetSqlSessionFactoryFromXML {

	public static void main(String[] args) throws IOException {
		//配置文件的名称
		String resource = "ssm/configuration.xml";
		//通过Mybatis包中的Resources对象很轻松的获取到配置文件
		Reader reader = Resources.getResourceAsReader(resource);
		//通过SqlSessionFactoryBuilder创建
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		//获得session实例
		SqlSession session =sqlSessionFactory.openSession();
		User user = new User();
		user.setId(8);
		//完成数据库的插入
		session.insert("add", user);
		session.commit();
		session.close();
		System.out.println(sqlSessionFactory);
	}
}

二、在程序中构建这些对象来创建

 

package ssm;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import com.sm.model.User;

public class GetSqlSessionFactoryFromProgram {

	public static void main(String[] args) throws SQLException {
		String driver = "oracle.jdbc.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String username="scott";
		String password="tiger";
		//创建使用缓存池的数据源
		/* 
		 * <dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		 */
		DataSource dataSource =new PooledDataSource(driver,url,username,password);
		
		//创建事务
		/*
		 * <transactionManager type="JDBC" />
		 */
		TransactionFactory transactionFactory =  new JdbcTransactionFactory();
		
		Environment environment = new Environment("development", transactionFactory, dataSource);
		
		Configuration configuration = new Configuration(environment);
		//加入资源
		/*
		 * <mapper resource="ssm/BlogMapper.xml"/>
		 */
		configuration.addMapper(UserMapper.class);
		SqlSessionFactory sqlSessionFactory = new  SqlSessionFactoryBuilder().build(configuration);
		System.out.println(sqlSessionFactory);
		
		SqlSession session =  sqlSessionFactory.openSession();
		User user = new User();
		user.setId(6);
 		session.insert("add", user);
 		session.commit();
		session.close();
	}
}

 

三、通过与Spring集成,由Spring容器管理创建

 

 1. 在spring的配置文件applicationContext.xml中配置

 

<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="configLocation" value="classpath*:conf/configuration.xml"/>
	
</bean>

 

如果使用则直接注入,获取SqlSessionFactory实例即可。

 

posted @ 2018-05-10 17:38  寻水的小鱼  阅读(16980)  评论(0编辑  收藏  举报