springmvc和mybatis整合关键配置
springmvc+mybaits的系统架构:
第一步:整合dao层
mybatis和spring整合,通过spring管理mapper接口。
使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二步:整合service层
通过spring管理 service接口。
使用配置方式将service接口配置在spring配置文件中。
实现事务控制。
第三步:整合springmvc
由于springmvc是spring的模块,不需要整合。
所需要的jar包:
数据库驱动包:mysql5.1
mybatis的jar包
mybatis和spring整合包
log4j包
dbcp数据库连接池包
spring3.2所有jar包
jstl包
mybatis配置文件 sqlMapConfig.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><!-- 全局setting配置,根据需要添加 --><!-- 配置别名 --><typeAliases><!-- 批量扫描别名 --><package name="cn.itcast.ssm.po"/></typeAliases><!-- 配置mapper由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。必须遵循:mapper.xml和mapper.java文件同名且在一个目录 --><!-- <mappers></mappers> -->
</configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=
applicationContext-dao.xml
配置:
数据源
SqlSessionFactory
mapper扫描器
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 --><context:property-placeholder location="classpath:db.properties" /><!-- 配置数据源 ,dbcp --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="30" /><property name="maxIdle" value="5" /></bean><!-- sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /></bean><!-- mapper扫描器 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 --><property name="basePackage" value="cn.itcast.ssm.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean></beans>
mybatis mapper接口映射文件 ItemsMapperCustom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.itcast.ssm.mapper.ItemsMapperCustom" ><!-- 定义商品查询的sql片段,就是商品查询条件 --><sql id="query_items_where"><!-- 使用动态sql,通过if判断,满足条件进行sql拼接 --><!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 --><if test="itemsCustom!=null"><if test="itemsCustom.name!=null and itemsCustom.name!=''">items.name LIKE '%${itemsCustom.name}%'</if></if></sql><!-- 商品列表查询 --><!-- parameterType传入包装对象(包装了查询条件)resultType建议使用扩展对象--><select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo"resultType="cn.itcast.ssm.po.ItemsCustom">SELECT items.* FROM items <where><include refid="query_items_where"></include></where></select></mapper>
applicationContext-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 商品管理的service -->
<bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"/>
</beans>
事务控制(applicationContext-transaction.xml)
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源dataSource在applicationContext-dao.xml中配置了--><property name="dataSource" ref="dataSource"/>
</bean><!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 传播行为 --><tx:method name="save*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/></tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config></beans>
springmvc.xml
创建springmvc.xml文件,配置处理器映射器、适配器、视图解析器
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包--><context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan><!--注解映射器 --><!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --><!--注解适配器 --><!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --><!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter实际开发时使用mvc:annotation-driven--><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 视图解析器解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置jsp路径的前缀 --><property name="prefix" value="/WEB-INF/jsp/"/><!-- 配置jsp路径的后缀 --><property name="suffix" value=".jsp"/></bean><!-- 自定义参数绑定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><!-- 转换器 --><property name="converters"><list><!-- 日期类型转换 --><bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/></list></property></bean>
</beans>
工程的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>springmvc_mybatis1208</display-name><!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- springmvc前端控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 --><url-pattern>*.action</url-pattern></servlet-mapping><!-- post乱码过虑器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>
转载于:https://www.cnblogs.com/reblue520/p/6239914.html
springmvc和mybatis整合关键配置相关推荐
- SpringMvc和Mybatis整合需要配置的xml
applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...
- springMVC+Spring+mybatis整合配置版与注解版
springMVC+Spring+mybatis整合 , 使用配置版来完成: -----------------------------前端 1.创建web系统,导入jar包: spring的jar包 ...
- java元婴期(29)----java进阶(springmvc(3)---springmvc和mybatis整合参数绑定(上))
springmvc和mybatis整合 需求 使用springmvc和mybatis完成商品列表查询. 整合思路 springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和 ...
- SpringMVC与Mybatis整合---SpringMVC学习笔记(六)
SpringMVC整合Mybatis的系统架构: 整合思路 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper ...
- SSM框架终极篇——Spring、SpringMVC、MyBatis整合练习(超级详细)
SSM框架终极篇--Spring.SpringMVC.MyBatis整合练习 一.准备工作 环境: 新版IDEA MySQL 5.7 Tomcat 9.0.2 Maven 要求: 熟练掌握MySQL数 ...
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十四)——SpringMVC和MyBatis整合
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十三)--S ...
- mybatis学习(十一)——springmvc++spring+mybatis整合
做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...
- Spring、Spring MVC、MyBatis整合文件配置详解
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经 ...
最新文章
- docker探索-使用docker service管理swarm(十一 )
- Cer Crt Pem Pfx 证书格式转换
- matlab多元约束最小值,无约束多变量最小值求解问题
- leetcode —— 面试题32 - III. 从上到下打印二叉树 III
- 利用IKVM在C#中调Java程序
- D3.js学习(一)
- ios 判断手机角度_iPhone那么贵,为什么电池还那么小呢?安卓手机电池都那么大了!...
- MongoDB实战系列之三:MongoDB的主从部署
- Linux二进制保护(文末福利)
- 京东-黑客马拉松大赛复盘
- Microsoft Office SharePoint Designer 2007 宣布免费
- 百度php工程师面试题
- 无法启用家庭计算机共享,如何解决Win7无法启用共享访问的问题?
- 分享一些图片懒加载组件的设计思路
- JavaScript内的类型转换
- ECharts 中的事件和行为
- Fabrice Bellard其人 ---- FFMPEG及其他……
- 强大的抓包工具 Fiddler Web Debugger v5.0 中文破解版
- .flo 文件转换为.png 文件 ; matlab 读取 .ppm 和 .flo 文件
- 微信公众号开发教程[017]-网页开发-JSSDK
热门文章
- 当年学生的一件事,心情很难过
- OpenCV 【四】————Watershed Algorithm(图像分割)——分水岭算法的原理及实现
- TCP性能和发送接收Buffer的关系
- 手机自带的显示基站命令
- 开源库BearSSL介绍及使用
- 程序员的自我修养--链接、装载与库笔记:系统调用与API
- java多线程工具类_Java多线程系列之:线程的并发工具类
- matlab 数据集制作,机器学习数据集制作与划分MATLAB实现
- linux创建redis容器,docker-compose实现redis部署及键值添加
- 数据处理过程放在oracle,Oracle存储过程语法及常用函数总结