Spring integrates Mybatis, spring4.x integrates Mybatis3.x

Spring integrates Mybatis, spring4.x integrates Mybatis3.x

 

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

Sweet Potato YaoMarch 14, 2018

http://fanshuyao.iteye.com/

 

1. spring.xml 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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	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-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<context:component-scan base-package="com.lqy.ssm" use-default-filters="false">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- druid configuration data source -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
	     <!-- Basic attributes url, user, password -->
	     <property name="driverClassName" value="${jdbc_driver_class_name}" />
	     <property name="url" value="${jdbc_url}" />
	     <property name="username" value="${jdbc_username}" />
	     <property name="password" value="${jdbc_password}" />
	
	     <!-- Configure filters for monitoring statistics interception -->
	     <property name="filters" value="stat" />
	
	     <!-- Configure initialization size, minimum, maximum -->
	     <property name="maxActive" value="20" />
	     <property name="initialSize" value="1" />
	     <property name="minIdle" value="1" />
	
	     <!-- 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="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="true" />
	     <property name="maxOpenPreparedStatements" value="20" />
	 </bean>
	 
	 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<!-- Specify the mybatis global configuration file, no longer needed -->
	 	<!-- <property name="configLocation" value="classpath:mybatis.xml"></property> -->
	 	<property name="dataSource" ref="dataSource"></property>
	 	<!-- Specify the location of the Mybatis mapper file-->
	 	<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	 	<!-- <property name="typeAliasesPackage" value="com.lqy.ssm.bean"></property> -->
	 	<property name="plugins">
	        <array>
	        	<!-- Configure the pagehelper paging plugin, see: https://pagehelper.github.io/docs/howtouse/ -->
	            <bean class="com.github.pagehelper.PageInterceptor">
	            	<property name="properties">
	            		<value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countSql
                            autoRuntimeDialect = true
                        </value>
	            	</property>
	            </bean>
	        </array>
	    </property>
	 </bean>
	 
	 <!-- Configure a sqlSession that can be executed in batches -->
	 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	 	<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
	 	<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	 </bean>
	 
	 <!-- Configure the scanner and add the implementation of the Mybatis interface to the ioc container-->
	 <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<!-- Scan the implementation of all dao interfaces-->
	 	<property name="basePackage" value="com.lqy.ssm.dao"></property>
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
	 </bean>
	
	
	<!-- Configure Transaction Manager -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 注解方式的事务	<tx:advice id="txAdvice" transaction-manager="transactionManager" proxy-target-class="true"> -->
	<!-- Provide transaction enhancement through AOP configuration, how to enter transaction-->	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- Let all methods of all beans in the service package have transactions-->
			<tx:method name="*" />
			<!--
			<tx:method name="save*" 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="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			 -->
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="page*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	
	<aop:config proxy-target-class="true">
		<!-- pointcut expression-->
		<aop:pointcut id="serviceMethod" expression=" execution(* com.lqy.ssm.service..*(..))" />
		<!-- Reference to transaction enhancement -->
		<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
	</aop:config>
	

</beans>

 

 

Second, the jdbc.properties file

jdbc_username=root
jdbc_password=12345678
jdbc_driver_class_name=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

 

 

Three, springMvc.xml 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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

	<!-- use-default-filters is set to false to turn off default scanning -->
	<context:component-scan base-package="com.lqy.ssm" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- View resolver -->
	<mvc:view-resolvers>
		<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/>
	</mvc:view-resolvers>
	
	<!-- Support object and json conversion -->
	<!-- <mvc:annotation-driven /> -->
	<mvc:annotation-driven>
		<!-- conversion-service="conversionService" -->
		<mvc:message-converters>
			<!-- UTF-8 character formatting -->
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
						<value>application/xml;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
						<value>text/plain;charset=UTF-8</value>
				        <value>text/xml;charset=UTF-8</value>
					</list>
				</property>
			</bean>

			<!-- Process the date type in responseBody-->
			<bean
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="objectMapper">
					<bean class="com.fasterxml.jackson.databind.ObjectMapper">
						<property name="dateFormat">
							<bean class="java.text.SimpleDateFormat">
								<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
							</bean>
						</property>
					</bean>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	<!-- Hand over requests that springMvc can't handle to tomcat -->
	<mvc:default-servlet-handler/>
	
	<!-- Configuration file upload -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
			<value>32505856</value> <!-- The upload file size is limited to 31M, 31*1024*1024 -->
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
	
	
	

</beans>

 

Fourth, pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lqy</groupId>
  <artifactId>ssm</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  
	<properties>
		<spring.version>4.3.13.RELEASE</spring.version>
		
		<mybatis.version>3.4.6</mybatis.version>
		<mybatis.spring.version>1.3.1</mybatis.spring.version>
		<mybatis.generator.version>1.3.6</mybatis.generator.version>
		<pagehelper.version>5.1.2</pagehelper.version>
		
		<slf4j.version>1.7.7</slf4j.version>
		
		<jackson.version>2.6.5</jackson.version>
		<fastjson.version>1.2.46</fastjson.version>
		
		<druid.version>1.1.9</druid.version>
		<mysql.version>5.1.45</mysql.version>
		
		<el.version>2.2.5</el.version>
		<standard.version>1.1.2</standard.version>
		<jstl.version>1.2</jstl.version>
		<jsp.api.version>2.2</jsp.api.version>
		<javax.servlet.api.version>3.0.1</javax.servlet.api.version>
		
		<junit.version>4.12</junit.version>
	</properties>
  	
  	
	<dependencies>
	
		<!-- spring dependency-->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-instrument</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-webmvc</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
		
		
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		
		<!-- fastjson dependencies -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>${fastjson.version}</version>
		</dependency>
		
		<!-- mybatis-->
		<dependency>
		    <groupId>org.mybatis</groupId>
    		<artifactId>mybatis</artifactId>
		    <version>${mybatis.version}</version>
	    </dependency>
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>${mybatis.spring.version}</version>
	    </dependency>
	    <dependency>
		    <groupId>org.mybatis.generator</groupId>
		    <artifactId>mybatis-generator-core</artifactId>
		    <version>${mybatis.generator.version}</version>
		    <scope>provided</scope>
		</dependency>
		<!-- Mybatis paging plugin usage see: https://pagehelper.github.io/docs/howtouse/-->
		<dependency>
		    <groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper</artifactId>
		    <version>${pagehelper.version}</version>
		</dependency>
	    
		
		<!-- druid data connection pool -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		
		
		<!-- jsp page dependency package-->
		<dependency>
			<groupId>javax.el</groupId>
			<artifactId>javax.el-api</artifactId>
			<version>${el.version}</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${standard.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.api.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${javax.servlet.api.version}</version>
			<scope>provided</scope>
		</dependency>
				
		
		<dependency>
	    	<groupId>junit</groupId>
	      	<artifactId>junit</artifactId>
	      	<version>${junit.version}</version>
	      	<scope>test</scope>
		</dependency>
		
		
	</dependencies>
  
  
  <build>
    <finalName>ssm</finalName>
    
    <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>  
            </configuration>  
         </plugin>
    </plugins>
    
  </build>
</project>

 

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

Sweet Potato YaoMarch 14, 2018

http://fanshuyao.iteye.com/

 

Guess you like

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