Spring+SpringMVC+mybatis框架整合

1、jdbc.properties

 1 driverClassName=com.mysql.jdbc.Driver
 2 url=jdbc\:mysql\://127.0.0.1\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
 3 uname=root
 4 password=12345
 5 minIdle=45
 6 maxIdle=50
 7 initialSize=5
 8 maxActive=100
 9 maxWait=100
10 removeAbandonedTimeout=180
11 removeAbandoned=true

2、application-mybatis.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:p="http://www.springframework.org/schema/p"  
 6             xmlns:tx="http://www.springframework.org/schema/tx"  
 7             xmlns:context="http://www.springframework.org/schema/context"  
 8             xsi:schemaLocation="   
 9                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
10                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
11                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
12                 http://www.springframework.org/schema/context 
13                 http://www.springframework.org/schema/context/spring-context.xsd">  
14       
15       <!-- 读取JDBC的配置文件 -->
16       <context:property-placeholder location="classpath:jdbc.properties"/>
17       
18       <!-- JNDI获取数据源(dbcp连接池) -->
19       <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
20           <property name="driverClassName" value="${driverClassName}"/>
21           <property name="url" value="${url}"/>
22           <property name="username" value="${uname}"/>
23           <property name="password" value="${password}"/>
24           <property name="initialSize" value="${initialSize}"/>
25           <property name="maxActive" value="${maxActive}"/>
26           <property name="maxIdle" value="${maxIdle}"/>
27           <property name="minIdle" value="${minIdle}"/>
28           <property name="maxWait" value="${maxWait}"/>
29           <!-- 当前空闲连接数< 2 && (当前活动数>最大活动数-3) -->
30           <property name="removeAbandoned" value="${removeAbandoned}"/>
31           
32           <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
33           <!-- sql心跳 :保证连接池中连接是真实有效的连接-->
34           <!-- testWhileIdle testOnBorrow testOnReturn 
35           validationQuery-select 1
36            -->
37           <!-- 开启Evict的定时校验,循环校验 -->
38           <property name="testWhileIdle" value="true"/>
39           <!-- 定义Evict的时间间隔,单位:毫秒,大于0才会开启evict -->
40           <property name="timeBetweenEvictionRunsMillis" value="60000"/>
41           <!-- 在进行borrowObject处理时,会对拿到的连接进行校验-false不校验 -->
42           <property name="testOnBorrow" value="false"/>
43           <!-- 在进行returnObject处理时,会对返回的连接进行校验-false不校验 -->
44           <property name="testOnReturn" value="false"/>
45           <!-- 校验使用的sql语句,validationQuery,复杂的校验sql会影响性能 -->
46           <property name="validationQuery" value="select 1"/>
47           <!-- 配置每次校验连接的数量,一般等于maxActive -->
48           <property name="numTestsPerEvictionRun" value="${maxActive}"/>
49       </bean> 
50       
51       <!-- 事务管理 -->
52       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
53           <property name="dataSource" ref="dataSource"/>
54       </bean>
55       <!-- 配置mybatis sqlSessionFactoryBean -->
56       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
57           <property name="dataSource" ref="dataSource"/>
58           <property name="configLocation" value="classpath:mybatis-config.xml"/>
59       </bean>
60       <!-- AOP事务处理  -->
61       <aop:aspectj-autoproxy/>
62       <aop:config proxy-target-class="true">
63           <aop:pointcut expression="execution(* *org.slsale.service..*(..))" id="transService"/>
64           <aop:advisor advice-ref="txAdvice" pointcut-ref="transService"/>
65       </aop:config>
66       <!-- txAdvice:需要定义事务管理器,进行事务处理 -->
67       <tx:advice id="txAdvice"  transaction-manager="transactionManager">
68           <tx:attributes>
69               <tx:method name="hl*" propagation="REQUIRED" rollback-for="Exception"/>
70           </tx:attributes>
71       </tx:advice>
72       
73       <!-- mapper 接口所在包名,spring会自动查找其下的Mapper -->
74       <bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer">
75           <property name="basePackage" value="org.slsale.dao"/>
76       </bean>
77       
78       <!-- redis配置 -->
79       
80 </beans>

3、log4j.properties

 1 log4j.rootLogger=debug,CONSOLE,file
 2 #log4j.rootLogger=ERROR,ROLLING_FILE
 3 
 4 log4j.logger.org.slsale=debug
 5 log4j.logger.org.apache.ibatis=debug
 6 log4j.logger.org.mybatis.spring=debug
 7 log4j.logger.java.sql.Connection=debug
 8 log4j.logger.java.sql.Statement=debug
 9 log4j.logger.java.sql.PreparedStatement=debug
10 log4j.logger.java.sql.ResultSet=debug
11 
12 ######################################################################################
13 # Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
14 ######################################################################################
15 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
16 log4j.appender.Threshold=debug
17 log4j.appender.CONSOLE.DatePattern=yyyy-MM-dd
18 log4j.appender.CONSOLE.Target=System.out
19 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
20 log4j.appender.CONSOLE.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
21 
22 ######################################################################################
23 # Rolling File  \u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6
24 ######################################################################################
25 #log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
26 #log4j.appender.ROLLING_FILE.Threshold=INFO
27 #log4j.appender.ROLLING_FILE.File=${baojia.root}/logs/log.log
28 #log4j.appender.ROLLING_FILE.Append=true
29 #log4j.appender.ROLLING_FILE.MaxFileSize=5000KB
30 #log4j.appender.ROLLING_FILE.MaxBackupIndex=100
31 #log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
32 #log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
33 
34 ######################################################################################
35 # DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
36 ######################################################################################
37 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
38 log4j.appender.file.DatePattern=yyyy-MM-dd
39 log4j.appender.file.File=${SLSaleSystem.root}/logs/log.log
40 log4j.appender.file.Append=true
41 log4j.appender.file.Threshold=debug
42 log4j.appender.file.layout=org.apache.log4j.PatternLayout
43 log4j.appender.file.layout.ConversionPattern= - (%r ms) - %d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
44 
45 #DWR \u65e5\u5fd7
46 #log4j.logger.org.directwebremoting = ERROR
47 
48 #\u663e\u793aHibernate\u5360\u4f4d\u7b26\u7ed1\u5b9a\u503c\u53ca\u8fd4\u56de\u503c
49 #log4j.logger.org.hibernate.type=DEBUG,CONSOLE 
50 
51 #log4j.logger.org.springframework.transaction=DEBUG
52 #log4j.logger.org.hibernate=DEBUG
53 #log4j.logger.org.acegisecurity=DEBUG
54 #log4j.logger.org.apache.myfaces=TRACE
55 #log4j.logger.org.quartz=DEBUG
56 
57 #log4j.logger.com.opensymphony=INFO  
58 #log4j.logger.org.apache.struts2=DEBUG  
59 log4j.logger.com.opensymphony.xwork2=debug

4、mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="lazyLoadingEnabled" value="false" />
    </settings>
    <typeAliases>
        <!-- 实体类取别名,方便在mapper中使用 -->
        <package name="org.slsale.pojo"/>
    </typeAliases>

</configuration>

5、spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14      <!-- 以 annotation的方式,装配controller-->
15      <mvc:annotation-driven/>
16      <!-- spring扫描包下所有类,让标注spring注解的类生效 -->
17      <context:component-scan base-package="org.slsale"/>
18      
19      
20      <!-- 视图的对应 -->
21      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
22          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
23          <property name="prefix" value="/WEB-INF/pages/"/>
24          <property name="suffix" value=".jsp"/>
25      </bean>   
26      <!-- 静态文件映射 -->
27      <mvc:resources location="/statics/" mapping="/statics/**"/> 
28      
29      <!-- 配置文件上传 -->      
30      <bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
31          <property name="maxUploadSize" value="5000000"/>
32      </bean>
33      
34      <!-- 配置interceptors -->
35      
36 </beans>

6、导jar包

猜你喜欢

转载自www.cnblogs.com/yutianbao/p/9162012.html