springMVC框架--SSM整合开发(二)
2016-10-20 14:24
549 查看
1 SSM整合开发工程搭建
1.1 整合思路
在mybatis和spring整合的基础上 添加springMVC。spring要管理springMVC编写的Handler(controller)、mybatis的SqlSessionFactory、mapper(dao)接口
第一步:整合dao,spring和mybatis整合
第二步: 整合service,spring管理service接口,service中可以调用spring容器中dao(mapper)
第三步:整合controller,spring管理controller接口,在controller调用service
1.2 所需jar包
mybatis:3.2.7spring:3.2.0
mybatis的jar
mybatis和spring整合包
spring的所有jar包(包括 springmvc的包)
数据库驱动包
log4j日志
1.3 工程结构
1.3.1 相关配置文件
applicationContext-dao.xml---配置数据源、SqlSessionFactory、mapper扫描器、加载mybatis配置文件applicationContext-service.xml---配置service接口管理
applicationContext-transaction.xml--事务管理
sprintmvc.xml---springmvc的配置文件,配置处理器映射器、适配器、视图解析器
SqlMapConfig.xml---mybatis的配置文件,配置别名、settings(延迟加载,二级缓存相关配置)、mapper扫描器(和spring整合后mapper扫描器由spring管理)
Web.xml---配置前端控制器,加载spring相关配置文件
db.properties---数据库配置文件
log4j.properties---日志文件
1.3.2 applicationContext-dao.xml
配置mybatis的数据源、sqlSessionFactory、mapper扫描器、加载sqlMapConfig.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">
<!-- 加载数据库配置文件 -->
<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="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>
<!--
MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描包的路径
如果要扫描多个包,中间使用半角逗号分隔
要求mapper.xml和mapper.java同名且在同一个目录
-->
<property name="basePackage" value="cn.itcast.ssm.mapper"/>
<!-- 使用sqlSessionFactoryBeanName -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
1.3.3 applicationContext-service.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"> <!-- 商品管理的service -->
<bean id="itemsService"class="cn.itcast.ssm.service.impl.ItemsServiceImpl"/>
</beans>
1.3.4 applicationContext-transation.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">
<!-- 使用声明式事务配置,可以有效规范代码 -->
<!-- 事务管理器 -->
<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="update*"propagation="REQUIRED"/>
<tx:method name="delete*"propagation="REQUIRED"/>
<tx:method name="find*"propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*"propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*"propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(*cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config>
</beans>
1.3.5 前端控制器配置(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"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"version="2.5">
<!--配置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>
<display-name>springMVCfirst</display-name>
<!--前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件的地址
如果不配置contextConfigLocation,
默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml
-->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
可以配置/ ,此工程所有请求全部由springmvc解析,此种方式可以实现RESTful方式,需要特殊处理对静态文件的解析不能由springmvc解析
可以配置*.do或*.action,所有请求的url扩展名为.do或.action由springmvc解析,此种方法常用
不可以/*,如果配置/*,返回jsp也由springmvc解析,这是不对的。
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--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>
<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.3.1 配置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"> <!-- 使用spring组件扫描 -->
<context:component-scan base-package="cn.itcast.ssm.controller"/>
<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- <mvc:annotation-drivenconversion-service="conversionService">
</mvc:annotation-driven>-->
<!-- 注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 配置视图解析器要求将jstl的包加到classpath -->
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
1.4 商品列表开发
1.4.1 需求
查询商品列表1.4.2 pojo
功能描述:根据条件查询商品信息,返回商品列表商品类Item.java
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name){
this.name = name == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Floatprice) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Datecreatetime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(Stringdetail) {
this.detail = detail == null ? null : detail.trim();
}
}
商品信息的扩展类ItemCustom.java
public class ItemsCustom extends Items{
//可以扩展其他属性
}
商品信息的包装类:ItemsQueryVo.java
public class ItemsQueryVo {
//商品信息
private ItemsCustom itemsCustom;
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public voidsetItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom= itemsCustom;
}
}
1.4.3 mapper
商品信息接口ItemesMapperCustom.java/**
* 查询商品列表
* @author xia
*
*/
public interface ItemsMapperCustom {
// 商品查询列表
public List<ItemsCustom>findItemsList(ItemsQueryVo itemsQueryVo)
throwsException;
}
1.4.4 mapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itcast.ssm.mapper.ItemsMapperCustom">
<!-- 商品查询的sql片段
建议是以单表为单位定义查询条件
建议将常用的查询条件都写出来
-->
<sql id="query_items_where">
<if test="itemsCustom!=null">
<if test="itemsCustom.name!=nulland itemsCustom.name!=''">
and name like '%${itemsCustom.name}%'
</if>
<if test="itemsCustom.id!=null">
and id = #{itemsCustom.id}
</if>
</if>
</sql>
<!-- 商品查询
parameterType:输入查询条件
-->
<select id="findItemsList" parameterType="itemsQueryVo"
resultType="cn.itcast.ssm.po.ItemsCustom">
SELECT * FROM items
<where>
<include refid="query_items_where"/>
</where>
</select>
</mapper>
1.4.5 service
public class ItemsServiceImpl implements ItemsService {//注入mapper
@Autowired
private ItemsMapperCustom itemsMapperCustom;
//商品查询列表
public List<ItemsCustom>findItemsList(ItemsQueryVo itemsQueryVo)
throws Exception {
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
1.4.6 在applicationContext-service.xml中配置service
<!-- 商品管理 的service --><bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"/>
1.4.7 controller
@Controller//定义url的根路径,访问时根路径+方法的url
@RequestMapping("/items")
public class ItemsController {
//注入service
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequestrequest) throws Exception {
System.out.println(request.getParameter("id"));
//调用service查询商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
// 指定逻辑视图名
modelAndView.setViewName("itemsList");
return modelAndView;
}
}
1.4.8 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 }/items/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="${itemsList }" var="item">
<tr>
<td>${item.name}</td>
<td>${item.price}</td>
<td><fmt:formatDate value="${item.createtime}"
pattern="yyyy-MM-ddHH:mm:ss" />
</td>
<td>${item.detail}</td>
<td><a
href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
相关文章推荐
- Web框架梳理:第五章:SpringMVC、SpringMVC注解开发、SSM:整合
- JavaEE_Mybatis_SpringMVC_框架整合_lesson1_简单的SSM框架搭建_使用原始Dao开发方式进行测试
- SSM框架(一)之SSM框架整合(Spring,SpringMVC,MyBatis)
- SSM(Spring,SpringMVC,MyBatis)整合开发流程
- SSM框架搭建详细过程,springmvc、spring、mybatis整合
- SSM框架之SpringMVC(1)--Mybatis和SpringMVC的整合
- springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven SSM
- SpringMVC、MyBatis框架整合开发项目的简单说明(使用框架说明)
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- SSM框架(二)--Spring、SpringMVC和MyBatis整合(详细教程)
- springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven SSM
- SSM(Spring、SpringMVC和Mybatis)框架整合404问题解决
- ssm框架学习03--springmvc和mybatis整合
- 基于maven搭建ssm开发框架(1) SpringMVC和spring的整合
- SSM框架——详细整合教程(Spring 4.0.2+SpringMVC 4.0.2+MyBatis3.2.6)
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- maven整合:最新ssm框架实现spring4.2,springmvc4.2,mybatis3.2
- 基于maven搭建ssm开发框架(1) mybatis和spring的整合
- 基于maven搭建ssm开发框架(1) Service层整合
- springmvc4 mybatis 整合 框架源码 bootstrap html5 mysql oracle sqlsever spring SSM