zoukankan      html  css  js  c++  java
  • Spring集成Mybatis,spring4.x整合Mybatis3.x

    Spring集成Mybatis,spring4.x整合Mybatis3.x

    ==============================

    蕃薯耀 2018年3月14日

    http://www.cnblogs.com/fanshuyao/

    一、spring.xml配置文件

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:aop="http://www.springframework.org/schema/aop"  
    5.     xmlns:context="http://www.springframework.org/schema/context"  
    6.     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"  
    7.     xmlns:p="http://www.springframework.org/schema/p"  
    8.     xmlns:tx="http://www.springframework.org/schema/tx"  
    9.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
    11.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
    12.         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd  
    13.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
    14.   
    15.     <context:property-placeholder location="classpath:jdbc.properties"/>  
    16.       
    17.     <context:component-scan base-package="com.lqy.ssm" use-default-filters="false">  
    18.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    19.     </context:component-scan>  
    20.       
    21.     <!-- druid配置数据源 -->  
    22.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
    23.          <!-- 基本属性 url、user、password -->  
    24.          <property name="driverClassName" value="${jdbc_driver_class_name}" />  
    25.          <property name="url" value="${jdbc_url}" />  
    26.          <property name="username" value="${jdbc_username}" />  
    27.          <property name="password" value="${jdbc_password}" />  
    28.       
    29.          <!-- 配置监控统计拦截的filters -->  
    30.          <property name="filters" value="stat" />  
    31.       
    32.          <!-- 配置初始化大小、最小、最大 -->  
    33.          <property name="maxActive" value="20" />  
    34.          <property name="initialSize" value="1" />  
    35.          <property name="minIdle" value="1" />  
    36.       
    37.          <!-- 配置获取连接等待超时的时间 -->  
    38.          <property name="maxWait" value="60000" />       
    39.       
    40.          <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
    41.          <property name="timeBetweenEvictionRunsMillis" value="60000" />  
    42.       
    43.          <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
    44.          <property name="minEvictableIdleTimeMillis" value="300000" />  
    45.       
    46.          <property name="testWhileIdle" value="true" />  
    47.          <property name="testOnBorrow" value="false" />  
    48.          <property name="testOnReturn" value="false" />  
    49.       
    50.          <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->  
    51.          <property name="poolPreparedStatements" value="true" />  
    52.          <property name="maxOpenPreparedStatements" value="20" />  
    53.      </bean>  
    54.        
    55.      <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">  
    56.         <!-- 指定mybatis全局配置文件,已经不需要 -->  
    57.         <!-- <property name="configLocation" value="classpath:mybatis.xml"></property> -->  
    58.         <property name="dataSource" ref="dataSource"></property>  
    59.         <!-- 指定Mybatis mapper文件的位置 -->  
    60.         <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
    61.         <!-- <property name="typeAliasesPackage" value="com.lqy.ssm.bean"></property> -->  
    62.         <property name="plugins">  
    63.             <array>  
    64.                 <!-- 配置pagehelper分页插件,使用见:https://pagehelper.github.io/docs/howtouse/ -->  
    65.                 <bean class="com.github.pagehelper.PageInterceptor">  
    66.                     <property name="properties">  
    67.                         <value>  
    68.                             helperDialect=mysql  
    69.                             reasonable=true  
    70.                             supportMethodsArguments=true  
    71.                             params=count=countSql  
    72.                             autoRuntimeDialect=true  
    73.                         </value>  
    74.                     </property>  
    75.                 </bean>  
    76.             </array>  
    77.         </property>  
    78.      </bean>  
    79.        
    80.      <!-- 配置一个可以批量执行的sqlSession -->  
    81.      <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
    82.         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>  
    83.         <constructor-arg name="executorType" value="BATCH"></constructor-arg>  
    84.      </bean>  
    85.        
    86.      <!-- 配置扫描器,将Mybatis接口的实现加入到ioc容器 -->  
    87.      <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    88.         <!-- 扫描所有dao接口的实现 -->  
    89.         <property name="basePackage" value="com.lqy.ssm.dao"></property>  
    90.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>  
    91.      </bean>  
    92.       
    93.       
    94.     <!-- 配置事务管理器 -->  
    95.     <bean id="transactionManager"  
    96.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    97.         <property name="dataSource" ref="dataSource"></property>  
    98.     </bean>  
    99.       
    100.     <!-- 注解方式的事务 <tx:advice id="txAdvice" transaction-manager="transactionManager" proxy-target-class="true"> -->  
    101.     <!-- 通过AOP配置提供事务增强,事务如何切入 -->   
    102.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    103.         <tx:attributes>  
    104.             <!-- 让service包下所有Bean的所有方法拥有事务 -->  
    105.             <tx:method name="*" />  
    106.             <!--   
    107.             <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
    108.             <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
    109.             <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
    110.             <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
    111.             <tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception" />  
    112.              -->  
    113.             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
    114.             <tx:method name="count*" propagation="SUPPORTS" read-only="true" />  
    115.             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />  
    116.             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />  
    117.             <tx:method name="query*" propagation="SUPPORTS" read-only="true" />  
    118.             <tx:method name="list*" propagation="SUPPORTS" read-only="true" />  
    119.             <tx:method name="page*" propagation="SUPPORTS" read-only="true" />  
    120.         </tx:attributes>  
    121.     </tx:advice>  
    122.       
    123.     <aop:config proxy-target-class="true">  
    124.         <!-- 切点表达式 -->  
    125.         <aop:pointcut id="serviceMethod" expression=" execution(* com.lqy.ssm.service..*(..))" />  
    126.         <!-- 引用到事务增强 -->  
    127.         <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />  
    128.     </aop:config>  
    129.       
    130.   
    131. </beans>  

    二、jdbc.properties文件

    Java代码  收藏代码
    1. jdbc_username=root  
    2. jdbc_password=12345678  
    3. jdbc_driver_class_name=com.mysql.jdbc.Driver  
    4. jdbc_url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull  

    三、springMvc.xml文件

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:context="http://www.springframework.org/schema/context"  
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
    8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
    9.   
    10.     <!-- use-default-filters设置为false关闭默认扫描 -->  
    11.     <context:component-scan base-package="com.lqy.ssm" use-default-filters="false">  
    12.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    13.     </context:component-scan>  
    14.       
    15.     <!-- 视图解析器 -->  
    16.     <mvc:view-resolvers>  
    17.         <mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/>  
    18.     </mvc:view-resolvers>  
    19.       
    20.     <!-- 支持对象与json的转换 -->  
    21.     <!-- <mvc:annotation-driven /> -->  
    22.     <mvc:annotation-driven>  
    23.         <!-- conversion-service="conversionService" -->  
    24.         <mvc:message-converters>  
    25.             <!-- UTF-8字符格式化 -->  
    26.             <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
    27.                 <property name="supportedMediaTypes">  
    28.                     <list>  
    29.                         <value>application/json;charset=UTF-8</value>  
    30.                         <value>application/xml;charset=UTF-8</value>  
    31.                         <value>text/html;charset=UTF-8</value>  
    32.                         <value>text/plain;charset=UTF-8</value>  
    33.                         <value>text/xml;charset=UTF-8</value>  
    34.                     </list>  
    35.                 </property>  
    36.             </bean>  
    37.   
    38.             <!-- 处理responseBody 里面日期类型 -->  
    39.             <bean  
    40.                 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
    41.                 <property name="objectMapper">  
    42.                     <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
    43.                         <property name="dateFormat">  
    44.                             <bean class="java.text.SimpleDateFormat">  
    45.                                 <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
    46.                             </bean>  
    47.                         </property>  
    48.                     </bean>  
    49.                 </property>  
    50.             </bean>  
    51.         </mvc:message-converters>  
    52.     </mvc:annotation-driven>  
    53.       
    54.     <!-- 将springMvc不能处理的请求交给tomcat -->  
    55.     <mvc:default-servlet-handler/>  
    56.       
    57.     <!-- 配置文件上传 -->  
    58.     <bean id="multipartResolver"  
    59.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    60.         <property name="defaultEncoding">  
    61.             <value>UTF-8</value>  
    62.         </property>  
    63.         <property name="maxUploadSize">  
    64.             <value>32505856</value>     <!-- 上传文件大小限制为31M,31*1024*1024 -->  
    65.         </property>  
    66.         <property name="maxInMemorySize">  
    67.             <value>4096</value>  
    68.         </property>  
    69.     </bean>  
    70.       
    71.       
    72.       
    73.   
    74. </beans>  

    四、pom.xml文件

    Xml代码  收藏代码
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
    3.   <modelVersion>4.0.0</modelVersion>  
    4.   <groupId>lqy</groupId>  
    5.   <artifactId>ssm</artifactId>  
    6.   <packaging>war</packaging>  
    7.   <version>0.0.1-SNAPSHOT</version>  
    8.   <name>ssm Maven Webapp</name>  
    9.   <url>http://maven.apache.org</url>  
    10.     
    11.     
    12.     <properties>  
    13.         <spring.version>4.3.13.RELEASE</spring.version>  
    14.           
    15.         <mybatis.version>3.4.6</mybatis.version>  
    16.         <mybatis.spring.version>1.3.1</mybatis.spring.version>  
    17.         <mybatis.generator.version>1.3.6</mybatis.generator.version>  
    18.         <pagehelper.version>5.1.2</pagehelper.version>  
    19.           
    20.         <slf4j.version>1.7.7</slf4j.version>  
    21.           
    22.         <jackson.version>2.6.5</jackson.version>  
    23.         <fastjson.version>1.2.46</fastjson.version>  
    24.           
    25.         <druid.version>1.1.9</druid.version>  
    26.         <mysql.version>5.1.45</mysql.version>  
    27.           
    28.         <el.version>2.2.5</el.version>  
    29.         <standard.version>1.1.2</standard.version>  
    30.         <jstl.version>1.2</jstl.version>  
    31.         <jsp.api.version>2.2</jsp.api.version>  
    32.         <javax.servlet.api.version>3.0.1</javax.servlet.api.version>  
    33.           
    34.         <junit.version>4.12</junit.version>  
    35.     </properties>  
    36.       
    37.       
    38.     <dependencies>  
    39.       
    40.         <!-- spring依赖 -->  
    41.         <dependency>  
    42.             <groupId>org.springframework</groupId>  
    43.             <artifactId>spring-context-support</artifactId>  
    44.             <version>${spring.version}</version>  
    45.         </dependency>  
    46.         <dependency>  
    47.             <groupId>org.springframework</groupId>  
    48.             <artifactId>spring-aspects</artifactId>  
    49.             <version>${spring.version}</version>  
    50.         </dependency>  
    51.         <dependency>  
    52.             <groupId>org.springframework</groupId>  
    53.             <artifactId>spring-instrument</artifactId>  
    54.             <version>${spring.version}</version>  
    55.         </dependency>  
    56.         <dependency>  
    57.             <groupId>org.springframework</groupId>  
    58.             <artifactId>spring-web</artifactId>  
    59.             <version>${spring.version}</version>  
    60.         </dependency>  
    61.         <dependency>  
    62.             <groupId>org.springframework</groupId>  
    63.             <artifactId>spring-webmvc</artifactId>  
    64.             <version>${spring.version}</version>  
    65.         </dependency>  
    66.         <dependency>  
    67.             <groupId>org.springframework</groupId>  
    68.             <artifactId>spring-jdbc</artifactId>  
    69.             <version>${spring.version}</version>  
    70.         </dependency>  
    71.         <dependency>  
    72.             <groupId>org.springframework</groupId>  
    73.             <artifactId>spring-test</artifactId>  
    74.             <version>${spring.version}</version>  
    75.             <scope>test</scope>  
    76.         </dependency>  
    77.           
    78.           
    79.         <dependency>  
    80.             <groupId>com.fasterxml.jackson.core</groupId>  
    81.             <artifactId>jackson-databind</artifactId>  
    82.             <version>${jackson.version}</version>  
    83.         </dependency>  
    84.           
    85.         <!-- fastjson依赖包 -->  
    86.         <dependency>  
    87.             <groupId>com.alibaba</groupId>  
    88.             <artifactId>fastjson</artifactId>  
    89.             <version>${fastjson.version}</version>  
    90.         </dependency>  
    91.           
    92.         <!-- mybatis-->  
    93.         <dependency>  
    94.             <groupId>org.mybatis</groupId>  
    95.             <artifactId>mybatis</artifactId>  
    96.             <version>${mybatis.version}</version>  
    97.         </dependency>  
    98.         <dependency>  
    99.             <groupId>org.mybatis</groupId>  
    100.             <artifactId>mybatis-spring</artifactId>  
    101.             <version>${mybatis.spring.version}</version>  
    102.         </dependency>  
    103.         <dependency>  
    104.             <groupId>org.mybatis.generator</groupId>  
    105.             <artifactId>mybatis-generator-core</artifactId>  
    106.             <version>${mybatis.generator.version}</version>  
    107.             <scope>provided</scope>  
    108.         </dependency>  
    109.         <!-- mybatis分页插件 使用见:https://pagehelper.github.io/docs/howtouse/-->  
    110.         <dependency>  
    111.             <groupId>com.github.pagehelper</groupId>  
    112.             <artifactId>pagehelper</artifactId>  
    113.             <version>${pagehelper.version}</version>  
    114.         </dependency>  
    115.           
    116.           
    117.         <!-- druid数据连接池 -->  
    118.         <dependency>  
    119.             <groupId>com.alibaba</groupId>  
    120.             <artifactId>druid</artifactId>  
    121.             <version>${druid.version}</version>  
    122.         </dependency>  
    123.           
    124.         <dependency>  
    125.             <groupId>mysql</groupId>  
    126.             <artifactId>mysql-connector-java</artifactId>  
    127.             <version>${mysql.version}</version>  
    128.         </dependency>  
    129.           
    130.           
    131.         <!-- jsp页面依赖包 -->  
    132.         <dependency>  
    133.             <groupId>javax.el</groupId>  
    134.             <artifactId>javax.el-api</artifactId>  
    135.             <version>${el.version}</version>  
    136.         </dependency>  
    137.         <dependency>  
    138.             <groupId>taglibs</groupId>  
    139.             <artifactId>standard</artifactId>  
    140.             <version>${standard.version}</version>  
    141.         </dependency>  
    142.         <dependency>  
    143.             <groupId>javax.servlet</groupId>  
    144.             <artifactId>jstl</artifactId>  
    145.             <version>${jstl.version}</version>  
    146.         </dependency>  
    147.         <dependency>  
    148.             <groupId>javax.servlet.jsp</groupId>  
    149.             <artifactId>jsp-api</artifactId>  
    150.             <version>${jsp.api.version}</version>  
    151.             <scope>provided</scope>  
    152.         </dependency>  
    153.         <dependency>  
    154.             <groupId>javax.servlet</groupId>  
    155.             <artifactId>javax.servlet-api</artifactId>  
    156.             <version>${javax.servlet.api.version}</version>  
    157.             <scope>provided</scope>  
    158.         </dependency>  
    159.                   
    160.           
    161.         <dependency>  
    162.             <groupId>junit</groupId>  
    163.             <artifactId>junit</artifactId>  
    164.             <version>${junit.version}</version>  
    165.             <scope>test</scope>  
    166.         </dependency>  
    167.           
    168.           
    169.     </dependencies>  
    170.     
    171.     
    172.   <build>  
    173.     <finalName>ssm</finalName>  
    174.       
    175.     <plugins>  
    176.         <plugin>    
    177.             <groupId>org.apache.maven.plugins</groupId>    
    178.             <artifactId>maven-compiler-plugin</artifactId>    
    179.             <version>3.1</version>    
    180.             <configuration>    
    181.                 <source>1.7</source>    
    182.                 <target>1.7</target>    
    183.             </configuration>    
    184.          </plugin>   
    185.     </plugins>  
    186.       
    187.   </build>  
    188. </project>  

    ==============================

    蕃薯耀 2018年3月14日

    http://www.cnblogs.com/fanshuyao/

  • 相关阅读:
    MySQL之SELECT用法
    Linux中find常见用法示例
    date命令
    backupMysql.sh
    Shell脚本中使用function(函数)示例
    zabbix邮件告警
    Zabbix系统数据采集方法总结
    TOMCAT原理详解及请求过程
    tomcat-users.xml 配置
    zabbix_sender用法实例
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/8565524.html
Copyright © 2011-2022 走看看