首页 > 代码库 > spring+springMvc+mybatis整合
spring+springMvc+mybatis整合
项目中有用到spring整合springMvc和mybatis的开发框架,现以下面的例子进行说明:
开发框架搭建如下:
1、建web项目(注意:这里例子没用到maven管理)
2、引入依赖的jar包(spring、springMVC、mybatis以及其他相关依赖jar包)
3、配置整合文件
1)web.xml配置(配置spring支持、配置SpringMVC支持)
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>CRM</display-name> 4 <welcome-file-list> 5 <welcome-file>login.jsp</welcome-file> 6 </welcome-file-list> 7 8 <!-- Spring配置文件 --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:applicationContext.xml</param-value> 12 </context-param> 13 <!-- 编码过滤器 --> 14 <filter> 15 <filter-name>encodingFilter</filter-name> 16 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 17 <async-supported>true</async-supported> 18 <init-param> 19 <param-name>encoding</param-name> 20 <param-value>UTF-8</param-value> 21 </init-param> 22 </filter> 23 <filter-mapping> 24 <filter-name>encodingFilter</filter-name> 25 <url-pattern>/*</url-pattern> 26 </filter-mapping> 27 <!-- Spring监听器 --> 28 <listener> 29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 30 </listener> 31 32 <!-- 添加对springmvc的支持 --> 33 <servlet> 34 <servlet-name>springMVC</servlet-name> 35 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 36 <init-param> 37 <param-name>contextConfigLocation</param-name> 38 <param-value>classpath:spring-mvc.xml</param-value> 39 </init-param> 40 <load-on-startup>1</load-on-startup> 41 <async-supported>true</async-supported> 42 </servlet> 43 <servlet-mapping> 44 <servlet-name>springMVC</servlet-name> 45 <url-pattern>*.do</url-pattern> 46 </servlet-mapping> 47 </web-app>
2)spring配置 (配置applicationContext.xml、配置加载mybatis文件)
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:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jee="http://www.springframework.org/schema/jee" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 13 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 15 16 <!-- 自动扫描 --> 17 <context:component-scan base-package="com.hik.service" /> 18 19 <!-- 配置数据源 --> 20 <bean id="dataSource" 21 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 22 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 23 <property name="url" value="jdbc:mysql://localhost:3306/db_crm"/> 24 <property name="username" value="root"/> 25 <property name="password" value="passwd"/> 26 </bean> 27 28 <!-- 配置mybatis的sqlSessionFactory --> 29 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource" /> 31 <!-- 自动扫描mappers.xml文件 --> 32 <property name="mapperLocations" value="classpath:com/hik/mappers/*.xml"></property> 33 <!-- mybatis配置文件 --> 34 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 35 </bean> 36 37 <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 38 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 39 <property name="basePackage" value="com.hik.dao" /> 40 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 41 </bean> 42 43 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 44 <bean id="transactionManager" 45 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 46 <property name="dataSource" ref="dataSource" /> 47 </bean> 48 49 <!-- 配置事务通知属性 --> 50 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 51 <!-- 定义事务传播属性 --> 52 <tx:attributes> 53 <tx:method name="insert*" propagation="REQUIRED" /> 54 <tx:method name="update*" propagation="REQUIRED" /> 55 <tx:method name="edit*" propagation="REQUIRED" /> 56 <tx:method name="save*" propagation="REQUIRED" /> 57 <tx:method name="add*" propagation="REQUIRED" /> 58 <tx:method name="new*" propagation="REQUIRED" /> 59 <tx:method name="set*" propagation="REQUIRED" /> 60 <tx:method name="remove*" propagation="REQUIRED" /> 61 <tx:method name="delete*" propagation="REQUIRED" /> 62 <tx:method name="change*" propagation="REQUIRED" /> 63 <tx:method name="check*" propagation="REQUIRED" /> 64 <tx:method name="get*" propagation="REQUIRED" read-only="true" /> 65 <tx:method name="find*" propagation="REQUIRED" read-only="true" /> 66 <tx:method name="load*" propagation="REQUIRED" read-only="true" /> 67 <tx:method name="*" propagation="REQUIRED" read-only="true" /> 68 </tx:attributes> 69 </tx:advice> 70 71 <!-- 配置事务切面 --> 72 <aop:config> 73 <aop:pointcut id="serviceOperation" 74 expression="execution(* com.hik.service.*.*(..))" /> 75 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> 76 </aop:config> 77 78 79 80 </beans>
3)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:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jee="http://www.springframework.org/schema/jee" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 13 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 15 16 <!-- 使用注解的包,包括子集 --> 17 <context:component-scan base-package="com.hik.controller" /> 18 19 <!-- 视图解析器 --> 20 <bean id="viewResolver" 21 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/" /> 23 <property name="suffix" value=".jsp"></property> 24 </bean> 25 26 </beans>
4)mybatis配置
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 <package name="com.hik.entity"/> 9 </typeAliases> 10 </configuration>
4、建立包和相关类(controller、service、dao、entity)
5、创建数据库及表
创建db_crm数据库、表t_user
6、部署web项目测试,没有报错则加载成功。
spring+springMvc+mybatis整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。