Spring和MyBatis的集成

SpringMyBatis的整合java

 

1. Spring和各個框架的整合

 

Spring目前是JavaWeb開發中最終的框架,提供一站式服務,能夠其餘各個框架整合集成spring

 

Spring整合方案sql

 

1.1. SSH

Ssh是早期的一種整合方案數據庫

Struts2 Web層框架express

Spring : 容器框架mybatis

Hibernate : 持久層框架架構

 

2. SSM

主流的項目架構的三大框架(相對其餘框架而言,最優秀)app

 SpringMVC spring本身家的 Web層框架,spring的一個模塊框架

 Spring :容器框架ide

 MyBatis :持久層框架

 

 

3. SpringMyBatis整合

3.1. 集成思路

實際開發,使用Maven項目,直接引入項項目在Maven倉庫中的座標便可

 

學習階段: 手動導入jar包,從零開始集成(鞏固基礎知識)

 

3.2. 建立java項目

 

 

3.3. 準備集成相關jar

3.3.1. Spring依賴包

 

3.3.2. Mybatis依賴包

 

 

3.3.3. MyBatisSpring框架集成的橋樑包

Spring本身並無集成MyBatis框架,須要MyBatis本身來集成,因此須要本身提供Spring框架集成的橋樑包

 

若是咱們使用的mybatis3.4.4 不能直接使用mybatis內置的 橋樑包版本,版本比較低,沒法正常運行,須要單獨下載一個比價高的版本

 

 

3.3.4. 數據庫驅動包和鏈接池

 

 

 

 

 

3.3.5. Mybatis支持的日誌包log4j

 

 

3.4. 項目集成須要各類配置文件

 

 

 

 

3.5. 完成項目層與層之間spring對象的建立和依賴關係的維護

3.5.1. Service

package cn.zj.mybatis.service.impl;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import cn.zj.mybatis.mapper.UserMapper;

import cn.zj.mybatis.pojo.User;

import cn.zj.mybatis.service.UserService;

 

@Service

public class UserServiceImpl implements UserService {

/*

 * 問題: UserMapper 代理對象如何建立?

 * 答 :使用 SqlSession 操做對象建立 !

 *

 * 問題 : SqlSession 對象如何建立?

 *  

 * 答 : SqlSessionFactory 工廠對象建立?

 *

 * 問題: SqlSessionFactory 對象如何建立

 *

 * 1,和Spring框架集成以前

 *  MyBatis框架本身讀取配置文件中的相關配置去建立

 * 2, 和Spring框架集成以後

 *  交個Spring容器來建立

 * 問題: 如何在Spring框架中配置,建立出來SqlSessionFactory對象?

 *  mybatis和spring集成的類查閱 橋樑包

 *  org.mybatis.spring.SqlSessionFactoryBean 建立 SqlSessionFactory

 *

 */

@Autowired

private UserMapper mapper;

 

@Override

public int insert(User user) {

return mapper.insert(user);

}

 

@Override

public User selectByPrimaryKey(Integer id) {

System.out.println(mapper);

return mapper.selectByPrimaryKey(id);

}

 

}

 

3.5.2. 測試代碼

package cn.zj.mybatis.test;

 

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

import cn.zj.mybatis.pojo.User;

import cn.zj.mybatis.service.UserService;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class UserServiceTest {

 

@Autowired

private UserService service;

 

@Test

public void testInsert() {

User user = new User(null, "喬峯", "qiaofeng", 30);

int row = service.insert(user);

System.out.println(row);

 

}

 

@Test

public void testSelectByPrimaryKey() {

User user = service.selectByPrimaryKey(8);

System.out.println(user);

}

 

}

 

 

3.5.3. applicationContext配置文件的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

    http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        ">

 

 

<!-- 設置註解配置包掃描位置 -->

<context:component-scan base-package="cn.zj.mybatis"/>

 

</beans>

 

3.6. MyBatis 框架SqlSessionFactory對象的建立

 

 * 問題: UserMapper 代理對象如何建立?

 * 答 :使用 SqlSession 操做對象建立 !

 *

 * 問題 : SqlSession 對象如何建立?

 *  

 * : SqlSessionFactory 工廠對象建立?

 *

 * 問題: SqlSessionFactory 對象如何建立

 *

 * 1,和Spring框架集成以前

 *  MyBatis框架本身讀取配置文件中的相關配置去建立

 * 2, 和Spring框架集成以後

 *  交個Spring容器來建立

 * 問題: 如何在Spring框架中配置,建立出來SqlSessionFactory對象?

 *  mybatisspring集成的類查閱 橋樑包

 *  org.mybatis.spring.SqlSessionFactoryBean 建立 SqlSessionFactory

 *

 */

建立MyBatis框架工廠對象的 類在mybatis-spring1.2.1.jar 橋樑包中的

org.mybatis.spring.SqlSessionFactoryBean  以下圖

 

 

3.6.1. 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

    http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        ">

 

 

<!-- 配置讀取 db.properties 數據庫配置文件 -->

<context:property-placeholder location="classpath:db.properties"/>

 

<!-- 配置數據源鏈接池 -->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

<property name="driverClassName" value="${jdbc.driverClassName}"/>

<property name="url" value="${jdbc.url}"/>

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}"/>

<property name="maxActive" value="${jdbc.maxActive}"/>

</bean>

 

 

<!--

配置MyBatis框架的 SqlSessionFactoryBean 類,建立

 

 SqlSessionFactory 工廠對象

 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 1.注入數據源 -->

<property name="dataSource" ref="dataSource"/>

 

 

<!-- 2.配置映射文件 -->

<property name="mapperLocations">

<array>

<!-- <value>classpath:cn/zj/mybatis/mapper/UserMapper.xml</value> -->

<!-- 可使用通配符 * 讀取 目錄下面全部的配置文件 -->

<value>classpath:cn/zj/mybatis/mapper/*Mapper.xml</value>

</array>

</property>

 

<!-- 3. 配置別名使用包掃描 -->

<property name="typeAliasesPackage" value="cn.zj.mybatis.pojo"/>

 

<!-- 4.讀取mybat-config.xml配置文件,此配置文件可能還會配一些mybatis框架的

其餘個性化配置

實際項目開發可能不用配置

 -->

 <property name="configLocation" value="classpath:mybatis-config.xml"/>

</bean>

</beans>

3.7. 建立MyBatisMapper接口的代理對象

使用橋樑包 org.mybatis.spring.mapper.MapperFactoryBean<T> 建立 UserMapper代理對象

 

 

此種方式每個Mapper接口須要單獨配置,若是Mapper過多,建立Mapper可能形成配置代碼過多

    <!-- 建立UserMapper代理對象-建立單個Mapper對象

   

 使用橋樑包 org.mybatis.spring.mapper.MapperFactoryBean<T> 建立 UserMapper代理對象

  -->

  

  <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

  

   <!-- 注入SqlSessionFacotry對象 -->

   <property name="sqlSessionFactory" ref="sqlSessionFactory"/>

  

   <!-- 注入UserMapper接口類型:底層建立UserMapper的代理對象 -->

   <property name="mapperInterface" value="cn.zj.mybatis.mapper.UserMapper"/>

  

  </bean>

 

 

3.8. 使用包掃描建立MyBatisMapper接口的代理對象

 

 

 

<!-- 批量建立Mapper代理對象 ,使用包掃描建立Mapper代理對象

  使用橋樑包

  org.mybatis.spring.mapper.MapperScannerConfigurer

  -->

  

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

   <!-- 配置須要建立Mapper接口代理對象對應的包 -->

   <property name="basePackage" value="cn.zj.mybatis.mapper"/>

  

   <!-- 配置SqlSessionFactoryBean 的名稱,不是引用 -->

   <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

  </bean>

 

3.9. MyBatis的事務管理器的配置

通常開發,事務的管理都會使用aop切入到業務層

 

  <!-- 配置事務管理器 -->

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

   <!-- 注入數據源 -->

   <property name="dataSource" ref="dataSource"/>

  </bean>

  

  <!-- spring事務配置 -->

  <tx:advice id="txAdvice" transaction-manager="transactionManager">

  

   <!-- 事務屬性配置 -->

   <tx:attributes>

   <!-- DQL :查詢操做,配置只讀事務 -->

   <tx:method name="get*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

   <tx:method name="select*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

   <tx:method name="find*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

   <tx:method name="query*" read-only="true" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

  

   <!-- 其餘 SQL :非只讀事務 -->

   <tx:method name="*" read-only="false" isolation="REPEATABLE_READ"  propagation="REQUIRED"/>

  

   </tx:attributes>

  

  </tx:advice>

  

  <!-- 配置AOP 切入事務 -->

  

  <aop:config>

   <!-- 切入點 -->

   <aop:pointcut expression="execution(* cn.zj.mybatis.service..*.*(..))" id="pt"/>

  

   <!-- 切面 -->

   <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>

  </aop:config>

相關文章
相關標籤/搜索