Talking about the core configuration of mybatis, the integration of spring and mybatis

In the integration of these two frameworks, we use the jar package of mybatis-spring to integrate, because there is no support for mybatis in spring3, so this is developed by the mybatis community itself

sqlSessionFactory

First of all, we need to know that mybatis is operated through sqlsession, and sqlSession is created by SQLSessionFactory.

So we have to configure a session factory in xml to let spring manage

 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
    </bean>

 datasource

 

There is a configuration property in it that is datasource

 

It is the configuration for connecting to the database. Here I am using alibaba's druid

 

<!-- dataSource configuration -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="url" value="${jdbcUrl}" />
        <property name="username" value="${jdbcUsername}" />
        <property name="password" value="${jdbcPassword}" />

        <!-- Configure initialization size, minimum, maximum -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        <!-- Configure the time for getting a connection to wait for timeout-->
        <property name="maxWait" value="60000" />

        <!-- How long is the configuration interval to perform detection, and detect idle connections that need to be closed, in milliseconds-->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds-->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="${jdbcValidationQuery}" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- Open PSCache and specify the size of PSCache on each connection-->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

        <!-- Configure filters for monitoring statistics interception -->
        <property name="filters" value="stat" />
    </bean>

 scan mapper files

Each table or entity class of mybatis has a corresponding ***.xml file,

Now we will automatically scan the xml file in the mapper package, register it in the spring container, and realize the integration of spring and mybatis

There are several ways to scan

Let's talk about the first

(1)

foldersScannerConfigure

 

  <!-- Spring and mybatis integrated configuration, scan all dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.wonder.mapper"></property>
            </bean>

  There is a basePackage attribute which is used to define the location of the mapper package to be scanned. When there are multiple packages to be scanned, they can be separated by commas

 

In this way, mapperscannerconfigurer can register the xml files in the package as mapperFactoryBean objects one by one,

Of course, sometimes the files in our mapper package are not all we want to scan, so we need to do some filtering

At this time, there are two properties to choose from

One is annotationClass and the other is markerInterface

(1) annotationClass is an annotation filter, that is, only the annotations can be scanned for registration

(2) markerInterface is an interface filter, that is, only those who inherit an interface can scan.

 

The first 

Will be scanned with mybatisMapper annotations

  1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  2.    <property name="basePackage" value="com.wonders.mybatis.mapper" />  
  3.    <property name="annotationClass" value="com.wonders.mybatis.annotation.MybatisMapper"/>  
  4. </bean>  

 

the second

Will implement MapperInterface for scanning

  1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  2.    <property name="basePackage" value="com.wonders.mybatis.mapper" />  
  3.    <property name="markerInterface" value="com.wonders.mybatis.mapper.MapperInterface"/>  
  4. </bean>  

 

When both are present, the union is taken, that is, if one of the two is present, the registration will be scanned.

 

(2) Scan sqlMapperConfig.xml to register

In this configuration is the configuration of each entity class and its corresponding xml

 

<configuration>
<mappers>
    <mapper resource="com/wonder/mapper/User.xml"/>
</mappers>
</configuration>

 

 

Then configure it in spring-mybatis.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
    </bean>

 In this way, the configured xml in the xml configuration can be scanned one by one

 

Finally, the whole configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.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-3.2.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Automatically scan web packages and incorporate annotated classes into spring container management-->
    <context:component-scan base-package="com.wonder"></context:component-scan>

    <!-- dataSource configuration -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- Basic attributes url, user, password -->
        <property name="url" value="${jdbcUrl}" />
        <property name="username" value="${jdbcUsername}" />
        <property name="password" value="${jdbcPassword}" />

        <!-- Configure initialization size, minimum, maximum -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        <!-- Configure the time for getting a connection to wait for timeout-->
        <property name="maxWait" value="60000" />

        <!-- How long is the configuration interval to perform detection, and detect idle connections that need to be closed, in milliseconds-->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- Configure the minimum lifetime of a connection in the pool, in milliseconds-->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="${jdbcValidationQuery}" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- Open PSCache and specify the size of PSCache on each connection-->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

        <!-- Configure filters for monitoring statistics interception -->
        <property name="filters" value="stat" />
    </bean>

    <!-- mybatis file configuration, scan all mapper files -->
  <!--  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" >
            </bean>-->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
    </bean>



    <!-- Spring and mybatis integrated configuration, scan all dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.wonder.mapper"></property>
            </bean>

    <!-- Transaction management of dataSource data source -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource" />

    <!-- Configure Spring to use CGLIB proxy -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!-- Enable support for transaction annotations-->
    <tx:annotation-driven transaction-manager="transactionManager" />

   <!-- <task:scheduled-tasks>
        <task:scheduled ref="timeTaskServiceImpl" method="task" cron="0 * 19 * * ?"/>
    </task:scheduled-tasks>-->

<!--Transaction interception notification-->
    <tx:advice id="txAdvice"  transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="batchInsert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="get*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
            <tx:method name="select*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--aop-->
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wonder..*.*(..))"/>
    </aop:config>

<!--

    <!– Cache配置 –>
    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
-->

</beans>

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326167199&siteId=291194637