首页 > 代码库 > mybatis整合spring

mybatis整合spring

<dependencies>
  
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.9.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        
        
        <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- mysql驱动包依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <!-- alibaba数据源依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.26</version>
        </dependency>
        
        
        <!-- 日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        
        <!-- servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- springmvc返回json相关-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.8</version>
        </dependency>
        
         <!-- springmvc文件上传依赖 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        
        
        
        <!-- <dependency>
            <groupId>org.wltea</groupId>
            <artifactId>analyzer</artifactId>
            <version>2012_u6</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/analyzer-2012_u6.jar</systemPath>
        </dependency> -->
        
        
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        
        
        
    
  </dependencies>
  
  <build>
     <plugins>
        <plugin> 
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source> 
                <target>1.7</target> 
                <encoding>UTF8</encoding> 
            </configuration>
        </plugin>
        
        
        <plugin>  
            <groupId>org.mortbay.jetty</groupId>  
            <artifactId>jetty-maven-plugin</artifactId>  
            <version>8.1.8.v20121106</version>  
            <configuration>  
                <reload>manual</reload>  
                <webAppConfig>  
                    <contextPath>/${project.build.finalName}</contextPath>  
                </webAppConfig>  
                <connectors>  
                    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
                        <port>9080</port>  
                        <!--<maxIdleTime>60000</maxIdleTime>-->  
                    </connector>  
                </connectors>  
            </configuration>  
        </plugin>  
        
        
        <!-- <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>8.1.16.v20140903</version>
          <configuration>
                 <webDefaultXml>src/main/resources/jetty/webdefault.xml</webDefaultXml> 
                 <scanIntervalSeconds>5</scanIntervalSeconds>
                 <webApp>
                     <contextPath>/my</contextPath>
                 </webApp>
                 <connectors>
                     <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                         <port>9000</port>
                         <maxIdleTime>60000</maxIdleTime>
                     </connector>
                 </connectors>
             </configuration>
        </plugin>
         -->
        
    </plugins>
  </build>
  
</project>

 

<?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:p="http://www.springframework.org/schema/p"
    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.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:property-placeholder location="classpath:config.properties"/>
        
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="http://www.mamicode.com/${jdbc_url}" />
        <property name="username" value="http://www.mamicode.com/${jdbc_username}" />
        <property name="password" value="http://www.mamicode.com/${jdbc_password}" />

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="http://www.mamicode.com/0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="http://www.mamicode.com/20" />
        <!-- 连接池最大空闲 -->
        <!-- <property name="maxIdle" value="http://www.mamicode.com/20" /> -->
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="http://www.mamicode.com/0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="http://www.mamicode.com/60000" />

        <!-- <property name="poolPreparedStatements" value="http://www.mamicode.com/true" /> <property 
            name="maxPoolPreparedStatementPerConnectionSize" value="http://www.mamicode.com/33" /> -->

        <property name="validationQuery" value="http://www.mamicode.com/${validationQuery}" />
        <property name="testOnBorrow" value="http://www.mamicode.com/false" />
        <property name="testOnReturn" value="http://www.mamicode.com/false" />
        <property name="testWhileIdle" value="http://www.mamicode.com/true" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/25200000" />

        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="http://www.mamicode.com/true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="http://www.mamicode.com/1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="http://www.mamicode.com/true" />

        <!-- 监控数据库 -->
        <!-- <property name="filters" value="http://www.mamicode.com/stat" /> -->
        <property name="filters" value="http://www.mamicode.com/mergeStat" />
    </bean>
    <!-- myBatis文件 -->
    
    
    
    <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> -->
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="http://www.mamicode.com/classpath:com/dogoframework/mapping/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="http://www.mamicode.com/com.dogoframework.dao" />
        <property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory" />
    </bean>

    <!-- 配置事务管理器 -->
    <!-- <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> -->

    <!--  PROPAGATION_REQUIRED-支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
    PROPAGATION_SUPPORTS-支持当前事务,如果当前没有事务,就以非事务方式执行。
    PROPAGATION_MANDATORY-支持当前事务,如果当前没有事务,就抛出异常。
    PROPAGATION_REQUIRES_NEW-新建事务,如果当前存在事务,把当前事务挂起。
    PROPAGATION_NOT_SUPPORTED-以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
    PROPAGATION_NEVER-以非事务方式执行,如果当前存在事务,则抛出异常。
    PROPAGATION_NESTED-如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
     -->
    <!-- 注解方式配置事物 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
  
    <!-- 拦截器方式配置事物 -->
    <!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="*" rollback-for="Exception"  />
        </tx:attributes>
    </tx:advice>
    <aop:config>
    
        <aop:pointcut id="transactionPointcut" expression="execution(* com.dogoframework.service.*.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut"
            advice-ref="transactionAdvice" />
    </aop:config> -->


</beans>

 

mybatis整合spring