框架整合——Spring與MyBatis框架整合

Spring整合MyBatis

1. 整合 Spring

【整合目標:在spring的配置文件中配置SqlSessionFactory以及讓mybatis用上spring的聲明式事務】java

1). 加入 Spring 的 jar 包和配置文件

<1>、Spring框架須要的jar包:

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jarmysql

<2>、Spring的配置文件Spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


	<!-- 掃描包 -->
	<context:component-scan base-package="com.neuedu">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<!-- 處理全局異常,能夠標記在類上 -->
		<context:exclude-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>


	<!-- 加載外部屬性文件(數據庫驅動) -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置數據庫加載內容(c3p0) -->
	<bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
	</bean>

     <!-- 對於 mybatis 而言,使用的事務管理器是 DataSourceTransactionManager --> 
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="ComboPooledDataSource" />
     </bean>

     <tx:annotation-driven />

	<!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="ComboPooledDataSource"></property>
          <!-- 配置mybatis配置文件的位置和名稱 -->
		<property name="configLocation" value="classpath:mytabis-config.xml"></property>
	</bean>
	<!-- 掃描接口和sql配置文件 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.neuedu.mapper"></property>
	</bean>
</beans>

  

2). 加入 mybatis 的 jar 包和配置文件:實際上須要配置的就是 settings 的部分。

<1>、mybatis須要的jar包:

mybatis-3.2.8.jarweb

<2>、mybatis的配置文件mytabis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
		<!DOCTYPE configuration
		PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 總控配置文件 -->
<configuration>
	<settings>
          <!-- 支持數據庫中下劃線命名的參數向項目對象中駝峯式命名的屬性自動匹配 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
          <!-- 支持懶加載 -->
          <setting name="lazyLoadingEnabled" value="true"/> </settings> </configuration>
     </settings>
</configuration>

 

3). 加入數據庫驅動和數據庫鏈接池的 jar 包

<1>、加入數據庫驅動jdbc.properties:

jdbc.user=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/mytabis
jdbc.driver=com.mysql.jdbc.Driver

<2>、加入數據庫鏈接池的jar包:  

c3p0-0.9.1.2.jar
mysql-connector-java-5.1.7-bin.jarajax

4). 加入其餘須要的jar包:

<1>、動態代理須要的jar包:

cglib-2.2.2.jar
javassist-3.17.1-GA.jar
asm-3.3.1.jarspring

<2>、Spring_MyBatis框架整合jar包

mybatis-spring-1.2.2.jar sql

<3>、ajax須要的包

jackson-all-1.9.11.jar數據庫

<4>、日誌須要的jar包

log4j-1.2.17.jarexpress

共須要24個jar包!mybatis

5). 在 Spring 的配置文件中配置數據源(Spring.xml已配置).

     <!-- 加載外部屬性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置數據庫加載內容(c3p0) -->
	<bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
	</bean>

  

6). 在 Spring 的配置文件中配置 SqlSessionFactory(Spring.xml已配置)

     <!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="ComboPooledDataSource"></property>
          <!-- 配置mybatis配置文件的位置和名稱 -->
		<property name="configLocation" value="classpath:mytabis-config.xml"></property>
	</bean>

 

 

7)在mybatis的全局配置文件中配置settings屬性

<configuration>
	<settings>
          <!-- 支持數據庫中下劃線命名的參數向項目對象中駝峯式命名的屬性自動匹配 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
          <!-- 支持懶加載 -->
          <setting name="lazyLoadingEnabled" value="true"/> </settings> </configuration>
     </settings>
</configuration>

  

8). 最終整合的結果:能夠從 IOC 容器中獲取 Mapper,而後直接調用 Mapper 的方法。

注意:幾乎不直接調用 SqlSession 的任何方法.

須要注意的是:Spring的事務處理的是:runtimeException:而編譯時異常是沒有處理的,因此須要
本身單獨設置RollBackFor=Exception.class
eg:FileInputStream input = new FileInputStream(new File("D:\\2323\23.txt"))mvc

 

例:簡單的對數據庫的操做

EmployeeMapper.java:

public interface EmployeeMapper {
	
	public employee getEmployeeById(int id); 
	
}

EmployeeMapper.xml:

	<select id="getEmployeeById" parameterType="Integer" resultType="com.neuedu.Bean.employee">
		select id,last_name,email,gender
		from tbl_employee 
		where id = #{id} 
	</select>

測試類:

public class smTest {

	@Test
	public void test() {
		ApplicationContext ioc =new ClassPathXmlApplicationContext("Spring.xml");
		EmployeeMapper bean = ioc.getBean(EmployeeMapper.class);
		employee employee = bean.getEmployeeById(10);
		System.out.println(employee);
	}
}

輸出結果爲:

employee [id=10, lastName=hah, gender=1, email=email]

相關文章
相關標籤/搜索