1. 程式人生 > >Spring(4)之 Spring與Mybatis整合

Spring(4)之 Spring與Mybatis整合

Spring與Mybatis整合

  1. 整合思路
    Spring管理:Mybatis的 SqlSessionFactory、Mapper
    第一步:整合dao
    第二步:新增事務管理
    第三步:整合service、Spring管理Service介面、service通過IOC容器呼叫Dao(mapper)
  2. jar 包
    ① mybatis 的 jar包
    mybatis : mybatis、log4j、cglib、slf4j、javassist、asm)
    ② spring 的 jar包:
    spring-core: spring-core、spring-context、spring-beans、spring-expression、spring-logging、aopalliance)
    spring-aop:
    aspectjrt.jar、aspectjweaver.jar、spring-aop)
    spring-jdbc: spring-jdbc、spring-orm、spring-tx、mysql-connector、c3p0)
    ③ mybatis和 spring的整合包
    mybatis-spring: mybatis-spring
    ④ 資料庫驅動包
  3. 工程結構
    配置檔案:
    1.SqlMapConfig.xml —mybais配置檔案:配置別名、setting、mapper(一般在spring配置檔案中配置)
    2.Spring配置檔案(可寫在一個配置檔案也可以分開寫):
    bean.xml
    bean-dao.xml
    —配置資料來源、SqlSessionFactory、Mapper掃描器
    bean-service.xml —配置 service
    bean-transaction.xml —事務管理
  4. Mybatis與Spring整合的過程中:
    Spring對於 Mybatis中 Mapper的管理方式有兩種(兩者都是mybatis和spring整合包中的類 [ 如下方class="org.mybatis.spring… ]):(下方 1.2 bean-dao.xml 中):

①. MapperFactoryBean:
缺點:如果有多個 Mapper介面物件,則要配置多次;

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.asd.mapper.UserMapper"></property>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

②. MapperScannerConfigurer:
可以一次性配置多個 Mapper介面物件;
(第二種管理 Mapper的方式,使用 MapperScannerConfigurer:自動生成代理物件,sqlSessionFactory 可以不寫; MapperScannerConfigurer: 是Mapper掃描器,將指定包下的mapper介面自動建立物件,bean的id是mapper的類名,首字母小寫; <property name=“basePackage”…>: 配置掃描包的路徑,要求:mapper.xml與mapper.java同名且同目錄;<property name=“sqlSessionFactoryBeanName”…>: 使用 sqlSessionFactory;)

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.asd.mapper"/>
    //<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

eg:工程目錄結構:
在這裡插入圖片描述
eg:1.1 SqlMapConfig.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>
    <typeAliase> 
        <package name="com.iotek.po"/>
    </typeAliase>

    <!-- 若是讓Spring來管理,此處可以不配置 -->
    <!-- <mapper>
    	<package name="com.asd.mapper"/>
    </mapper>-->

</configuration>

1.2 bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 開啟註解掃描 -->
    <context:component-scan base-package="com.asd"></context:component-scan>

</beans>

1.2 bean-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1. 資料庫連線池 -->
    <!-- 法一: -->
    <!-- <bean id="dataSource" class="com.mchange.v2.com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value="12345"></property>
    </bean> -->
    <!-- 法二: -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="${jdbc.driver}"></property>
    	<property name="jdbcUrl" value="${jdbc.url}"></property>
    	<property name="user" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 2. SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置資料來源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 載入mybatis的配置檔案 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
    </bean>
    
    <!-- 3. Mapper配置 -->
    
    <!-- 法二(常用)簡化版:第二種管理Mapper的方式,使用MapperScannerConfigurer:自動生成代理物件,sqlSessionFactory 可以不寫-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.asd.mapper"/>
    </bean>
   
    <!-- 法二(常用)未簡化版:-->
    <!-- MapperScannerConfigurer:是Mapper掃描器,將指定包下的mapper介面自動建立物件,bean的id是mapper的類名(首字母小寫) -->
    <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置掃描包的路徑,要求:mapper.xml與mapper.java同名且同目錄 -->
        <property name="basePackage" value="com.asd.mapper"/>
        <!-- 使用sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>-->
    
    <!-- 法一: 第一種管理Mapper的方式,使用MapperFactoryBean -->
    <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    	<property name="mapperInterface" value="com.asd.mapper.UserMapper"></property>
    	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <bean id="userMapper2" class="org.mybatis.spring.mapper.MapperFactoryBean">
    	<property name="mapperInterface" value="com.asd.mapper.OrderMapper"></property>
    	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>-->
    
</beans>

1.2 ’ db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=12345

1.2’ bean-service.xml
(法一:可以使用 xml 方式生成 service的 bean物件;法二:也可以使用註解掃描的方式生成 Service類的物件:則下方 1.5 UserService.java 中可以使用@Service的類註解;)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 法一: -->
    //<bean id="userService" class="com.asd.service.UserService"></bean>
    <!-- 法二: -->
    <context:component-scan base-package="com.asd"></context:component-scan>

</beans>

1.2’’ bean-transaction.xml
(事務管理兩種方式:第一種: 配置事務管理器、事務增強、AOP;
第二種: 開啟事務註解,這樣可以在需要配置事務的類、或者方法上直接使用註解。)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
        
    <!-- 法一: -->
    
    <!-- 事務增強 -->
    <!--<tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>
    		<tx:method name="save" read-only="false"/>
    		<tx:method name="get*" read-only="true"/>
    		<tx:method name="*" read-only="false"/>
    	</tx:attributes>
    </tx:advice>
    <!-- AOP(切面) -->
    <aop:config>
    	<aop:advisor advice-ref="txAdvice" point-cut="execution(* com.asd.service.*.*(..))"/>
    </aop:config>  -->
    
     <!-- 法二: -->
     //使用此方法開啟事務後,可以直接在下方1.5 UserService.java的類或者方法上使用@Transactional註解
     <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

1.3 User.java

public class User{
	private int id;
	private String username;
	private int age;
	set、get();
}

1.4 UserMapper.java

public interface UserMapper{
	public User findUserById(int id)throws Exception;
}

1.4’ UserMapper.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="com.asd.mapper.UserMapper">
    <select id="findUserById" parameterType="int" resultType="user">
	    select * from user where id=#{id}
   </select>
</mapper>

1.5 UserService.java

(因為1.2 bean-dao.xml 中有 mapper的配置,即Mapper掃描器,掃描指定包下的mapper介面自動建立 mapper物件(bean的id是mapper的類名,首字母小寫),所以此處生成了userMapper可以使用@Autowired註解來自動注入; 1.2’ bean-service.xml 中使用 xml方式生成 UserService的 bean物件;)對於:
@Service   此註解在上方1.2’ bean-service.xml中(法二)開啟註解掃描後才可使用;
@Autowired   此註解無需上方1.2’ bean-service.xml中(法二)開啟註解掃描後才可使用,因為1.2 bean-dao.xml 中有 mapper掃描器,可自動生成 mapper物件了;

@Service 
@Transactional
public class UserService{
    @Autowired  //自動注入
    private UserMapper userMapper;
    
    public User findUserById(int id) throws Exception{
        return userMapper.findUserById(id);
    } 
}

Test.java
(此 .getBean(“sqlSessionFactory”)是上面1.2 bean-dao.xml中生成的 sqlSessionFactory的 bean物件;)

public class Test{
	ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring/bean.xml","spring/bean-*.xml");
	@Test
	public void test throws Exception{
    	SqlSessionFactory factory=(SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
    	SqlSession sqlSession=factory.openSession();
    	UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    	System.out.println(userMapper.findUserById(20));
    } 
    
    @Test
    public void test2 throws Exception{
        UserService userService=(UserService)applicationContext.getBean("userService");
        System.out.println(userService.findUserById(20));
        //直接得到UserService的bean物件,通過userService呼叫方法
        //【結果: User[id=20,username=測試3,age=2] 】
    }

}