SSM整合
(一) SSM框架整合思路的介绍和分析
介绍:SSM(Spring+SpringMVC+MyBatis)整合,就是三个框架协同开发。
Spring整合Mybatis 就是将Mybatis核心配置文件当中数据源的配置,事务的管理,以及工厂的配置,Mapper接口的实现类等交给Spring管理。
Spring整合SpringMVC,就是在web.xml当中添加监听器,当服务器启动,监听器触发,监听器执行了Spring的核心配置文件,核心配置文件被加载。
整合核心步骤:
Spring 基础框架单独运行
SpringMVC 框架单独运行
Spring 整合SpringMVC 框架
Spring 整合Mybatis 框架
测试SSM 整合结果
(二)准备SSM整合环境
1 创建数据库和表结构
CREATE TABLE `role` (
`roleid` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键值',
`rolename` varchar(32) DEFAULT NULL COMMENT '角色名称',
`roledes` varchar(32) DEFAULT NULL COMMENT '角色描述',
PRIMARY KEY (`roleid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2 创建 Maven 工程
3 导入核心坐标
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-version>5.2.5.RELEASE</spring-version>
<mybatis-version>3.4.6</mybatis-version>
</properties>
<dependencies>
<!--mybatis相关包-->
<!--mysql的驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--mybatis核心-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--日志包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!--spring相关的-->
<!--springIOC包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--织入器包:-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!--springmvc依赖:-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--解析器包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
4 编写数据模型实体类
/**
* @author offcn
* 角色实体类
**/
public class Role implements Serializable {
private Integer roleid;//角色id
private String rolename;//角色名称
private String roledes;//角色的描述
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getRoledes() {
return roledes;
}
public void setRoledes(String roledes) {
this.roledes = roledes;
}
@Override
public String toString() {
return "Role{" +
"roleid=" + roleid +
", rolename='" + rolename + '\'' +
", roledes='" + roledes + '\'' +
'}';
}
}
5 编写持久层接口
/**
* @author offcn
**/
public interface RoleMapper {
Role findById(Integer id);
List<Role> findAll();
void insert(Role role);
void update(Role role);
void delete(Integer id);
}
6 编写持久层接口对应的sql配置文件
<?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="com.offcn.mapper.RoleMapper">
<resultMap id="BaseResultMap" type="com.offcn.pojo.Role" >
<id column="roleid" property="roleid" jdbcType="INTEGER" />
<result column="rolename" property="rolename" jdbcType="VARCHAR" />
<result column="roledes" property="roledes" jdbcType="VARCHAR" />
</resultMap>
<!-- Role findById(Integer id);-->
<select id="findById" resultMap="BaseResultMap">
select * from role where roleid =#{roleid}
</select>
<!-- List<Role> findAll();-->
<select id="findAll" resultMap="BaseResultMap">
select * from role
</select>
<!--void insert(Role role);-->
<insert id="insert" parameterType="com.offcn.pojo.Role" >
insert into role(rolename,roledes) values(#{rolename},#{roledes})
</insert>
<!-- void update(Role role);-->
<update id="update" parameterType="com.offcn.pojo.Role">
UPDATE role
SET
roleid = #{roleid} ,
rolename = #{rolename} ,
roledes = #{roledes}
WHERE
roleid = #{roleid}
</update>
<!--void delete(Integer id);-->
<delete id="delete" parameterType="int">
delete from role where roleid=#{roleid}
</delete>
</mapper>
7 编写业务层接口
/**
* @author offcn
**/
public interface RoleService {
Role findById(Integer id);
List<Role> findAll();
void insert(Role role);
void update(Role role);
void delete(Integer id);
}
8 编写业务层接口实现类
/**
* @author offcn
**/
public class RoleServiceImpl implements RoleService {
@Override
public Role findById(Integer id) {
return null;
}
@Override
public List<Role> findAll() {
return null;
}
@Override
public void insert(Role role) {
}
@Override
public void update(Role role) {
}
@Override
public void delete(Integer id) {
}
}
(三)SSM整合步骤
1 Spring 框架在 web 工程中独立运行
1.1 创建 Spring 配置文件beans_core.xml并导入约束
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描器-->
<context:component-scan base-package="com.offcn"></context:component-scan>
</beans>
1.2 使用注解配置业务层
@Service("roleService") //使用注解配置业务层对象
public class RoleServiceImpl implements RoleService {
@Override
public Role findById(Integer id) {
return null;
}
...
}
1.3 测试 Spring 独立运行
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
private RoleService roleService;
@Test
public void testIOC(){
System.out.println(roleService);
}
}
//运行结果: com.offcn.service.RoleServiceImpl@478190fc
//说明: SpringIOC 功能能够正常使用
2 SpringMVC 在 web 工程中运行
2.1 在 web.xml 中配置核心控制器(DispatcherServlet)
<!--配置前端控制器: -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc的配置文件:-->
<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的映射路径-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.2 编写 SpringMVC 的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解扫描器-->
<context:component-scan base-package="com.offcn.controller"></context:component-scan>
<!--处理器配置-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器:-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.3 编写 Controller
/**
* @author offcn
**/
@Controller
@RequestMapping("role")
public class RoleController {
@RequestMapping("findRole")
@ResponseBody
public Role findRole(Integer roleid){
Role role = new Role();
role.setRoleid(roleid);
role.setRolename("管理员");
role.setRoledes("管理整个网站");
return role;
}
}
2.4 编写index.jsp测试
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>角色详情页面</title>
</head>
<body>
<h2>根据id唯一性查询角色信息</h2>
<form action="/role/findRole" method="post">
id: <input name="roleid" type="text" ><br/>
<button>查询</button>
</form>
</body>
</html>
3 整合 Spring 和 SpringMVC:web.xml中添加监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置了一个监听器: ServletContext-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4 Spring整合MyBatis配置
4.1 Spring 接管 MyBatis 的 Session 工厂
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/blog
jdbc.username=root
jdbc.password=root
<!--数据源-->
<context:property-placeholder location="classpath:dbConfig.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--mybatis的核心工厂对象-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--别名-->
<property name="typeAliasesPackage" value="com.offcn.pojo"/>
<!--mapper 文件的位置-->
<property name="mapperLocations" value="classpath:com/offcn/mapper/*.xml"/>
<!--配置分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
4.2 配置自动扫描所有 Mapper 接口和文件
<!--指定扫描器: 扫描mapper接口,生成代理类: -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定扫描的包位置-->
<property name="basePackage" value="com.offcn.mapper"/>
</bean>
4.3 配置 Spring 的事务
<!--事务管理器平台-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入一个数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解式事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
(四)测试 SSM 整合结果
1 编写测试jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>角色详情页面</title>
</head>
<body>
<h2>根据id唯一性查询角色信息</h2>
<form action="/role/findRole" method="post">
id: <input name="roleid" type="text" ><br/>
<button>查询</button>
</form>
<hr/>
<a href="/role/findAll">查询所有角色</a>
<hr/>
<h2>添加角色</h2>
<form action="/role/insert" method="post">
角色名称: <input name="rolename" type="text" ><br/>
角色描述: <input name="roledes" type="text" ><br/>
<button>添加</button>
</form>
<hr/>
<h2>更新角色</h2>
<form action="/role/update" method="post">
id: <input name="roleid" type="text" ><br/>
角色名称: <input name="rolename" type="text" ><br/>
角色描述: <input name="roledes" type="text" ><br/>
<button>更新</button>
</form>
<hr/>
<h2>删除角色</h2>
<form action="/role/delete" method="post">
id: <input name="roleid" type="text" ><br/>
<button>删除</button>
</form>
</body>
</html>
2 修改控制器中的方法和业务层当中方法
package com.offcn.controller;
import com.offcn.pojo.Role;
import com.offcn.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author offcn
**/
@Controller
@RequestMapping("role")
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("findRole")
@ResponseBody
public Role findRole(Integer roleid){
Role role = roleService.findById(roleid);
return role;
}
@RequestMapping("findAll")
@ResponseBody
public List<Role> findAll(){
List<Role> listRoles = roleService.findAll();
return listRoles;
}
@RequestMapping("insert")
@ResponseBody
public String insert(Role role){
try {
roleService.insert(role);
return "ok";
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
@RequestMapping("update")
@ResponseBody
public String update(Role role){
try {
roleService.update(role);
return "ok";
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
@RequestMapping("delete")
@ResponseBody
public String delete(Integer roleid){
try {
roleService.delete(roleid);
return "ok";
} catch (Exception e) {
e.printStackTrace();
}
return "fail";
}
}
/**
* @author offcn
**/
@Service("roleService")
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
@Override
public Role findById(Integer id) {
return roleMapper.findById(id);
}
@Override
public List<Role> findAll() {
return roleMapper.findAll();
}
@Override
public void insert(Role role) {
roleMapper.insert(role);
}
@Override
public void update(Role role) {
roleMapper.update(role);
}
@Override
public void delete(Integer id) {
roleMapper.delete(id);
}
}
3 测试运行并展示运行结果
<!--引入外部的Tomcat7插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
SSM集成
主要内容
-
SSM集成
-
登陆
-
拦截器
-
退出
-
客户列表
一、创建一个maven web工程
把****pom.xml中packaging改成war
添加web工程所需要的webapp 和WEB-INF、web.xml
一、引入依赖(Mybatis依赖,spring依赖,springmvc依赖)
引入依赖
<properties>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<!--导入junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--导入spring的context坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--导入Jdbc模块依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--导入Mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--导入C3P0连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!--mybatis-Spring适配包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mybatis orm框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!--springmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--springmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--解析json依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--文件上传依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- jsp相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
把java和resources中的资源添加到classpath中
<build>
<resources>
<resource>
<directory>src\main\java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src\main\resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
使用maven tomcat插件
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--指定编码格式-->
<uriEncoding>utf-8</uriEncoding>
<!--指定项目启动后的访问路径-->
<path>/</path>
<!--指定访问端口号-->
<port>8080</port>
</configuration>
</plugin>
</plugins>