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 ...
随机推荐
- 51nod1563
题解: 其实只要排个序贪心一下就好了...代码600B不到... 代码: #include<bits/stdc++.h> using namespace std; ,INF=1e9; in ...
- Django的坑
静态地址:我还没有向服务器部署,这只是临时方案 编辑项目setting.py,(main/setting.py)最下面一部分# Static files (CSS, JavaScript, Image ...
- C++中的接口继承和实现继承
很多人认为,C++中是不存在接口继承的,只有Java.C#这类语言才提供了相应的语法支持. 但是,如同鲁迅说过的某句名言:世上本没有接口继承,用的人多了,才有了接口继承.C++中依然可以实现接口继承, ...
- xitong
回复 YAJE3 :http://msdn.anjieart.net/和http://msdn.ez58.net/files/windows%20vista同样是MSDN网站 msdnitellyou ...
- 利用InstallShiled 10.5制作AE应用程序安装包
[转]利用InstallShiled 10.5制作AE应用程序安装包 作者:3SNEWS 社区ESRI(ArcGIS)版版主:zhaoxiang_whuhttp://www.3snews.net/bb ...
- 踢掉某个li
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- avalon 路由问题
1, 直接使用avalon的 amd加载器, 可以不需要 require.js 2, 配置baseUrl 路径, 这个一定要在 js所在的目录, 而不是jsp所在的目录, 如果js 和jsp分开 ...
- mysql 删除表中记录
一.清除mysql表中数据 delete from 表名;truncate table 表名;不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以 ...
- pip source
linux版本 sudo vim .pip/pip.conf[global]index-url = http://pypi.douban.com/simple[install]trusted-host ...
- hadoop之HDFS运行小观察
hadoop 是当前很火的一个 大数据运行框架和平台, 对于这个神奇的大家伙我甚是搞不清楚,前段时间闲来无视便把 HADOOP 运行起来, 看着它的操作记录存储部分(操作日志), IMAGE 记录着 ...