springmvc教程(2)
springmvc教程系列
springmvc史上最好教程(3)
springmvc史上最好教程(1)
一、整合mybatis
为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合。
整合目标:控制层采用springmvc、持久层使用mybatis实现。
1.1需求
实现商品查询列表,从mysql数据库查询商品信息。
1.2jar包
包括:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。
参考:“mybatis与springmvc整合全部jar包”目录
1.3Dao
目标:
1、spring管理SqlSessionFactory、mapper
详细参考mybatis教程与spring整合章节。
1.3.1sqlMapConfig.xml
在classpath下创建mybatis/sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!—使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers --><mappers><package name="com.sihai.ssm.mapper" /></mappers></configuration>
1.3.2applicationContext-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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池 --><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><!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><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="com.sihai.springmvc.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean></beans>
1.3.3ItemsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.sihai.ssm.mapper.ItemsMapper"><!-- sql片段 --><!-- 商品查询条件 --><sql id="query_items_where"><if test="items!=null"><if test="items.name!=null and items.name!=''">and items.name like '%${items.name}%'</if></if></sql><!-- 查询商品信息 --><select id="findItemsList" parameterType="queryVo" resultType="items">select * from items<where><include refid="query_items_where"/></where></select></mapper>
1.3.4ItemsMapper.java
public interface ItemsMapper {//商品列表public List<Items> findItemsList(QueryVo queryVo) throws Exception;}
1.4Service
目标:
1、Service由spring管理
2、spring对Service进行事务控制。
1.4.1applicationContext-service.xml
配置service接口。
1.4.2applicationContext-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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.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:attributes></tx:advice><!-- 切面 --><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* com.sihai.springmvc.service.impl.*.*(..))"/></aop:config></beans>
1.4.3OrderService
public interface OrderService {//商品查询列表public List<Items> findItemsList(QueryVo queryVo)throws Exception;}@Autowiredprivate ItemsMapper itemsMapper;@Overridepublic List<Items> findItemsList(QueryVo queryVo) throws Exception {//查询商品信息return itemsMapper.findItemsList(queryVo);}}
1.5Action
1.5.1springmvc.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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 扫描controller注解,多个包中间使用半角逗号分隔 --><context:component-scan base-package="com.sihai.ssm.controller"/><!--注解映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><!--注解适配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- ViewResolver --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>
1.5.2web.xml
加载spring容器,配置springmvc前置控制器。
<?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</display-name><!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/spring/applicationContext.xml,/WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 解决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><!-- springmvc的前端控制器 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-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>
1.5.3OrderController
@Controllerpublic class OrderController {@Autowiredprivate OrderService orderService;@RequestMapping("/queryItem.action")public ModelAndView queryItem() throws Exception {// 商品列表List<Items> itemsList = orderService.findItemsList(null);// 创建modelAndView准备填充数据、设置视图ModelAndView modelAndView = new ModelAndView();// 填充数据modelAndView.addObject("itemsList", itemsList);// 视图modelAndView.setViewName("order/itemsList");return modelAndView;}}
1.6测试
http://localhost:8080/springmvc_mybatis/queryItem.action
springmvc教程(2)相关推荐
- springmvc教程(4)
springmvc教程系列 springmvc史上最好教程(2) springmvc史上最好教程(1) springmvc史上最好教程(3) 2注解开发-高级 2.1上传图片 2.1.1配置虚拟目录 ...
- springmvc教程--注解开发基础详解
springmvc教程系列 springmvc史上最好教程(2) springmvc史上最好教程(1) 一. 注解开发-基础 1.1 需求 使用springmvc+mybatis架构实现商品信息维护. ...
- springmvc教程(3)
springmvc教程系列 springmvc史上最好教程(2) springmvc史上最好教程(1) 一. 注解开发-基础 1.1需求 使用springmvc+mybatis架构实现商品信息维护. ...
- springmvc教程--快速入门教程
springmvc教程系列: springmvc史上最好教程(3) springmvc史上最好教程(2) 一. SpringMVC架构 1.1. Spring web mvc介绍 Spring web ...
- springmvc教程(1)
springmvc教程系列: springmvc史上最好教程(3) springmvc史上最好教程(2) 一. SpringMVC架构 1.1. Spring web mvc介绍 Spring web ...
- spring-mvc教程_使用MVC模式制作游戏-教程和简介
spring-mvc教程 游戏开发中一种有用的体系结构模式是MVC(模型视图控制器)模式. 它有助于分离输入逻辑,游戏逻辑和UI(渲染). 在任何游戏开发项目的早期阶段,它的用途很快就会被注意到,因为 ...
- SpringMVC教程下篇
SpringMVC教程下篇 内容包括: 绑定数组: 将表单数据绑定到list: @RequestMapping注解的三种用法: Controller方法返回值: 乱码问题总结 异常处理: 照片上传: ...
- SpringMVC教程上篇
SpringMVC教程上篇 SpringMVC优势: SpringMVC代码执行流程: 框架结构: 架构流程: 组件说明: SpringMVC与Mybatis整合 ! 效果: 开发流程:
- 最全面的SpringMVC教程(六)——WebSocket
前言 本文为 [SpringMVC教程]WebSocket 相关知识介绍,具体将对WebSocket进行简介,并通过实战案例对WebSocket的使用进行详尽介绍~
最新文章
- 刷题两个月,从入门到字节跳动offer,这是我的模板 | GitHub 1.2k星
- 软件工程 / 为什么基于接口而非实现编程?
- codeforces 932D Tree 倍增法+二分搜索
- 2018-2019-1 20165303 实验五 通讯协议设计
- 可持续字典树 Perfect Security
- 偷梁换柱 | 无备份情况下的数据恢复实践
- 华为云PB级数据库GaussDB(for Redis)揭秘第十期:GaussDB(for Redis)迁移系列(上)
- EntityModelStudio系列教程
- java如何打JAR包
- 写给想做好社区网站人员的一本书
- 剑指offer七:两个链表的第一个公共结点
- fritzing元件太少_fritzing传感器元件库
- 面试官:ca证书存储在哪的
- CSS opacity - 实现图片半透明效果
- lol刷金币python脚本_用Python写王者荣耀刷金币脚本
- 找个好人就嫁了吧 - 刘思伟
- 产品分析报告——“京东到家”
- SAP-PM设备模块-维修执行-维修工单
- matlab怎么加采样开关,开关量采集模块怎么使用?
- 《无线通信与网络》tcp udp 对比_TCP与UDP究竟谁更可靠?
热门文章
- ESP8266--学习笔记(八)串口源码分析
- ubuntu 12.04下搭建web服务器(MySQL+PHP+Apache) 教程
- cmake (4)引用子目录的库
- 石墨烯区块链(6)开发实例
- 独一无二的《斗罗大陆》小游戏火爆上线,玩家闯关等你来~(等级有点儿难)
- 多线程通信—生产者和消费者模式
- python—多进程之进程之间通信
- [ARM-assembly]-全局变量/静态全局变量/初始化/未初始化变量的存放位置分析
- 动态链接库编写与使用(VC6)
- 蜜罐中利用jsonp跨域漏洞和xss漏洞的分析