Spring Integration Integration MyBatis

1. Import required jar dependent

!--MyBatis和Spring的整合包 由MyBatis提供-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>
<!--MyBatis的核心jar文件-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>
View Code

2.MyBatis and Spring integration (XML Edition)

1.dao Interface

 

 2.entity entity class

 

 3.service layer interface

 

 4.serviceimpl implementation class

 

 5.PersonDao.xml Configuration

 

 6.jdbc.properties Configuration

 

 7. A large configuration file

<?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: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: // the WWW .springframework.org / Schema / AOP / the Spring-aop.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 "> 
   <- <context:! Scan-Component Base -package = " com.spring " / > -> 
    <! - the built-in spring configuration data source data source -> 
    <= the bean ID"dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 引入属性文件 -->
    <context:property-placeholder location="jdbc.properties.properties"/>
    <!--注册DAO层:mapper的代理对象-->
    <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.spring.dao.PersonDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <!--配置service层对象-->
    <bean id="personService" class="com.spring.service.PersonServiceImpl">
        <property name="dao" ref="personDao"></property>
    </bean>
    <!-- sqlSessionFactory 创建SqlSession对象的工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <Property name = " dataSource "  ref = " dataSource " > </ Property> 
        <-! Mybatis large configuration file -> 
        <Property name = " typeAliasesPackage " value = " com.spring.entity " > </ Property > 
        <- scan sql profile:! xml file of mapper needs -> 
        <Property name = " mapperLocations " value = " the CLASSPATH *:. mapper / * xml " /> 
    </ bean> 
    <- MapperScannerConfigurer scanning mapper! document scanners ->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.spring.dao"></property>
    </bean>

    <!-- transactionManager 事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    <-! Transaction Notifications ->
    </ bean>
    <tx: advice-Transaction Manager =" TransactionManager " the above mentioned id = " txAdvice " > 
        <tx: the Attributes> 
            ! <- Series GET method set the transaction isolation level and propagation behavior -> 
            <tx: Method, name = " GET * " Isolation = " READ_COMMITTED " propagation = " REQUIRED " /> 
        </ TX: Attributes> 
    </ TX: the advice> 
</ Beans>
View Code

8. Test Class

 

 result:

 

 

Guess you like

Origin www.cnblogs.com/mayuan01/p/11797655.html