一、整合思路

1.1 Dao层

  • SqlMapConfig.xml:空文件即可,但是需要文件头。
  • applicationContext-dao.xml

    • 数据库连接池

    • SqlSessionFactory对象,需要spring和mybatis整合包下的。

    • 配置mapper文件扫描器。

1.2 Service层

  • applicationContext-service.xml:包扫描器,扫描@service注解的类。
  • applicationContext-trans.xml:配置事务。

1.3 Controller层

  • Springmvc.xml:

    • 包扫描器,扫描@Controller注解的类。
    • 配置注解驱动

    • 配置视图解析器

  • Web.xml文件:
    • 配置监听器读取applicationContext.xml上下文

    • 配置前端控制器读取springmvc.xml

    • 配置POST提交乱码过滤器

二、整合步骤

2.1 创建动态web工程

2.2 导入jar包

  1. spring(包括springmvc)

  2. mybatis

  3. mybatis-spring整合包

  4. 数据库驱动

  5. 第三方连接池。

  6. Json依赖包Jackson

   

2.3 加入配置文件

  创建资源文件夹config,在其下创建mybatis和spring文件夹,用来存放配置文件,如下图:

    

【SqlMapConfig.xml】

  使用逆向工程来生成Mapper相关代码,不需要配置别名。在config/mybatis下创建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>

【applicationContext-dao.xml】

  配置数据源、配置SqlSessionFactory、mapper扫描器。

  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" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10.  
  11. <!-- 加载配置文件 -->
  12. <context:property-placeholder location="classpath:db.properties"/>
  13.  
  14. <!-- 数据库连接池 -->
  15. <!-- destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. -->
  16. <bean id="dataSource" class=" org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  17. <property name="driverClassName" value="${jdbc.driver}" />
  18. <property name="url" value="${jdbc.url}" />
  19. <property name="username" value="${jdbc.username}" />
  20. <property name="password" value="${jdbc.password}" />
  21. <property name="maxActive" value="10" />
  22. <property name="maxIdle" value="5" />
  23. </bean>
  24.  
  25. <!-- 配置SqlSessionFactory -->
  26. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  27. <!-- 数据库连接池 -->
  28. <property name="dataSource" ref="dataSource"/>
  29. <!-- 加载mybatis的全局配置文件 -->
  30. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
  31. </bean>
  32.  
  33. <!-- 配置Mapper扫描 -->
  34. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  35. <!-- 配置Mapper扫描包 -->
  36. <property name="basePackage" value="cn.itcast.ssm.mapper"></property>
  37. </bean>
  38.  
  39. </beans>

【db.properties】

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root

【applicationContext-service.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" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10.  
  11. <!-- 配置service扫描 -->
  12. <context:component-scan base-package="cn.itcast.ssm.service" />
  13.  
  14. </beans>

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

【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. <!-- 配置controller扫描包 -->
  11. <context:component-scan base-package="cn.itcast.ssm.controller" />
  12.  
  13. <!-- 注解驱动 -->
  14. <mvc:annotation-driven />
  15.  
  16. <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
  17. "/WEB-INF/jsp/test.jsp" -->
  18. <!-- 配置视图解析器 -->
  19. <bean
  20. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <!-- 配置逻辑视图的前缀 -->
  22. <property name="prefix" value="/WEB-INF/jsp/" />
  23. <!-- 配置逻辑视图的后缀 -->
  24. <property name="suffix" value=".jsp" />
  25. </bean>
  26.  
  27. </beans>

【web.xml】

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>springmvc-web</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. <welcome-file>index.htm</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. <welcome-file>default.html</welcome-file>
  12. <welcome-file>default.htm</welcome-file>
  13. <welcome-file>default.jsp</welcome-file>
  14. </welcome-file-list>
  15.  
  16. <!-- 配置spring -->
  17. <context-param>
  18. <param-name>contextConfigLocation</param-name>
  19. <param-value>classpath:spring/applicationContext*.xml</param-value>
  20. </context-param>
  21.  
  22. <!-- 使用监听器加载spring配置文件 -->
  23. <listener>
  24. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  25. </listener>
  26.  
  27. <!-- 配置SrpingMVC的前端控制器 -->
  28. <servlet>
  29. <servlet-name>springmvc-web</servlet-name>
  30. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  31. <init-param>
  32. <param-name>contextConfigLocation</param-name>
  33. <param-value>classpath:spring/springmvc.xml</param-value>
  34. </init-param>
  35. </servlet>
  36.  
  37. <servlet-mapping>
  38. <servlet-name>springmvc-web</servlet-name>
  39. <!-- 配置所有以action结尾的请求进入SpringMVC -->
  40. <url-pattern>*.action</url-pattern>
  41. </servlet-mapping>
  42. </web-app>

2.4 加入jsp页面

  将itemList.jsp放到WEB-INF/jsp目录下

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>查询商品列表</title>
  10. </head>
  11. <body>
  12. <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
  13. 查询条件:
  14. <table width="100%" border=1>
  15. <tr>
  16. <td><input type="submit" value="查询"/></td>
  17. </tr>
  18. </table>
  19. 商品列表:
  20. <table width="100%" border=1>
  21. <tr>
  22. <td>商品名称</td>
  23. <td>商品价格</td>
  24. <td>生产日期</td>
  25. <td>商品描述</td>
  26. <td>操作</td>
  27. </tr>
  28. <c:forEach items="${itemList }" var="item">
  29. <tr>
  30. <td>${item.name }</td>
  31. <td>${item.price }</td>
  32. <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
  33. <td>${item.detail }</td>
  34.  
  35. <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
  36.  
  37. </tr>
  38. </c:forEach>
  39.  
  40. </table>
  41. </form>
  42. </body>
  43.  
  44. </html>

三、实现商品列表显示

3.1 需求

  实现商品查询列表,从mysql数据库查询商品信息。

  

3.2 Dao开发

  使用逆向工程,生成代码(注意修改逆向工程的配置文件)

    

3.3 ItemService接口

  1. public interface ItemService {
  2.  
  3. /**
  4. * 查询商品列表
  5. *
  6. * @return
  7. */
  8. List<Item> queryItemList();
  9.  
  10. }

3.4 ItemServiceImpl实现类

  1. @Service
  2. public class ItemServiceImpl implements ItemService {
  3.  
  4. @Autowired
  5. private ItemMapper itemMapper;
  6.  
  7. @Override
  8. public List<Item> queryItemList() {
  9. // 从数据库查询商品数据
  10. List<Item> list = this.itemMapper.selectByExample(null);
  11.  
  12. return list;
  13. }
  14.  
  15. }

3.5 ItemController

  1. @Controller
  2. public class ItemController {
  3.  
  4. @Autowired
  5. private ItemService itemService;
  6.  
  7. /**
  8. * 显示商品列表
  9. *
  10. * @return
  11. */
  12. @RequestMapping("/itemList")
  13. public ModelAndView queryItemList() {
  14. // 获取商品数据
  15. List<Item> list = this.itemService.queryItemList();
  16.  
  17. ModelAndView modelAndView = new ModelAndView();
  18. // 把商品数据放到模型中
  19. modelAndView.addObject("itemList", list);
  20. // 设置逻辑视图
  21. modelAndView.setViewName("itemList");
  22.  
  23. return modelAndView;
  24. }
  25.  
  26. }

3.6 测试

  

SprimgMVC学习笔记(二)—— 整合Mybatis的更多相关文章

  1. MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...

  2. Java学习笔记-spring整合mybatis

    这个项目就是一个例子,只有添加图书的功能: 项目架构: resource: 整合流程: 1.pom文件节点,这两个是整合用的,其他节点不再赘述: <!-- https://mvnreposito ...

  3. Spring Boot学习笔记(五)整合mybatis

    pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...

  4. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  5. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  6. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  7. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  8. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  9. 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    [转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...

  10. java之jvm学习笔记二(类装载器的体系结构)

    java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...

随机推荐

  1. C# Remoting 简单实现

    此处下载源代码(VS2010编译通过)   http://files.cnblogs.com/files/qqhfeng/%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E6 ...

  2. leetcode424

    public class Solution { public int CharacterReplacement(string s, int k) { int len = s.Length; ]; , ...

  3. 数据从HDFS-->HIVE-->HBASE 执行过程

    1.数据已经load进去hdfs 2.hive.hbase已经安装成功(我用的是hadoop 2.4 hbase 0.98.12 hive 1.2.1) 3.开始! 4.在hive建立表同时生成对应的 ...

  4. cookie禁用后非重定向跳转时session的跟踪

  5. AWT简介

    -------------siwuxie095                         AWT 简介:     AWT(Abstract Window Toolkit)是最原始的 Java G ...

  6. 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据

    一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...

  7. QT中显示图像数据

    博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325091 一般图像数据都是以RGBRGBRGB……字节流的方式(解码完成后的原 ...

  8. 每个程序中只有一个public类,主类?

    import java.io.*; public class GameSaverTest { public static void main(String[] args){ //创建人物 GameCh ...

  9. UVA1723 Intervals

    这题$n$倍经验…… 考虑差分约束: 我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} \leq c_i$,那么连边$ ...

  10. 移动应用中的AR开发,5款最受欢迎工具推荐!

      英文原文:Top 5 Tools for Augmented Reality in Mobile Apps 还记得前段时间在网上很火的 3D 小熊不?托它的福,为相当一部分人科普了增强现实(AR) ...