介绍
下载SSM整合包
整合目标:
Spring和SpringMVC是一个整体,共用一个bean容器,所以我们不需要整合Spring和SpringMVC。让Spring管理MyBatis的SqlSessionFactory对象,生成SqlSessionFctory时,会读取MyBatis的核心配置文件。mybatis和spring剩下的整合,就是通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册生成的代理对象。生成代理对象时,需要SqlSessionFactory对象。
第一步:整合dao层 让Spring管理SqlSessionFctory对象。 mybatis和spring整合,通过spring管理mapper接口。 使用mapper的扫描器自动扫描mapper接口在spring中进行注册生成的代理对象。生成代理对象时,需要SqlSessionFactory对象。
第二步:整合service层 通过注解方式把Dao层对象注入到Service接口的实现类中。 在Servicr层中实现事务控制。
第三步:整合springmvc 由于springmvc是spring的模块,不需要整合。
整合SSM
导入所有整合jar包。我们这里通过驱动式整合,我们要查询数据库里面的所有商品信息。
用到的基础类:
包名cn.domarvel.po:
Item.java:
public class Item {
private Integer id;
private String name;
private Double price;
private String detail;
private byte[] pic;
private Date createtime;
包名cn.domarvel.pocustom(这个是自定义po类,一般我们从数据库查数据用的就是poCustom,都是为了扩展性!!遵循这种规范比较好!!!)
ItemCustom.java:
public class ItemCustom extends Item{
}
包名cn.domarvel.dao
ItemMapper.java:
public interface ItemMapper {
public List<ItemCustom>
findAllItems();
}
ItemMapper.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="cn.domarvel.pocustom.ItemCustom">
<select id="findAllItems" resultType="cn.domarvel.po.ItemCustom">
SELECT * FROM items
</select>
</mapper>
包名cn.domarvel.service
定义service接口
public interface ItemDaoService {
public List<ItemCustom>
findAllItems();
}
包名cn.domarvel.service.impl
创建service接口的实现类
@Service(
"itemDaoServiceImpl")
public class ItemDaoServiceImpl implements ItemDaoService{
@Resource(name=
"itemMapper")
private ItemMapper itemMapper;
/**
* 查询所有的商品
* @return
* @author FireLang
*/
@Override
public List<ItemCustom>
findAllItems() {
return itemMapper.findAllItems();
}
}
包名cn.domarvel.vo(当接受前端数据时就用这个,它一般是对pocustom或者其它类别的pocustom封装,以防止数据不够用,为什么不直接修改Item.java??因为这个Item类通常都是不修改的,你会越修改越乱。这是规范。你可以不准守都可以实现,但是准守总是有好处没坏处!)
ItemQueryVo.java
public class ItemQueryVo {
private ItemCustom itemCustom;
public ItemCustom
getItemCustom() {
return itemCustom;
}
public void setItemCustom(ItemCustom itemCustom) {
this.itemCustom = itemCustom;
}
}
包名cn.domarvel.web.controller
ItemHandler.java
/**
* 商品查询的控制器
* @author FireLang
*
*/
@Controller
public class ItemHandler {
@Resource(name=
"itemDaoServiceImpl")
private ItemDaoService itemDaoService;
@RequestMapping(
"/item/showItems")
public String
showItems(Model model){
model.addAttribute(
"itemsList", itemDaoService.findAllItems());
return "/WEB-INF/content/showItems.jsp";
}
}
WEB-INF下的content
showItems.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/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>Insert title here
</title>
</head>
<body>
<h3>欢迎来到首页
</h3>
<c:forEach items="${requestScope.itemsList }" var="item">
<p>商品名字=${item.name}---商品价格=${item.price }---生产日期=
<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>---描述=${item.detail }
</p>
</c:forEach>
</body>
</html>
整合Spring+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>
</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 ">
<context:property-placeholder location="classpath:Spring/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="400"/>
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="60" />
<property name="acquireIncrement" value="5"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="breakAfterAcquireFailure" value="false"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis/sqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.domarvel.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
db.properties文件内容如下:
jdbc
.username=root
jdbc
.password=
jdbc
.driver=
com.mysql.jdbc.Driver
jdbc
.url=jdbc:mysql://localhost:
3306/mybatis?&useServerPrepStmts=true&cachePrepStmts=true
配置事务(applicationContext-tx.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="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txadvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.domarvel.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
applicationContext.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 ">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="cn.domarvel.web.controller,cn.domarvel.service.impl"/>
</beans>
log4j.properties
### direct log messages to stdout ###
log4j
.appender.stdout=org
.apache.log4j
.ConsoleAppender
log4j
.appender.stdout.Target=System
.err
log4j
.appender.stdout.layout=org
.apache.log4j
.PatternLayout
log4j
.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %
5p %c{
1}:%L - %m%n
### direct messages to file mylog.log ###
log4j
.appender.file=org
.apache.log4j
.FileAppender
log4j
.appender.file.File=c:\mylog
.log
log4j
.appender.file.layout=org
.apache.log4j
.PatternLayout
log4j
.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %
5p %c{
1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j
.rootLogger=info, stdout
最后就是web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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/applicationContext*.xml
</param-value>
</init-param>
<load-on-startup>1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC
</servlet-name>
<url-pattern>/
</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>
好了,基本配置就完成了,可以尝试运行了。
SSM整合项目代码下载
整合完毕后你可能会疑惑,为什么我只加载了SpringMVC的配置文件,而没有加载Spirng的配置文件就能够运行整个项目,成功进行bean的管理。请查看这篇博客帮你答疑!!!