码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC + Spring + MyBatis 整合

时间:2016-01-27 18:58:30      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

整理下SSM(基于注解)的整合

1. web.xml 配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 6     
 7     <!-- log4j 配置 -->
 8         <context-param>
 9         <param-name>log4jConfigLocation</param-name>
10         <param-value>classpath:log4j.properties</param-value>
11         </context-param>
12         <context-param>
13         <param-name>log4jRefreshInterval</param-name>
14         <param-value>60</param-value>
15         </context-param>
16         <listener>
17         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
18         </listener>
19     
20     <!--  设置编码 UTF-8  -->
21     <filter>
22         <filter-name>encodingFilter</filter-name>
23         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
24         <init-param>
25             <param-name>encoding</param-name>
26             <param-value>UTF-8</param-value>
27         </init-param>
28         <init-param>
29             <param-name>forceEncoding</param-name>
30             <param-value>true</param-value>
31         </init-param>
32     </filter>
33     <filter-mapping>
34         <filter-name>encodingFilter</filter-name>
35         <url-pattern>/*</url-pattern>
36     </filter-mapping>  
37     
38     <!-- 配置 Spring -->
39     <context-param>
40         <param-name>contextConfigLocation</param-name>
41         <param-value>classpath*:/applicationContext.xml</param-value>
42     </context-param>
43     <listener>
44         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
45     </listener>
46     <!-- 配置springmvc -->
47     <servlet>
48         <servlet-name>springmvc</servlet-name>
49         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
50         <init-param>
51             <param-name>contextConfigLocation</param-name>
52             <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
53         </init-param>
54          <load-on-startup>1</load-on-startup>
55     </servlet>
56     <servlet-mapping>
57         <servlet-name>springmvc</servlet-name>
58         <url-pattern>/</url-pattern>
59     </servlet-mapping>
60   <welcome-file-list>
61     <welcome-file>index.html</welcome-file>
62   </welcome-file-list>
63 </web-app>

2 :log4j.properties

 1 #logger level default is INFO
 2 log4j.rootLogger=debug,console,file
 3  
 4 #append to console
 5 log4j.appender.console=org.apache.log4j.ConsoleAppender
 6 log4j.appender.console.layout=org.apache.log4j.PatternLayout
 7 log4j.appender.console.layout.ConversionPattern=%d %p - %m%n
 8  
 9 #append to file
10 log4j.appender.file=org.apache.log4j.RollingFileAppender
11 log4j.appender.file.File=/demo.log
12 log4j.appender.file.MaxFileSize=1MB
13 log4j.appender.file.MaxBackupIndex=10000
14 log4j.appender.file.layout=org.apache.log4j.PatternLayout
15 log4j.appender.file.layout.ConversionPattern=%d %p - %m%n
16  
17  
18 #ibatis logger config
19 log4j.logger.com.ibatis=debug
20 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
21 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
22 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
23 log4j.logger.java.sql.Connection=debug
24 log4j.logger.java.sql.Statement=debug
25 log4j.logger.java.sql.PreparedStatement=debug,stdout

3 :applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 8             http://www.springframework.org/schema/mvc 
 9             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
10             http://www.springframework.org/schema/context 
11             http://www.springframework.org/schema/context/spring-context-3.0.xsd 
12             http://www.springframework.org/schema/aop 
13             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
14             http://www.springframework.org/schema/tx 
15             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
16     <!-- 扫描service、dao组件 -->
17     <context:component-scan base-package="com.pro.*" />
18     <!-- 分解配置 jdbc.properites -->
19     <context:property-placeholder location="classpath:jdbc.properties" />
20     <!-- 数据源c3p0  ... -->
21     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
22         <property name="driverClass" value="${jdbc.driverClassName}" />
23         <property name="jdbcUrl" value="${jdbc.url}" />
24         <property name="user" value="${jdbc.username}" />
25         <property name="password" value="${jdbc.password}" />
26         <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
27         <property name="minPoolSize" value="${c3p0.pool.size.min}" />
28         <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
29         <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
30     </bean>
31     <!-- sessionFactory 将spring和mybatis整合 -->
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <property name="dataSource" ref="dataSource" />
34         <property name="configLocation" value="classpath:/mapper/sqlMapConfig.xml" />
35         <property name="mapperLocations" value="classpath:/mapper/**/*Mapper.xml" />    <!-- 加载mapper文件 -->
36     </bean>
37     
38     <!--       -->
39     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
40         <constructor-arg index="0" ref="sqlSessionFactory" /> 
41         <constructor-arg index="1" value="REUSE" />
42     </bean>
43      <!--   这个baseDao 主要是一些封装的方法,进行数据的交互    -->
44     <bean id="baseDao" class="com.pro.common.BaseDao"/>
45   
46     
47     <!-- 事务 -->
48     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
49         <property name="dataSource" ref="dataSource" />
50     </bean>
51     <tx:advice id="txAdvice" transaction-manager="txManager">
52         <tx:attributes>
53             <tx:method name="insert*" propagation="REQUIRED" />
54             <tx:method name="update*" propagation="REQUIRED" />
55             <tx:method name="delete*" propagation="REQUIRED" />
56             <tx:method name="find" read-only="true" />
57             <tx:method name="get" read-only="true" />
58             <tx:method name="*" propagation="REQUIRED" />
59         </tx:attributes>
60     </tx:advice>
61     <aop:config>
62         <aop:pointcut  id="pointCut"  expression="execution(* com.pro.*.*(..))"/>
63         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
64     </aop:config>
65     
66    <!-- 扫描service下的所有类,自动为spring容器管理 -->  
67     <context:component-scan base-package="com.pro.*.*.service.*"/>  
68      <!-- 打开面向切面工具 -->  
69     <aop:aspectj-autoproxy/>   
70 </beans>            

4 : 接下来 是springmvc-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" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 8     http://www.springframework.org/schema/mvc 
 9     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
10     http://www.springframework.org/schema/context 
11     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
12     http://www.springframework.org/schema/aop 
13     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
14     http://www.springframework.org/schema/tx 
15     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
16     <!-- 注解方式 -->
17     <mvc:annotation-driven />
18     <!-- 自动扫描Controller -->
19     <context:component-scan base-package="com.pro.*" />
20     <!-- 配置一个springmvc框架的视图解析器 -->
21 
22     <!--          一下暂时注释 ,这个主要是返回到 html 或JSP,ftl 等  如果采用这个 就想下一个配置的去掉就好 -->
23   <!-- 
24   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25 <  !- - 通过setter方法注入前缀 /WEB-INF/jsps/showMsg.jsp-  ->
26         <property name="prefix" value="/WEB-INF/jsps/"   />
27         <!- - 通过setter方法注入后缀 -  ->
28       <  property name="suffix" value=".jsp" />
29     </bean>
30     --> 
31 
32     <!--   配置这个主要是最近项目采用jQueryEasy 返回json数据 ,如果采用这个就讲上面的去掉就好    -->
33     
34     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
35         <property name="messageConverters">  
36             <list>  
37                 <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
38                      <property name="supportedMediaTypes" value="application/json" />
39                 </bean>  
40             </list>  
41         </property>  
42     </bean>
43       <!-- 错误页面 --> 
44     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
45         <property name="exceptionMappings">  
46             <props>   
47                 <prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>  
48             </props>  
49         </property>  
50     </bean>  
51 
52 
53 
54 
55     <!-- 支持文件上传 -->
56     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
57 <!-- 设置上传文件的最大尺寸为50MB -->
58 <property name="maxUploadSize">  
59    <value>52428800</value>  
60         </property>     
61     </bean>
62 </beans>

5:sqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 定义别名 -->
 7     <typeAliases>
 8         <typeAlias alias="SysUser"  type="com.pro.system.entity.SysUser" />  
 9     </typeAliases>
10     <!-- 这里不需要再注册mybatis的SQL映射文件 ,因为在Spring的配置文件中配置sqlSessionFactory时注册了mapper的路径-->
11 </configuration>     

6 :jdbc.properties

1 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
2 jdbc.url=jdbc:oracle:thin:@localhost:1521:dbname
3 jdbc.username=usename
4 jdbc.password=password
5 c3p0.pool.size.max=20
6 c3p0.pool.size.min=5
7 c3p0.pool.size.ini=3
8 c3p0.pool.size.increment=2 

包结构 :

技术分享

QQ:871820604

springMVC + Spring + MyBatis 整合

标签:

原文地址:http://www.cnblogs.com/hjwbla/p/5163858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!