SSM整合案例

一、首先我们需要导入相关的架包,这儿直接导入所需要的所有的jar包

asm-3.3.1.jar
c3p0-0.9.5.1.jar
cglib-2.2.2.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
ehcache-core-2.6.5.jar
hamcrest-core-1.3.jar
javassist-3.17.1-GA.jar
jstl-1.2.jar
junit-4.12.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mchange-commons-java-0.2.10.jar
mybatis-3.2.7.jar
mybatis-ehcache-1.0.2.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.7-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.3.9.RELEASE.jar
spring-aspects-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-context-support-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-jdbc-4.3.9.RELEASE.jar
spring-orm-4.3.9.RELEASE.jar
spring-test-4.3.9.RELEASE.jar
spring-tx-4.3.9.RELEASE.jar
spring-web-4.3.9.RELEASE.jar
spring-webmvc-4.3.9.RELEASE.jar

二、首先我们创建需要的po类:Items.java,再创建po类的扩展类,ItemsCustomer.java,再次创建PO的包装对象 :ItemsCustomer.java

三、创建mapper类以及mapper.xml文件:ItemsMapperCustomer.xml


四、创建好po类即相关类后,我们就需要配置mybatis的配置文件:SqlMapConfig.xml,ItemsMapperCustomer.java

<?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>

<!-- 设置应用log4j日志-->

<settings>
<setting name="logImpl" value="LOG4J" />
</settings>

<!-- 配置别名-->
<typeAliases>
<package name="xb.cn.ssm.po" />
</typeAliases>
</configuration>

五、创建Spring的配置文件,由于后面需要用到更多的Spring的配置文件,所以注意创建配置文件时注意它的命名:

5.1、创建applicationContext-dao.xml文件并配置

<?xml version="1.0" encoding="UTF-8"?>
<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 ">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:c3p0_db.properties" />
<!-- 配置数据源 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>


<!-- sqlSessionFactory的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 也是配置mapper,直接从mapper包中批量扫描mapper接口,自动创建 代理对象,并且在Spring容器中 注册遵循的规范,
在mapper包中.java文件和.xml的文件名 
必须相同。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xb.cn.ssm.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>

六,配置完后便开始实现service业务层,创建service接口以及实现类。

package xb.cn.ssm.service;
import java.util.List;
import xb.cn.ssm.po.ItemsCustomer;
import xb.cn.ssm.po.ItemsQueryVo;
//商品管理service
public interface ItemsService {
// 商品查询列表
public List<ItemsCustomer> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

package xb.cn.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import xb.cn.ssm.mapper.ItemsMapperCustomer;
import xb.cn.ssm.po.ItemsCustomer;
import xb.cn.ssm.po.ItemsQueryVo;
import xb.cn.ssm.service.ItemsService;


//商品查询的service实现类
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsMapperCustomer itemsMapperCustomer;
@Override
public List<ItemsCustomer> findItemsList(ItemsQueryVo itemsQueryVo)
throws Exception {
return itemsMapperCustomer.findUserList(itemsQueryVo);
}
}

七、创建完service后,便创建service的配置文件:applicationContext-service.xml,也是由Spring管理.

<?xml version="1.0" encoding="UTF-8"?>
<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 ">
<!-- 注册ItemsService -->
<bean class="xb.cn.ssm.service.impl.ItemsServiceImpl" id="itemsService"></bean>
</beans>

八、创建完service的配置文件之后便在创建springMVC中的Controller配置文件:applicationContext-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="transactionManager">
<!-- 数据源 dataSource在applicationContext-dao.xml中配置 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdivce" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="insert*" 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>
<aop:advisor advice-ref="txAdivce"
pointcut="execution(* xb.cn.ssm.service.ipml.*.*(..))" />
</aop:config>
</beans>

九、创建Controller完后,创建SpringMVC的配置文件:springmvc.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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,controller等 -->
<context:component-scan base-package="xb.cn.ssm.controller"></context:component-scan>
<!-- 视图解析器 -->
<!-- 解析jsp文件,默认使用jstl标签 classPath下要有jstl的包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 注解映射器  -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>

十、所有的配置文件都配置完后便实现Controller的实现类:ItemsController.java

package xb.cn.ssm.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 xb.cn.ssm.po.ItemsCustomer;
import xb.cn.ssm.service.ItemsService;
//商品的Controller
@Controller
public class ItemsController {
// 商品的查询
@Autowired
private ItemsService itemsService;
@RequestMapping("/findItems")
public ModelAndView findItems() throws Exception {
List<ItemsCustomer> itmesList = itemsService.findItemsList(null);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request的setAttribute,在jsp页面中通过这个itemsList来区数据
modelAndView.addObject("itemsList", itmesList);
// 指定视图
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
}

十一、上面完成之后,基本上就完成了,最后就是SpringMVC的视图,创建jsp文件。itemsList.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 }/item/queryItem.action"
method="post">
查询条件:
<table width="100%" border="1" >
<tr  align="center">
<td><input type="submit" value="查询" /></td>
</tr>
</table>
商品列表:
<table width="100%"  align="center" border="1" >
<tr  align="center">
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr style="border: 1px solid gray;" align="center">
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}"
pattern="yyyy-MM-dd HH:mm:ss" /></td>
<td>${item.detail }</td>
<td><a
href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>

</c:forEach>
</table>
</form>
</body>
</html>

提示下:这儿创建po类的扩展类以及包装类视为了项目的更好的扩展性。

上面就是基本的实现SSM的完整的一个查询商品的小列子。注意在还得添加c3p0的配置文件以及log4j的日志文件。新手勿喷!!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值