springMVC学习(3)-springMVC和mybatis整合
一、需求:
使用springmvc和mybatis完成商品列表查询。
二、整合思路:
springMVC+mybaits的系统架构:

1步):整合dao层
mybatis和spring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
2步):整合service层
通过spring管理 service接口。使用配置方式将service接口配置在spring配置文件中。实现事务控制。
3步)整合springmvc 由于springmvc是spring的模块,不需要整合。
所需要的jar包:
数据库驱动包:mysql5.1、mybatis的jar包、mybatis和spring整合包、log4j包、dbcp数据库连接池包、spring3.2所有jar包、jstl包
三、开始整合和开发:
1)工程结构:



2)整合dao:
配置sqlMapConfig.xml;
配置applicationContext-dao.xml:(数据源、sqlSessionFactory、mapper扫描器)
逆向工程生成mapper接口、mapper.xml、po类;将生成的文件拷贝至工程中;
编写扩展po类poCustom、以及对应的poCustom.xml和接口poCustom.java;
3)整合Serive:
配置applicationContext-service.xml(配置Service.bean、配置事务)
4)整合springMVC:
配置springMVC.xml(配置处理器映射器、适配器、视图解析器;相关扫描包)
配置web.xml(配置前端控制器、监听器加载spring配置文件(使用通配符))
相关代码:
items.java:
package com.cy.po;
import java.util.Date;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
...
getters and setters
}
Items.java
ItemsCustom.java:
package com.cy.po; /**
* 商品信息的扩展类
* @author chengyu
*
*/
public class ItemsCustom extends Items{
//添加商品信息的扩展属性
}
ItemsQueryVo.java:
package com.cy.po; /**
* 商品包装对象
* @author chengyu
*
*/
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom; public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
} }
ItemsMapperCustom.java:
package com.cy.mapper; import java.util.List; import com.cy.po.ItemsCustom;
import com.cy.po.ItemsQueryVo; public interface ItemsMapperCustom {
//商品查询列表
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
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="com.cy.mapper.ItemsMapperCustom" > <sql id="query_items_where">
<!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->
<if test="itemsCustom!=null">
<if test="itemsCustom.name!=null and itemsCustom.name!=''">
and itemsCustom.name like '%${itemsCustom.name}%'
</if>
</if>
</sql> <!-- 商品列表查询 -->
<!-- parameterType传入包装对象(包装了查询条件)
resultType建议使用扩展对象
-->
<select id="findItemsList" parameterType="com.cy.po.ItemsQueryVo" resultType="com.cy.po.ItemsCustom">
select * from items
<where>
<include refid="query_items_where"></include>
</where>
</select>
</mapper>
ItemsService接口:
package com.cy.service; import java.util.List; import com.cy.po.ItemsCustom;
import com.cy.po.ItemsQueryVo; /**
* 商品管理service
* @author chengyu
*
*/
public interface ItemsService { public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
ItemsServiceImpl实现类:
package com.cy.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.cy.mapper.ItemsMapperCustom;
import com.cy.po.ItemsCustom;
import com.cy.po.ItemsQueryVo;
import com.cy.service.ItemsService; /**
* ItemsServiceImpl
*
*/
public class ItemsServiceImpl implements ItemsService { @Autowired
private ItemsMapperCustom itemsMapperCustom; @Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通过ItemsMapperCustom查询数据库
return itemsMapperCustom.findItemsList(itemsQueryVo);
} }
ItemsController:
package com.cy.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.cy.po.ItemsCustom;
import com.cy.service.ItemsService; @Controller
public class ItemsController { @Autowired
private ItemsService itemsService; @RequestMapping("/findItems")
public ModelAndView findItems() throws Exception { List<ItemsCustom> itemsList = itemsService.findItemsList(null); ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("itemsList");
return modelAndView;
}
}
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="com.cy.po"/>
</typeAliases> <!-- 配置mapper
由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
必须遵循:mapper.xml和mapper.java文件同名且在一个目录
-->
<!-- <mappers></mappers> -->
</configuration>
applicationContext-dao.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 "> <!-- 加载db.properties -->
<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="com.cy.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
applicationContext-serivice.xml:
<!-- 商品管理的service -->
<bean id="itemsService" class="com.cy.service.impl.ItemsServiceImpl" />
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"></property>
</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>
<!-- 切点 在com.cy.service.impl包下的所有类的所有方法(不论参数) -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.cy.service.impl.*.*(..))"/>
</aop:config>
</beans>
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 "> <!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 -->
<context:component-scan base-package="com.cy.controller" /> <!-- mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,实际开发时使用mvc:annotation-driven-->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器 解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包 -->
<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://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springMVC</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>
<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>
<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>
jsp页面和db.properties和log4j.properties和之前的配置一样;
测试:
浏览器访问http://localhost:8080/springMVC/findItems.action

springMVC学习(3)-springMVC和mybatis整合的更多相关文章
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
- (转)SpringMVC学习(五)——SpringMVC的参数绑定
http://blog.csdn.net/yerenyuan_pku/article/details/72511611 SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解.本文所有案 ...
- (转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272 SpringMVC介绍 SpringMVC是什么? SpringMVC和Stru ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- (转)SpringMVC学习(三)——SpringMVC的配置文件
http://blog.csdn.net/yerenyuan_pku/article/details/72231527 读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后 ...
- spring学习 六 spring与mybatis整合
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- SpringMVC学习(二)——SpringMVC架构及组件(及其运行原理)-转载
相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上一篇文章中SpringMVC的处理流程吗? 这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比 ...
- SpringMVC 学习 十一 springMVC控制器向jsp或者别的控制器传递参数的四种方法
以后的开发,大部分是发送ajax,因此这四种传递参数的方法,并不太常用.作为了解吧 第一种:使用原生 Servlet 在控制器的响应的方法中添加Servlet中的一些作用域:HttpRequestSe ...
随机推荐
- OPENWRT常用设置
常用设置: 计划任务,定时重启 系统--计划任务,每行一个计划任务. 然后是计划任务列表的格式: [minute] [hour] [day of month] [month] [day of week ...
- 2015年蓝桥杯省赛B组第3题--三羊献瑞
三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞 ------------------- 三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的 ...
- 什么是 dynaTrace Ajax
随着 jQuery.Dojo.YUI 等框架的兴起让构建 Web2.0 应用更加容易,但随之带来的定位等应用问题也越来越难,尤其是与性能相关的.dynaTrace Ajax Edition 是一个强大 ...
- C++ primer 第四版 练习3.13,3.14
读一组整数到 vector 对象,计算并输出每对相邻元素的 和.如果读入元素个数为奇数,则提示用户后一个元素 没有求和,并输出其值. vector<int> ivec; int ival; ...
- ora-01830:日期格式图片在转换整个输入字符串之前结束
在to_date的时候因为 fldsj这个字段的大小 与 ‘yyyy-MM-dd’不符合 所以需要截取下fldsj字段的大小 to_char(to_date(substr(fldsj,1,10),' ...
- oracle function用法(本文来自百度文库)
函数调用限制 1.SQL语句中只能调用存储函数(服务器端),而不能调用客户端的函数 2.SQL只能调用带有输入参数,不能带有输出,输入输出函数 3.SQL不能使用PL/SQL的特有数据类型(boole ...
- 题目1001:A+B for Matrices
题目1001:A+B for Matrices 时间限制:1 秒内存限制:32 兆 题目描述: This time, you are supposed to find A+B where A and ...
- can-utils源码解析cansend
前言 本文主要介绍socketCan中的发送函数cansend的源码解析. 代码 /* * cansend.c - simple command line tool to send CAN-frame ...
- android复制包需要修改的几个地方
1.要看什么情况,若是在同一个eclipse下,那么就需要修改包名.若不在的话,那就可以不用了. 2.这个app_name也是一样. 3.先说下情况,这是我修改好的.原本这个R的是引之前包的,必须要改 ...
- day43 数据库学习egon的博客 约束
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...