SSM整合开发
# SSM整合开发步骤 SSM = SpringMVC + Spring + Mybatis
1. 引入依赖
spring springmvc mybatis mybatis-spring mysql druid log4j servlet-api jstl fastjson(需要做ajax处理引入)
2. SM整合 = Spring + Mybatis
1). 建表
2). 实体
3). DAO接口
4). Mapper配置文件
5). Service接口
6). Service实现类 @Service @Transaction 注入DAO对象
7). 引入spring.xml编写spring整合mybatis配置
a. 开启注解扫描 <context:component-scan base-package="com.baizhi" />
b. 创建数据源对象DruidDataSource 注入driverClassName url username password
c. 创建sqlSessionFactory SqlSessionFactoryBean 注入dataSource 注入mapper配置文件位置 注入别名typeA,Package
d. 创建DAO MapperScannerConfigurer 注入SqlSessionFactoryBeanName 注入DAO接口所在的包
e. 创建事务管理器 DataSourceTransactionManager 注入dataSource
f. 开启注解式事务驱动 <tx:annotation-driven transaction-manager="事务管理器在工厂中的唯一标识"
8). 测试Service方法调用
3. SS整合 = Spring + SpringMVC无缝整合
1). 配置web.xml
a. 启动工厂监听器 ContextLoaderListener
b. 配置工厂配置文件 spring.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value> <!--指定spring配置文件的位置-->
</context-param>
<!--上面的两句话相当于把工厂创建出来了-->
c. 配置springmvc核心的Servlet DispatcherServlet url-pattern /
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>xxx.xxxx.xxx.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>.....
d. 配置springmvc接收post请求中文乱码 CharacterEncodingFilter url-pattern /* init-param encoding UTF-8
2). 引入springmvc.xml配置
a. 开启注解扫描 <context:component-scan base-package="com.baizhi.controller" />
b. 配置处理器映射器 和处理器适配器
<mvc:annotation-driven/>
c. 配置视图解析器 InternalResourceViewResolver 注入前缀 / 注入后缀 .jsp
3). 开发controller 加入@Controller @RequestMapping("") 注入xxxService(工厂已经加载,工厂中的service对象可以注入)
4. 服务器部署进行访问测试即可
- 在pom.xml文件中引入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
SM整合 = Spring + Mybatis 整合
- 建表
- 构建实体类
public class User {
private String id;
private String name;
private Integer age;
private Date bir;
}
- 构建DAO接口
public interface UserDAO {
void save(User user);
List<User> findAll();
}
- 编写对应的mapper配置文件
<?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.baizhi.dao.UserDAO">
<insert id="save" parameterType="User">
insert into t_user values(#{id}, #{name}, #{age}, #{bir})
</insert>
<select id="findAll" resultType="User">
select
id, name, age, bir
from t_user
</select>
</mapper>
- 构建Service接口
public interface UserService {
void save(User user);
List<User> findAll();
}
- 创建Service接口的实现类
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserDAO userDAO;
@Override
public void save(User user) {
userDAO.save(user);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<User> findAll() {
return userDAO.findAll();
}
}
- 编写spring配置文件spring.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
<context:component-scan base-package="com.baizhi"/>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/lb?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/baizhi/mapper/*.xml"/>
<property name="typeAliasesPackage" value="com.baizhi.eneity"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.baizhi.dao"/>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
- 测试Service方法的调用
public class TestUserService {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) context.getBean("userService");
userService.findAll().forEach(user -> {
System.out.println("user = " + user);
});
}
}
SS整合 = Spring + SpringMVC 无缝整合
- 配置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>
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>charset</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>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 引入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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.baizhi.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 开发控制器
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("findAll")
public String findAll(HttpServletRequest request){
List<User> users = userService.findAll();
request.setAttribute("users", users);
return "findAll";
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用来展示所有用户信息</title>
</head>
<body>
<h1>展示用户列表</h1>
<c:forEach items="${requestScope.users}" var="user">
${user.id} ==== ${user.name} ==== ${user.age} ==== ${user.bir} <br>
</c:forEach>
</body>
</html>
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("save")
public String save(User user){
try {
userService.save(user);
return "redirect:/user/findAll";
}catch (Exception e){
e.printStackTrace();
return "redirect:/add.jsp";
}
}
@RequestMapping("findAll")
public String findAll(HttpServletRequest request){
List<User> users = userService.findAll();
request.setAttribute("users", users);
return "findAll";
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加用户信息</title>
</head>
<body>
<h1>添加用户信息</h1>
<form action="${pageContext.request.contextPath}/user/save" method="post">
姓名:<input type="text" name="name"/> <br>
年龄:<input type="text" name="age"/> <br>
生日:<input type="text" name="bir"/> <br>
<input type="submit" value="保存用户信息">
</form>
</body>
</html>
测试:
总结
|