SSM整合(基于IDEA的maven方式)
一、工程目录结构、数据库信息、POM文件
1)目录结构

2)数据库内容
|
create table people(
id int primary key auto_increment,
name char(20) not null,
age int not null,
phone char(20) not null
);
insert into people values(default,’张三’,20,’13004144155’);
insert into people values(default,’李四’,20,’13004144155’);
insert into people values(default,’王五’,20,’13004144155’);
insert into people values(default,’赵六’,20,’13004144155’);
insert into people values(default,’前吧’,20,’13004144155’);
|
3)pom文件
3.1 依赖导入
|
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--Spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!--log4j日志文件的jar包--> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!--导入jstl标签库--> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mysql连接的jar包--> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--mybatis所需的jar包--> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mybatis与spring整合是所需的jar包--> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!--c3p0数据源配置的jar包--> <dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
|
3.2配置jetty服务器
|
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.5.v20170502</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<!-- web项目根路径 --> <contextPath>/</contextPath>
</webApp>
</configuration> </plugin>
|
二、ssm整合时的配置文件
1) web.xml(将spring与springmvc配置与服务器中)
|
<?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>SSM4T</display-name>
<!-- 配置Spring --> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置SpringMVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<!-- 自启动servlet,我们需要让此Servlet在加载配置文件加载前出现 --> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
|
2)Spring整合mybatis
2.1spring的application-context.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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--指定使用了spring注解的包的位置--> <context:component-scan base-package="com.abc.service"></context:component-scan>
<!--1 引入属性文件,在配置中占位使用 --> <context:property-placeholder location="classpath:db/db.properties" />
<!--2 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--驱动类名 --> <property name="driverClass" value="${mysql.driver}" />
<!-- url --> <property name="jdbcUrl" value="${mysql.url}" />
<!-- 用户名 --> <property name="user" value="${mysql.username}" />
<!-- 密码 --> <property name="password" value="${mysql.password}" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 --> <property name="acquireIncrement" value="5"></property>
<!-- 初始连接池大小 --> <property name="initialPoolSize" value="10"></property>
<!-- 连接池中连接最小个数 --> <property name="minPoolSize" value="5"></property>
<!-- 连接池中连接最大个数 --> <property name="maxPoolSize" value="20"></property>
</bean>
<!--3 sessionFatory的配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--别名类所在的包名--> <property name="typeAliasesPackage" value="com.abc.pojo"/>
<!--下划线与驼峰命名之间的转换--> <property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<!-- sql映射文件路径,及mybatis所加载SQL语句的*Mapper文件 --> <property name="mapperLocations" value="classpath*:mybatis/mapper/*Mapper.xml"></property>
</bean>
<!-- 自动扫描接口类,如果发现和某个mapper的namespace一致,那么就指导生成这个接口的实现,然后注入spring容器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定要自动扫描接口的基础包,实现接口 --> <property name="basePackage" value="com.abc.mapper"></property>
</bean> </beans>
|
2.2 spring中使用的数据库连接的db.properties文件
|
########### mysql connection profile ########### mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC mysql.username=root mysql.password=115967286
|
3)SpringMVC配置文件
|
<?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" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd">
<!-- springmvc可以用注解的模式来进行描述操作流程 --> <mvc:annotation-driven/>
<!--使用到SpringMVC注解类的包名--> <context:component-scan base-package="com.abc.controller"></context:component-scan> </beans>
|
三、项目编写架构流程
1)People实体类
|
public class People {
private int id;
private String name;
private int age;
private String phone;
public int getId() {
return id; }
public void setId(int id) {
this.id = id; }
public String getName() {
return name; }
public void setName(String name) {
this.name = name; }
public int getAge() {
return age; }
public void setAge(int age) {
this.age = age; }
public String getPhone() {
return phone; }
public void setPhone(String phone) {
this.phone = phone; }
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}'; } }
|
2)peopleMapper接口与其映射的配置文件PeopleMapper.xml
|
public interface PeopleMapper {
List<People> SelAllPeople(); }
|
|
<?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必须与PeopleMapper的路径相同,进行接口绑定,目的是为了更直观的理解--> <mapper namespace="com.abc.mapper.PeopleMapper">
<select id="SelAllPeople" resultType="People"> select *from people
</select> </mapper>
|
3)PeopleService与PeopleServiceImpl
|
public interface PeopleService { List<People> showPeoples(); }
|
|
/*将peopleServiceImpl类交给spring管理*/ @Service public class PeopleServiceImpl implements PeopleService {
/*通过spring注入peopleMapper的管理实体*/ @Autowired
private PeopleMapper peopleMapper;
@Override
public List<People> showPeoples() {
return peopleMapper.SelAllPeople(); } }
|
4)PeopleController层
|
/*将PeopleController交给SpringMVC管理*/ @Controller public class PeopleController {
/*spring注入管理对象*/ @Autowired
private PeopleService peopleService;
@GetMapping("people")
public String demo(Model model){ model.addAttribute("peoples",peopleService.showPeoples());
return "list_people.jsp"; } }
|
5)list_people.jsp加载页面
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head>
<title>Title</title> </head> <body> <h2>people加载页面</h2> <table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>电话</td>
<td>操作</td>
</tr>
<c:forEach var="p" items="${peoples}">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.age}</td>
<td>${p.phone}</td>
<td>
<input type="button" value="修改">
<input type="button" value="删除">
</td>
</tr>
</c:forEach> </table> </body> </html>
|

四、SSM整合总结
在ssm整合的时候我们首先需要将spring与springmvc交给服务器管理,具体方式便是在web.xml中的配置。
配置spring的时候我们可以通过全局的context-param配置spring文件所在的具体位置,通过listener来将spring配置于服务器中。
配置SpringMVC的时候,我们是使用Servlet的方式去配置,所以我们需要在配置SpringMVC的时候,通过load-on-startup来使其优先加载。同理我们也可以在Servlet中的init-param属性配置SpringMVC的具体位置。
将spring与springmvc交给完服务器管理后,我们便要具体的配置spring与SpringMVC的配置文件了。
spring配置文件中,首先我们通过context:component-sacn告诉spring,我们所用到spring注解的包的位置,接着便是spring整合mybatis了,我们会先使用c3p0的方式来管理数据源,然后将数据源通过spring的DI方式注入于mybatis的SqlSessionFactory中,最后使用到mybatis与spring关联的自动扫描接口类,生成对应位置接口生成对应的实体且交给spring管理。到此便完成了spring对于mybatis的整合。
SpringMVC配置中,我们首先需要通过mvc:annotation-drivern属性启动SpringMVC的注解,其次一样通过context:component-scan属性告诉SpringMVC所使用到SpringMVC注解的包的位置,注意spring是SpringMVC的父类,所以SpringMVC拥有spring的所有方法。