首页 > 编程语言 > 详细

Spring+SpringMVC+Mybatis整合

时间:2016-03-17 08:24:39      阅读:266      评论:0      收藏:0      [点我收藏+]

Spring4.1.6+SpringMVC+Mybatis3.3.0整合 MySQL5.*+文件上传

1.所需jar与解释

Spring+SpringMVC:
        spring-aop-4.1.6.RELEASE.jar
        spring-aspects-4.1.6.RELEASE.jar
        spring-beans-4.1.6.RELEASE.jar
        spring-context-4.1.6.RELEASE.jar
        spring-core-4.1.6.RELEASE.jar
        spring-expression-4.1.6.RELEASE.jar
        spring-jdbc-4.1.6.RELEASE.jar
        spring-orm-4.1.6.RELEASE.jar
        spring-tx-4.1.6.RELEASE.jar
        spring-web-4.1.6.RELEASE.jar
        spring-webmvc-4.1.6.RELEASE.jar
        
        AOP:在Spring2.*中
            aopalliance.jar
            aspectjweaver.jar

        文件上传:在Struts2中
            commons-fileupload-1.2.1.jar
            commons-io-1.3.2.jar

    Mybatis:
        mybatis-3.3.0.jar
        asm-4.2.jar
        cglib-3.1.jar
        commons-logging-1.2.jar
        log4j-1.2.17.jar
        log4j-api-2.2.jar
        log4j-core-2.2.jar
        slf4j-api-1.7.12.jar
        slf4j-log4j12-1.7.12.jar

    Mybatis与Spring整合:
        mybatis-spring-1.2.3.jar

    MySQL驱动包:
        mysql-connector-java-5.1.7-bin.jar

2.编写配置文件

  (1)web.xml ——Web容器

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3     <display-name>07spring+springmvc+mybatis</display-name>
 4 
 5     <!-- spring 配置 begin-->
 6     <context-param>
 7         <param-name>contextConfigLocation</param-name>
 8         <param-value>classpath:applicationContext.xml</param-value>
 9     </context-param>
10     <listener>
11         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12     </listener>
13     <!-- spring 配置 end-->
14     
15     <!-- spring mvc 配置 begin -->
16     <servlet>
17         <servlet-name>springmvc</servlet-name>
18         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
19         <init-param>
20             <param-name>contextConfigLocation</param-name>
21             <param-value>classpath:mvc.xml</param-value>
22         </init-param>
23         <load-on-startup>1</load-on-startup>
24     </servlet>
25     <servlet-mapping>
26         <servlet-name>springmvc</servlet-name>
27         <url-pattern>*.do</url-pattern>
28     </servlet-mapping>
29     <!-- spring mvc 配置 end -->
30 
31 </web-app>
web.xml

  (2)mvc.xml ——SpringMVC

技术分享
 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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 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 
15     <!-- 配置文件上传 begin -->
16     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
17         <property name="defaultEncoding" value="utf-8"/>
18         <property name="maxUploadSize" value="10485760000"/>
19         <property name="maxInMemorySize" value="40960"/>
20     </bean>
21     <!-- 配置文件上传 end -->
22     
23     <!-- 扫描该包中的所有注解 -->
24     <context:component-scan base-package="com.wd.controller"/>
25 </beans>
mvc.xml

  (3)applicationContext.xml ——Spring

技术分享
 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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     
 9     xsi:schemaLocation="
10         http://www.springframework.org/schema/beans
11         http://www.springframework.org/schema/beans/spring-beans.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd 
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx.xsd ">
18     <!-- 配置datasource begin -->
19     <!-- 读取数据库配置文件 -->
20     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
21         <property name="location" value="classpath:db.properties"/>
22     </bean>
23     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
24         <property name="driverClassName" value="${driver}"/>
25         <property name="url" value="${url}"/>
26         <property name="username" value="${username}"/>
27         <property name="password" value="${password}"/>
28     </bean>
29     <!-- 配置datasource end -->
30 
31     <!-- 配置SqlSessionFactory begin -->
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
33         <property name="dataSource" ref="dataSource"/>
34         <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
35     </bean>
36     <!-- 配置SqlSessionFactory end -->
37     
38     <!-- 配置事物 声明式事物 begin -->
39     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
40         <property name="dataSource" ref="dataSource"/>
41     </bean>
42     <tx:advice id="txAdvice" transaction-manager="txManager">
43         <!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
44         <!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
45         <tx:attributes>
46             <tx:method name="add*" propagation="REQUIRED"/>
47             <tx:method name="delete*" propagation="REQUIRED"/>
48             <tx:method name="update*" propagation="REQUIRED"/>
49             <tx:method name="*" read-only="true"/>
50         </tx:attributes>
51     </tx:advice>
52     <aop:config>
53         <aop:pointcut id="pointcut" expression="execution(* com.wd.service.*.*(..))" />
54         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
55     </aop:config>
56     <!-- 配置事物 声明式事物 end -->
57     
58     <!-- 配置注解 begin -->
59     <context:component-scan base-package="com.wd"/>    
60     <!-- 配置注解 end -->
61 </beans>
applicationContext.xml

  (4)mybatis.cfg.xml ——Mybatis

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <typeAliases>
 6         <package name="com.wd.entity"/>
 7     </typeAliases>
 8     <mappers>
 9         <!-- 所有mapper文件填写位置 -->
10         <mapper resource="com/wd/entity/book.mapper.xml"/>
11         <mapper resource="com/wd/entity/computer.mapper.xml"/>
12     </mappers>
13 </configuration>
mybatis.cfg.xml

  (5)db.properties ——数据源

技术分享
1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3307/wdmall
3 username=root
4 password=950906
db.properties

 

Spring+SpringMVC+Mybatis整合

原文:http://www.cnblogs.com/zhengbin/p/5285969.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!