SSM组成
- springmvc(主要进行web层的请求控制;controller处理请求;数据返回)
- mybatis(数据库持久化操作)
- spring(bean的管理,aop切面的编程)
整合步骤
1. 新建maven项目
修改pom.xml文件,添加依赖和插件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 加入你的mybaits的依赖 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.17.1-GA</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<!-- 导入spring 的依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- mybatis和spring要进行整合的时候使用的包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--spring中进行事务 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--spring jdbctemplete -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- 这个是springaop的使用注解方式 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<!-- 导入你springmvc -->
<!--spring提供的测试 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--springmvc的包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--一个springmvc项目启动的时候需要的辅助工具 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>s01</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--控制tomcat端口号 -->
<configuration>
<port>8080</port>
<!-- 发布到tomcat后的名称 -->
<!--/ 相当于把项目发布成ROOT -->
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<!-- <finalName>love</finalName> <server>tomcat7</server> -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<!--指定你的jdk地址 -->
<executable>
D:\Java\jdk1.8.0_65\bin\javac.exe
</executable>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2. 相关的properties文件
log4j.properties
日志管理
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?serverTimezone=GMT%2B8&useUnicode=true&characterEncodig=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
3. mybatis配置
sqlconfig.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>
<typeAliases>
<package name="com.ssm01.pojo"/>
</typeAliases>
</configuration>
4. spring+mybatis整合
4.1 applicationContext-dao.xml
spring整合dao层,主要配置数据源和mapper
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--加载配置文件,数据库属性文件-->
<context:property-placeholder location="classpath:*.properties"></context:property-placeholder>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"><value>${jdbc.driver}</value></property>
<property name="jdbcUrl"><value>${jdbc.url}</value></property>
<property name="user"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<!--mybatis和数据源进行连接,数据源传入给sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--加载数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis的全局配置文件-->
<property name="configLocation" value="classpath:sqlconfig.xml"></property>
</bean>
<!-- 扫描mapper包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径 -->
<property name="basePackage" value="com.ssm01.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
4.2 spring整合service层
applicationContext-service.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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.ssm01.service.impl"></context:component-scan>
</beans>
4.3 事务
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 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="del*" 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 对service进行切面管理,也就是所有的service中的所有方法的,所有属性进行事务管理-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm01.service.*.*.*(..))"/>
</aop:config>
</beans>
5. 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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描你的controller包 -->
<context:component-scan base-package="com.ssm01.controller"></context:component-scan>
<!--2. 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的
RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--3. 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
6. web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置让他去扫描你的spring里边的那几个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 需要配置一个过滤器,过滤你的前端请求在中的所有的数据转换成utf-8;解决了乱码问题 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置你的前端控制器 -->
<!-- 配置springmvc的前端控制器 -->
<servlet>
<servlet-name>ssm01</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm01</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
7. 进行业务操作
7.1 实体类
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class MyUser {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
}
7.2 mapper层(对应dao层),使用注解方式
public interface MyUserMapper {
@Select("select * from myuser")
List<MyUser> selectAll() throws Exception;
@Select("select * from myuser where id=#{id}")
MyUser selectOne(int id) throws Exception;
@Delete("delete from myuser where id=#{id}")
void deleteUser(int id) throws Exception;
@Insert("insert into myuser(username,birthday,address,sex) values(#{username},#{birthday},#{address},#{sex})")
void insertUser(MyUser user) throws Exception;
@Update("update myuser set username=#{username},address=#{address},sex=#{sex} where id=#{id}")
void updateUser(MyUser user) throws Exception;
}
7.3 service层
package com.ssm01.service;
import java.util.List;
import com.ssm01.pojo.MyUser;
public interface MyUserService {
List<MyUser> selectAll() throws Exception;
MyUser selectOne(int id) throws Exception;
void deleteUser(int id) throws Exception;
void insertUser(MyUser user) throws Exception;
void updateUser(MyUser user) throws Exception;
}
接口实现类
package com.ssm01.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm01.mapper.MyUserMapper;
import com.ssm01.pojo.MyUser;
import com.ssm01.service.MyUserService;
@Service
public class MyUserServiceImpl implements MyUserService {
@Autowired
MyUserMapper mapper;
@Override
public List<MyUser> selectAll() throws Exception {
// TODO Auto-generated method stub
return mapper.selectAll();
}
@Override
public void deleteUser(int id) throws Exception {
// TODO Auto-generated method stub
mapper.deleteUser(id);
}
@Override
public void insertUser(MyUser user) throws Exception {
// TODO Auto-generated method stub
mapper.insertUser(user);
}
@Override
public void updateUser(MyUser user) throws Exception {
// TODO Auto-generated method stub
mapper.updateUser(user);
}
@Override
public MyUser selectOne(int id) throws Exception {
// TODO Auto-generated method stub
return mapper.selectOne(id);
}
}
7.4 controller层
import java.util.ArrayList;
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.ssm01.pojo.MyUser;
import com.ssm01.service.MyUserService;
@Controller
public class MyUserController {
@Autowired
MyUserService myUserService;
@RequestMapping("/myuserlist.do")
public ModelAndView myuserlist() throws Exception {
List<MyUser> list = myUserService.selectAll();
ModelAndView mav = new ModelAndView();
mav.addObject("list", list);
mav.setViewName("myuserlist");
return mav;
}
}
8. 测试是否整合成功
网页myuserlist.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 uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<center><h2>用户信息展示2</h2></center>
<script type="text/javascript">
function queryMyUser(){
document.myuserForm.action = "${pageContext.request.contextPath }/queryMyusers.do";
document.myuserForm.submit();
}
function updateMyUser(){
document.myuserForm.action = "${pageContext.request.contextPath }/updateMyusers.do";
document.myuserForm.submit();
}
function deleteMyUser(){
document.myuserForm.action = "${pageContext.request.contextPath }/deleteMyusers.do";
document.myuserForm.submit();
}
</script>
<center>
<td><input type="button" value="批量查询" onclick="queryMyUser()"></td>
<td><input type="button" value="批量修改" onclick="updateMyUser()"></td>
<td><input type="button" value="批量删除" onclick="deleteMyUser()"></td>
</center>
<form name="myuserForm" action="myuserList.do" method="post">
<table width=100% border=1>
<tr>
<th>批量选择</th>
<th>用户id</th>
<th>用户姓名</th>
<th>用户生日</th>
<th>用户性别</th>
<th>用户地址</th>
<th>操作</th>
</tr>
<!-- 从第二行进行循环取值 -->
<c:forEach items="${list}" var="myuser">
<tr>
<td><input type="checkbox" value="${myuser.id}" name="ids"></td>
<td>${myuser.id }</td>
<td>${myuser.username }</td>
<td>${myuser.birthday }</td>
<td>${myuser.sex }</td>
<td>${myuser.address}</td>
<td><a href="up.do?id=${myuser.id}">修改</a>
<a href="del.do?id=${myuser.id}">删除</td>
</tr>
</c:forEach>
</table>
<a href="add.do">添加用户</a>
</form>
</body>
</html>
使用tomcat7插件运行,浏览器截图: