1. 程式人生 > >MyBatis框架進階(三)

MyBatis框架進階(三)

這篇文章中(事務管理、延遲載入、快取、連線池)的概念在我的另一個篇文章Hibernate框架進階(三)中都有作介紹,這裡就不再過多介紹

MyBatis事務管理

MyBatis管理事務是分為兩種方式:

(1)使用JDBC的事務管理機制,就是利用java.sql.Connection物件完成對事務的提交

(2)使用MANAGED的事務管理機制,這種機制mybatis自身不會去實現事務管理,而是讓程式的容器(JBOSS,WebLogic)來實現對事務的管理

在前面兩篇文章我們已經配置過事務管理,在mybatis-config.xml檔案中

<transactionManager type="JDBC"/>

MyBatis延遲載入

MyBatis延遲載入只需在mybatis-config.xml檔案中加上以下程式碼即可

    <settings>  
         <!-- 開啟延遲載入的開關 -->  
         <setting name="lazyLoadingEnabled" value="true" />  
         <!-- 將積極載入改為訊息載入即按需載入 -->  
         <setting name="aggressiveLazyLoading" value="false"/>  
    </settings>  

MyBatis分頁功能

為了便於觀察,在category表新增100個數據

        for (int i = 0; i < 100; i++) {
            Category c = new Category();
            c.setName("category name " + i);
            session.insert("addCategory", c);
        }

在Category.xml新增以下程式碼

	<select id="limitListCategory" resultType="Category">
		select * from category
		<if test="start!=null and count!=null">
			limit #{start},#{count}
		</if>
	</select>
在主類中編寫執行邏輯
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("count", 5);
		List<Category> categories = session.selectList("limitListCategory", map);
		for (Category category : categories) {
			System.out.println(category);
		}

利用PageHelper實現分頁功能

PageHelper是一款犀利的Mybatis分頁外掛,使用了這個外掛之後,分頁開發起來更加簡單容易。

新增PageHelper外掛依賴

		<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.1.4</version>
		</dependency>
mybatis-config.xml檔案中加上以下程式碼,開啟PageHelper外掛
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
	</plugins>
在主類中編寫執行邏輯
		// pagehelper外掛實現分頁功能
		PageHelper.offsetPage(0, 10);
		List<Category> categories = session.selectList("listCategory");
		for (Category category : categories) {
			System.out.println(category);
		}
		// pagehelper外掛實現獲取總數
		PageInfo<Category> pageInfo = new PageInfo<Category>(categories);
		System.out.println("總數:" + pageInfo.getTotal());
		System.out.println(pageInfo);

MyBatis二級快取

mybatis-config.xml檔案中啟動二級快取
<setting name="cacheEnabled" value="true"/>
在需要開啟二級快取的類配置檔案中新增快取配置,這裡使Category.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
    <mapper namespace="pojo">
        <cache/>     
    </mapper>

Mybatis使用c3p0連線池
新增c3p0依賴

		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.2</version>
		</dependency>
新建類C3P0DataSourceFactoryMyBatis使用C3P0需要自己寫個類繼承UnpooledDataSourceFactory,然後指定dataSource。ComboPooledDataSource。
這個ComboPooledDataSource就是c3p0的資料來源。
package pojo;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0DataSourceFactory extends UnpooledDataSourceFactory{
	
	public C3P0DataSourceFactory() {
		// TODO Auto-generated constructor stub
		this.dataSource = new ComboPooledDataSource();
	}

}
修改mybatis-config.xml配置檔案
			<dataSource type="pojo.C3P0DataSourceFactory">
				<property name="driverClass" value="com.mysql.jdbc.Driver" />
				<property name="jdbcUrl"
					value="jdbc:mysql://localhost/mybatis?characterEncoding=utf8&useSSL=true" />
				<property name="user" value="root" />
				<property name="password" value="admin" />
			</dataSource>