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

SpringMVC与Mybatis整合

时间:2019-01-20 00:10:20      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:必须   srping   视图解析器   class   int   conf   映射   前端   service   

 

1.jar包

 Spring的jar包;Mybatis的jar包;Mybatis与Spring整合包;数据库连接包;第三方连接池包。

2.需要配置的文件

SqlMapConfig.xml:Mybatis核心配置文件(主要交给Spring管理,并不需要什么配置,但必须存在)
db.properties:数据库连接配置,主要配置用户密码
applicationContext*.xml:Spring的核心配置,配置数据源、连接池、mapper扫描包位置、事务、service扫描。
(*表示可以将事务单独成一个配置,例如:applicationContext-dao.xml)
springmvc.xml:配置扫描注解包(可包括Spring配置中的service扫描包)、注解驱动(处理器的适配器和映射器配置)、视图解析器、自定义转换器
log4j.properties:日志配置
web.xml:前端配置

3.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 </configuration>

4.db.properties

技术分享图片

5.applicationContext*.xml

  applicationContext-dao.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8     xsi:schemaLocation="
 9     http://www.springframework.org/schema/beans 
10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
11     http://www.springframework.org/schema/context 
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/aop 
14     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
15     http://www.springframework.org/schema/tx 
16     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17     http://www.springframework.org/schema/util 
18     http://www.springframework.org/schema/util/spring-util-4.0.xsd">
19 
20    <!-- 加载配置文件 -->
21    <context:property-placeholder location="classpath:db.properties" />
22 
23     <!-- 数据库连接池 -->
24     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
25         destroy-method="close">
26         <property name="driverClassName" value="${jdbc.driver}" />
27         <property name="url" value="${jdbc.url}" />
28         <property name="username" value="${jdbc.username}" />
29         <property name="password" value="${jdbc.password}" />
30         <property name="maxActive" value="10" />
31         <property name="maxIdle" value="5" />
32     </bean>
33 
34     <!-- 配置SqlSessionFactory -->
35     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
36         <!-- 配置mybatis核心配置文件 -->
37         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
38         <!-- 配置数据源 -->
39         <property name="dataSource" ref="dataSource" />
40     </bean>
41     
42 
43     <!-- 配置mapper扫描设置 -->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45     <!-- 只需要设置基本包路径 -->
46         <property name="basePackage" value="com.springmvc.dao"></property>
47     </bean>
48 
49 </beans>

classpath指定是文件路径。建议将约束都导入,方便配置。

  applicationContext-trans.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8     xsi:schemaLocation="
 9     http://www.springframework.org/schema/beans 
10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
11     http://www.springframework.org/schema/context 
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/aop 
14     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
15     http://www.springframework.org/schema/tx 
16     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17     http://www.springframework.org/schema/util 
18     http://www.springframework.org/schema/util/spring-util-4.0.xsd">
19 
20     <!-- 事务管理器 -->
21     <bean id="transactionManager"
22         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
23         <!-- 数据源 -->
24         <property name="dataSource" ref="dataSource" />
25     </bean>
26 
27     <!-- 通知 -->
28     <tx:advice id="txAdvice" transaction-manager="transactionManager">
29         <tx:attributes>
30             <!-- 传播行为 -->
31             <tx:method name="save*" propagation="REQUIRED" />
32             <tx:method name="insert*" propagation="REQUIRED" />
33             <tx:method name="delete*" propagation="REQUIRED" />
34             <tx:method name="update*" propagation="REQUIRED" />
35             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
36             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
37             <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
38         </tx:attributes>
39     </tx:advice>
40 
41     <!-- 切面 -->
42     <aop:config>
43         <aop:advisor advice-ref="txAdvice"
44             pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
45     </aop:config>
46     
47     <
48 
49 
50 </beans>

上面演示的是采用配置式,下面采用注解式:

1     <bean id="transactionManager" class=""org.springframework.jdbc.datasource.DataSourceTransactionManager>
2         <property name="dataSource" ref="dataSource"></property>
3     </bean>
4     <tx:annotation-driven transaction-manager="transactionManager"/>

6.springmvc.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:p="http://www.springframework.org/schema/p"
 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-4.0.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9 
10         
11         <!-- 配置扫描注解 -->
12         <context:component-scan base-package="com.springmvc"/>
13         <!-- 注解驱动 -->
14         <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
15         
16         <!-- 配置转换器 -->
17         <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
18             <property name="converters">
19                 <set>
20                     <bean class="com.springmvc.converter.DateConverter" />
21                 </set>
22             </property>
23 
24         </bean>
25         
26         <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> 
27         "/WEB-INF/jsp/test.jsp" -->
28         <!-- 配置视图解析器 -->
29         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
30             <!-- 配置逻辑视图的前缀 -->
31             <property name="prefix" value="/WEB-INF/jsp/" />
32             <!-- 配置逻辑视图的后缀 -->
33             <property name="suffix" value=".jsp" />
34         </bean>
35         
36         
37 </beans>

7.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>springmvc-mybatis</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13     <!-- 配置spring -->
14     <context-param>
15         <param-name>contextConfigLocation</param-name>
16         <param-value>classpath:spring/applicationContext*.xml</param-value>
17     </context-param>
18 
19     <!-- 使用监听器加载Spring配置文件 -->
20     <listener>
21         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22     </listener>
23 
24     <!-- 配置SrpingMVC的前端控制器 -->
25     <servlet>
26         <servlet-name>springmvc-web</servlet-name>
27         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
28         <init-param>
29             <param-name>contextConfigLocation</param-name>
30             <param-value>classpath:spring/springmvc.xml</param-value>
31         </init-param>
32     </servlet>
33 
34     <servlet-mapping>
35         <servlet-name>springmvc-web</servlet-name>
36         <!-- 配置所有以action结尾的请求进入SpringMVC -->
37         <url-pattern>*.action</url-pattern>
38     </servlet-mapping>
39 
40 
41     <!-- 解决post乱码问题 -->
42     <filter>
43         <filter-name>encoding</filter-name>
44         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
45         <!-- 设置编码参是UTF8 -->
46         <init-param>
47             <param-name>encoding</param-name>
48             <param-value>UTF-8</param-value>
49         </init-param>
50     </filter>
51     <filter-mapping>
52         <filter-name>encoding</filter-name>
53         <url-pattern>/*</url-pattern>
54     </filter-mapping>
55 
56   
57 </web-app>

 

SpringMVC与Mybatis整合

标签:必须   srping   视图解析器   class   int   conf   映射   前端   service   

原文地址:https://www.cnblogs.com/lisd/p/10293652.html

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