1. 程式人生 > >MyBatis SqlSessionFactory的幾種常見建立方式

MyBatis SqlSessionFactory的幾種常見建立方式

MyBatis框架主要是圍繞著SqlSessionFactory這個類進行的,這個的建立過程如下:

  1. 定義一個Configuration物件,其中包含資料來源、事務、mapper檔案資源以及影響資料庫行為屬性設定settings
  2. 通過配置物件,則可以建立一個SqlSessionFactoryBuilder物件
  3. 通過 SqlSessionFactoryBuilder 獲得SqlSessionFactory 的例項
  4. SqlSessionFactory 的例項可以獲得操作資料的SqlSession例項,通過這個例項對資料庫進行操作
具體看看SqlSessionFactory的建立 一、通過Configuration.xml配置檔案進行建立
  • <!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>
  • 程式碼實現
  • 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容器管理建立
  • 在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例項即可。