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:
1 package com.cy.po; 2 3 import java.util.Date; 4 5 public class Items { 6 private Integer id; 7 private String name; 8 private Float price; 9 private String pic; 10 private Date createtime; 11 private String detail; 12 ... 13 14 getters and setters 15 }
ItemsCustom.java:
package com.cy.po; /** * 商品信息的扩展类 * @author chengyu * */ public class ItemsCustom extends Items{ //添加商品信息的扩展属性 }
ItemsQueryVo.java:
1 package com.cy.po; 2 3 /** 4 * 商品包装对象 5 * @author chengyu 6 * 7 */ 8 public class ItemsQueryVo { 9 //商品信息 10 private Items items; 11 //为了系统 可扩展性,对原始生成的po进行扩展 12 private ItemsCustom itemsCustom; 13 14 public Items getItems() { 15 return items; 16 } 17 public void setItems(Items items) { 18 this.items = items; 19 } 20 public ItemsCustom getItemsCustom() { 21 return itemsCustom; 22 } 23 public void setItemsCustom(ItemsCustom itemsCustom) { 24 this.itemsCustom = itemsCustom; 25 } 26 27 28 }
ItemsMapperCustom.java:
1 package com.cy.mapper; 2 3 import java.util.List; 4 5 import com.cy.po.ItemsCustom; 6 import com.cy.po.ItemsQueryVo; 7 8 public interface ItemsMapperCustom { 9 //商品查询列表 10 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; 11 }
ItemsMapperCustom.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.cy.mapper.ItemsMapperCustom" > 4 5 <sql id="query_items_where"> 6 <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 --> 7 <if test="itemsCustom!=null"> 8 <if test="itemsCustom.name!=null and itemsCustom.name!=''"> 9 and itemsCustom.name like '%${itemsCustom.name}%' 10 </if> 11 </if> 12 </sql> 13 14 <!-- 商品列表查询 --> 15 <!-- parameterType传入包装对象(包装了查询条件) 16 resultType建议使用扩展对象 17 --> 18 <select id="findItemsList" parameterType="com.cy.po.ItemsQueryVo" resultType="com.cy.po.ItemsCustom"> 19 select * from items 20 <where> 21 <include refid="query_items_where"></include> 22 </where> 23 </select> 24 </mapper>
ItemsService接口:
1 package com.cy.service; 2 3 import java.util.List; 4 5 import com.cy.po.ItemsCustom; 6 import com.cy.po.ItemsQueryVo; 7 8 /** 9 * 商品管理service 10 * @author chengyu 11 * 12 */ 13 public interface ItemsService { 14 15 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; 16 }
ItemsServiceImpl实现类:
1 package com.cy.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 7 import com.cy.mapper.ItemsMapperCustom; 8 import com.cy.po.ItemsCustom; 9 import com.cy.po.ItemsQueryVo; 10 import com.cy.service.ItemsService; 11 12 /** 13 * ItemsServiceImpl 14 * 15 */ 16 public class ItemsServiceImpl implements ItemsService { 17 18 @Autowired 19 private ItemsMapperCustom itemsMapperCustom; 20 21 @Override 22 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { 23 //通过ItemsMapperCustom查询数据库 24 return itemsMapperCustom.findItemsList(itemsQueryVo); 25 } 26 27 }
ItemsController:
1 package com.cy.controller; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import com.cy.po.ItemsCustom; 11 import com.cy.service.ItemsService; 12 13 @Controller 14 public class ItemsController { 15 16 @Autowired 17 private ItemsService itemsService; 18 19 @RequestMapping("/findItems") 20 public ModelAndView findItems() throws Exception { 21 22 List<ItemsCustom> itemsList = itemsService.findItemsList(null); 23 24 ModelAndView modelAndView = new ModelAndView(); 25 modelAndView.addObject("itemsList", itemsList); 26 modelAndView.setViewName("itemsList"); 27 return modelAndView; 28 } 29 }
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 <!-- 全局setting配置,根据需要添加 --> 8 9 <typeAliases> 10 <package name="com.cy.po"/> 11 </typeAliases> 12 13 <!-- 配置mapper 14 由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。 15 必须遵循:mapper.xml和mapper.java文件同名且在一个目录 16 --> 17 <!-- <mappers></mappers> --> 18 </configuration>
applicationContext-dao.xml:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 16 <!-- 加载db.properties --> 17 <context:property-placeholder location="classpath:db.properties" /> 18 19 <!-- 配置数据源 ,dbcp --> 20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 21 <property name="driverClassName" value="${jdbc.driver}" /> 22 <property name="url" value="${jdbc.url}" /> 23 <property name="username" value="${jdbc.username}" /> 24 <property name="password" value="${jdbc.password}" /> 25 <property name="maxActive" value="30" /> 26 <property name="maxIdle" value="5" /> 27 </bean> 28 29 <!-- 配置sqlSessionFactory --> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <property name="dataSource" ref="dataSource" /> 32 <!-- 加载mybatis的全局配置文件 --> 33 <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> 34 </bean> 35 36 <!-- 配置Mapper扫描器 --> 37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 38 <property name="basePackage" value="com.cy.mapper"></property> 39 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 40 </bean> 41 </beans>
applicationContext-serivice.xml:
1 <!-- 商品管理的service --> 2 <bean id="itemsService" class="com.cy.service.impl.ItemsServiceImpl" />
applicationContext-transaction.xml:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 16 <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类--> 17 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 18 <!-- 数据源 dataSource在applicationContext-dao.xml中配置了 --> 19 <property name="dataSource" ref="dataSource"></property> 20 </bean> 21 22 <!-- 通知 --> 23 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 24 <tx:attributes> 25 <!-- 传播行为 --> 26 <tx:method name="save*" propagation="REQUIRED"/> 27 <tx:method name="delete*" propagation="REQUIRED"/> 28 <tx:method name="insert*" propagation="REQUIRED"/> 29 <tx:method name="update*" propagation="REQUIRED"/> 30 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> 31 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 32 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> 33 </tx:attributes> 34 </tx:advice> 35 36 <!-- aop --> 37 <aop:config> 38 <!-- 切点 在com.cy.service.impl包下的所有类的所有方法(不论参数) --> 39 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.cy.service.impl.*.*(..))"/> 40 </aop:config> 41 </beans>
springmvc.xml:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 16 <!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 --> 17 <context:component-scan base-package="com.cy.controller" /> 18 19 <!-- mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,实际开发时使用mvc:annotation-driven--> 20 <mvc:annotation-driven></mvc:annotation-driven> 21 22 <!-- 视图解析器 解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包 --> 23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24 <property name="prefix" value="/WEB-INF/jsp/"/> 25 <property name="suffix" value=".jsp"/> 26 </bean> 27 </beans>
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>springMVC</display-name> 4 5 <!-- 加载spring容器 --> 6 <context-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> 9 </context-param> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 14 <!-- springmvc前端控制器 --> 15 <servlet> 16 <servlet-name>springmvc</servlet-name> 17 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 18 <init-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:spring/springmvc.xml</param-value> 21 </init-param> 22 </servlet> 23 <servlet-mapping> 24 <servlet-name>springmvc</servlet-name> 25 <url-pattern>*.action</url-pattern> 26 </servlet-mapping> 27 28 <welcome-file-list> 29 <welcome-file>index.html</welcome-file> 30 <welcome-file>index.htm</welcome-file> 31 <welcome-file>index.jsp</welcome-file> 32 <welcome-file>default.html</welcome-file> 33 <welcome-file>default.htm</welcome-file> 34 <welcome-file>default.jsp</welcome-file> 35 </welcome-file-list> 36 </web-app>
jsp页面和db.properties和log4j.properties和之前的配置一样;
测试:
浏览器访问http://localhost:8080/springMVC/findItems.action