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

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

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

蕃薯耀 2018年3月14日

http://fanshuyao.iteye.com/

一、spring.xml配置文件

<?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配置数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
	     <!-- 基本属性 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}" />
	
	     <!-- 配置监控统计拦截的filters -->
	     <property name="filters" value="stat" />
	
	     <!-- 配置初始化大小、最小、最大 -->
	     <property name="maxActive" value="20" />
	     <property name="initialSize" value="1" />
	     <property name="minIdle" value="1" />
	
	     <!-- 配置获取连接等待超时的时间 -->
	     <property name="maxWait" value="60000" />     
	
	     <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
	     <property name="timeBetweenEvictionRunsMillis" value="60000" />
	
	     <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
	     <property name="minEvictableIdleTimeMillis" value="300000" />
	
	     <property name="testWhileIdle" value="true" />
	     <property name="testOnBorrow" value="false" />
	     <property name="testOnReturn" value="false" />
	
	     <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
	     <property name="poolPreparedStatements" value="true" />
	     <property name="maxOpenPreparedStatements" value="20" />
	 </bean>
	 
	 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
	 	<!-- 指定mybatis全局配置文件,已经不需要 -->
	 	<!-- <property name="configLocation" value="classpath:mybatis.xml"></property> -->
	 	<property name="dataSource" ref="dataSource"></property>
	 	<!-- 指定Mybatis mapper文件的位置 -->
	 	<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	 	<!-- <property name="typeAliasesPackage" value="com.lqy.ssm.bean"></property> -->
	 	<property name="plugins">
	        <array>
	        	<!-- 配置pagehelper分页插件,使用见: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>
	 
	 <!-- 配置一个可以批量执行的sqlSession -->
	 <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>
	 
	 <!-- 配置扫描器,将Mybatis接口的实现加入到ioc容器 -->
	 <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<!-- 扫描所有dao接口的实现 -->
	 	<property name="basePackage" value="com.lqy.ssm.dao"></property>
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
	 </bean>
	
	
	<!-- 配置事务管理器 -->
	<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"> -->
	<!-- 通过AOP配置提供事务增强,事务如何切入 -->	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 让service包下所有Bean的所有方法拥有事务 -->
			<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">
		<!-- 切点表达式 -->
		<aop:pointcut id="serviceMethod" expression=" execution(* com.lqy.ssm.service..*(..))" />
		<!-- 引用到事务增强 -->
		<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
	</aop:config>
	

</beans>

二、jdbc.properties文件

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

三、springMvc.xml文件

<?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设置为false关闭默认扫描 -->
	<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>
	
	<!-- 视图解析器 -->
	<mvc:view-resolvers>
		<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/>
	</mvc:view-resolvers>
	
	<!-- 支持对象与json的转换 -->
	<!-- <mvc:annotation-driven /> -->
	<mvc:annotation-driven>
		<!-- conversion-service="conversionService" -->
		<mvc:message-converters>
			<!-- UTF-8字符格式化 -->
			<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>

			<!-- 处理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>
	
	<!-- 将springMvc不能处理的请求交给tomcat -->
	<mvc:default-servlet-handler/>
	
	<!-- 配置文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
			<value>32505856</value>		<!-- 上传文件大小限制为31M,31*1024*1024 -->
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
	
	
	

</beans>

四、pom.xml文件

<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>
			<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依赖包 -->
		<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分页插件 使用见:https://pagehelper.github.io/docs/howtouse/-->
		<dependency>
		    <groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper</artifactId>
		    <version>${pagehelper.version}</version>
		</dependency>
	    
		
		<!-- druid数据连接池 -->
		<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页面依赖包 -->
		<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>

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

蕃薯耀 2018年3月14日

http://fanshuyao.iteye.com/

猜你喜欢

转载自fanshuyao.iteye.com/blog/2413040