SSM整合-Spring-SpringMVC-Mybatis
一,前提
把Spring SpringMVC Mybatis框架整合成一套,在后期企业级开发中,可以实现复用。
- Mybatis:操作数据库的框架,特点:只需要写出Dao层接口和接口中方法对应的sql语句;
- SpringMVC:实现对外的接口,特点:代替Servlet、接收请求(键值对、字符串(json或者xml)、路径传参),给出响应(页面路径、数据);
- Spring:实现其他框架的整合,特点:提供IOC,统一进行对象的创建和管理,还提供AOP,可以在不改变的源码的情况下,对某些方法的增强。
二,SSM整和
2.1 导入依赖Jar包
- 数据库
- mybatis
- spring
- springmvc
- 其他相关
<dependencies>
<!-- 数据库 驱动和连接池-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- Mybatis相关 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- SpringMVC相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
<!-- Spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.9</version>
</dependency>
<!-- 其他jar-->
<!-- 文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- 简化类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- 日志log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
2.2实现配置
1.数据库连接信息
2.日志配置
3.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: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/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">
<!-- 1.加载外部的配置,一般都是数据库连接信息 -->
<!-- <context:property-placeholder location="classpath*:dbconfig.properties"></context:property-placeholder>-->
<!-- 2.配置数据库连接信息,创建数据库连接池的对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql:localhost:8080/db?useUnicode=true&characterEncoding=UTF8"></property>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="zzjava"></property>
</bean>
<!-- 3.配置Mybatis的连接工厂信息-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置数据库连接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置映射文件所在的路径-->
<property name="mapperLocations">
<array>
<value>classpath:mapper/*Mapper.xml</value>
</array>
</property>
</bean>
<!-- 4.Mybatis的扫描的对象 指定持久层的包-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="basePackage" value="com.feri.ssm.dao"></property>
</bean>
<!-- 5.配置需要IOC创建的额对象-->
<bean id="workServiceImpl" class="com.feri.ssm.service.impl.WorkServiceImpl">
<property name="dao" ref="workDao"></property>
</bean>
</beans>
4.springmMVC的配置
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 1.扫描控制层所在包,创建对应的类的对象,存储到IOC-->
<context:component-scan base-package="com.feri.ssm.controller"></context:component-scan>
<!-- 2.配置MVC相关信息-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 放行静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
5.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>ssm</display-name>
<!-- 1.配置Spring的上下文对象 -->
<!-- 1.1初始化参数加载Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 1.2.Spring的核心 监听器 监听ServletContext的生命周期,并创建上下文-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.Springmvc的前端控制器的配置 Servlet -->
<!-- 2.1.注册SpringMVC的前端控制器 就是Servlet的注册-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 2.2 局部参数:声明配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<!-- 2.3 配置Servlet的触发路径-->
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
3.3代码编写
3.3.1项目分包
entity-实体包:对应数据库的表 照抄
dao—持久层:实现表的常用的方法(CRUD)
mapper-映射文件夹:实现对应持久层的映射文件,定义接口中方法的sql语句
service.intf–业务接口层,实现业务逻辑功能的方法定义
service.impl–业务实现层,重写接口层的方法,实现业务逻辑
manager.intf–业务通用接口层,非数据库相关的业务逻辑的方法定义 manager.impl–业务通用实现层,非数据库相关的业务逻辑的方法定义 controller----控制层,实现对外的接口,接受请求和给出响应
config-------配置层,存储一些通用配置信息
util----------工具层,封装一些工具类
dao----------数据转换层,实现实体类的转换操作
bo-----------业务对象层,封装业务层的类
vo-----------视图对象层,封装终端(网站、app、小程序、快应用、物联网等)
exception----异常层,封装自定义异常
filter----------过滤器层
listener-------监听器层
……
3.3.2 编写代码
实体类
@Data
public class Work {
private Integer id;
private String name;
private String company;
private double money;
private String city;
}
持久层
<?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">
<!--namespace = 所需实现的接口全限定名-->
<mapper namespace="com.feri.ssm.dao.WorkDao">
<!-- 新增 多个参数封装了类-->
<insert id="save" useGeneratedKeys="true" keyProperty="id">
insert into t_work(name,company,money,city) values(#{name},#{company},#{money},#{city});
</insert>
<!-- 查询-->
<select id="selectAll" resultType="com.feri.ssm.entity.Work">
select * from t_work order by id desc
</select>
</mapper>
业务层
public interface WorkService {
//新增
int save(Work work);
//查询
List<Work> queryAll();
}
@Data
public class WorkServiceImpl implements WorkService {
//依赖
private WorkDao dao;
@Override
public int save(Work work) {
return dao.save(work);
}
@Override
public List<Work> queryAll() {
return dao.selectAll();
}
}
控制层
@Controller
@RequestMapping("/api/work/")
public class WorkController {
@Autowired //导入,从IOC容器为这个属性注入
private WorkService service;
//新增
@RequestMapping(value = "save",method = {RequestMethod.POST})
@ResponseBody
public int save(Work work){
return service.save(work);
}
//查询
@RequestMapping(value = "all",method = {RequestMethod.GET})
@ResponseBody
public List<Work> all(){
return service.queryAll();
}
}