SprimgMVC学习笔记(二)—— 整合Mybatis
一、整合思路
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包
spring(包括springmvc)
mybatis
mybatis-spring整合包
数据库驱动
第三方连接池。
Json依赖包Jackson
2.3 加入配置文件
创建资源文件夹config,在其下创建mybatis和spring文件夹,用来存放配置文件,如下图:
【SqlMapConfig.xml】
使用逆向工程来生成Mapper相关代码,不需要配置别名。在config/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>
- </configuration>
【applicationContext-dao.xml】
配置数据源、配置SqlSessionFactory、mapper扫描器。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 加载配置文件 -->
- <context:property-placeholder location="classpath:db.properties"/>
- <!-- 数据库连接池 -->
- <!-- destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. -->
- <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="10" />
- <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">
- <!-- 配置Mapper扫描包 -->
- <property name="basePackage" value="cn.itcast.ssm.mapper"></property>
- </bean>
- </beans>
【db.properties】
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
- jdbc.username=root
- jdbc.password=root
【applicationContext-service.xml】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 配置service扫描 -->
- <context:component-scan base-package="cn.itcast.ssm.service" />
- </beans>
【applicationContext-trans.xml】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- 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
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- 数据源 -->
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 通知 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- 传播行为 -->
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="delete*" 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="query*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 切面 -->
- <aop:config>
- <aop:advisor advice-ref="txAdvice"
- pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
- </aop:config>
- </beans>
【springmvc.xml】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 配置controller扫描包 -->
- <context:component-scan base-package="cn.itcast.ssm.controller" />
- <!-- 注解驱动 -->
- <mvc:annotation-driven />
- <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
- "/WEB-INF/jsp/test.jsp" -->
- <!-- 配置视图解析器 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 配置逻辑视图的前缀 -->
- <property name="prefix" value="/WEB-INF/jsp/" />
- <!-- 配置逻辑视图的后缀 -->
- <property name="suffix" value=".jsp" />
- </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"
- 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-web</display-name>
- <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>
- <!-- 配置spring -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/applicationContext*.xml</param-value>
- </context-param>
- <!-- 使用监听器加载spring配置文件 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置SrpingMVC的前端控制器 -->
- <servlet>
- <servlet-name>springmvc-web</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/springmvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc-web</servlet-name>
- <!-- 配置所有以action结尾的请求进入SpringMVC -->
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- </web-app>
2.4 加入jsp页面
将itemList.jsp放到WEB-INF/jsp目录下
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>查询商品列表</title>
- </head>
- <body>
- <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
- 查询条件:
- <table width="100%" border=1>
- <tr>
- <td><input type="submit" value="查询"/></td>
- </tr>
- </table>
- 商品列表:
- <table width="100%" border=1>
- <tr>
- <td>商品名称</td>
- <td>商品价格</td>
- <td>生产日期</td>
- <td>商品描述</td>
- <td>操作</td>
- </tr>
- <c:forEach items="${itemList }" var="item">
- <tr>
- <td>${item.name }</td>
- <td>${item.price }</td>
- <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
- <td>${item.detail }</td>
- <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
- </tr>
- </c:forEach>
- </table>
- </form>
- </body>
- </html>
三、实现商品列表显示
3.1 需求
实现商品查询列表,从mysql数据库查询商品信息。
3.2 Dao开发
使用逆向工程,生成代码(注意修改逆向工程的配置文件)
3.3 ItemService接口
- public interface ItemService {
- /**
- * 查询商品列表
- *
- * @return
- */
- List<Item> queryItemList();
- }
3.4 ItemServiceImpl实现类
- @Service
- public class ItemServiceImpl implements ItemService {
- @Autowired
- private ItemMapper itemMapper;
- @Override
- public List<Item> queryItemList() {
- // 从数据库查询商品数据
- List<Item> list = this.itemMapper.selectByExample(null);
- return list;
- }
- }
3.5 ItemController
- @Controller
- public class ItemController {
- @Autowired
- private ItemService itemService;
- /**
- * 显示商品列表
- *
- * @return
- */
- @RequestMapping("/itemList")
- public ModelAndView queryItemList() {
- // 获取商品数据
- List<Item> list = this.itemService.queryItemList();
- ModelAndView modelAndView = new ModelAndView();
- // 把商品数据放到模型中
- modelAndView.addObject("itemList", list);
- // 设置逻辑视图
- modelAndView.setViewName("itemList");
- return modelAndView;
- }
- }
3.6 测试
SprimgMVC学习笔记(二)—— 整合Mybatis的更多相关文章
- MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...
- Java学习笔记-spring整合mybatis
这个项目就是一个例子,只有添加图书的功能: 项目架构: resource: 整合流程: 1.pom文件节点,这两个是整合用的,其他节点不再赘述: <!-- https://mvnreposito ...
- Spring Boot学习笔记(五)整合mybatis
pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
[转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...
- java之jvm学习笔记二(类装载器的体系结构)
java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...
随机推荐
- C# Remoting 简单实现
此处下载源代码(VS2010编译通过) http://files.cnblogs.com/files/qqhfeng/%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E6 ...
- leetcode424
public class Solution { public int CharacterReplacement(string s, int k) { int len = s.Length; ]; , ...
- 数据从HDFS-->HIVE-->HBASE 执行过程
1.数据已经load进去hdfs 2.hive.hbase已经安装成功(我用的是hadoop 2.4 hbase 0.98.12 hive 1.2.1) 3.开始! 4.在hive建立表同时生成对应的 ...
- cookie禁用后非重定向跳转时session的跟踪
- AWT简介
-------------siwuxie095 AWT 简介: AWT(Abstract Window Toolkit)是最原始的 Java G ...
- 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据
一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...
- QT中显示图像数据
博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325091 一般图像数据都是以RGBRGBRGB……字节流的方式(解码完成后的原 ...
- 每个程序中只有一个public类,主类?
import java.io.*; public class GameSaverTest { public static void main(String[] args){ //创建人物 GameCh ...
- UVA1723 Intervals
这题$n$倍经验…… 考虑差分约束: 我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} \leq c_i$,那么连边$ ...
- 移动应用中的AR开发,5款最受欢迎工具推荐!
英文原文:Top 5 Tools for Augmented Reality in Mobile Apps 还记得前段时间在网上很火的 3D 小熊不?托它的福,为相当一部分人科普了增强现实(AR) ...