0%

【SSM整合】Spring+SpringMVC+Mybatis框架整合

将三大框架整合在一起的方法

一、前期准备

  • 导包(Maven设置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringMVC框架</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>整合ssm</artifactId>

<!--导入依赖-->
<dependencies>
<!--Junit:单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!--mysql8数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>

<!--数据库连接池(c3p0, dbcp)-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<!--Servlet jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>

<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>

<!--静态资源导出-->
<build>
<resources>
<!--maven资源打包,java目录下默认值打包java程序,不打包配置文件,通过设置也让其打包-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

</project>

二、整合Mybatis层

2.1 数据库创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create database `ssmbuild`;

use `ssmbuild`;

create table `book`(
`bookID` int(10) not null auto_increment,
`bookName` varchar(100) not null,
`bookCounts` int(11) not null,
`detail` varchar(200) not null,
key `bookID` (`bookID`)
)engine=innodb default charset=utf8

insert into `book` (`bookID`, `bookName`, `bookCounts`, `detail`)
values (1, 'java', 1, '从入门到放弃'),
(2, 'MySQL', 10, '从删库到跑路'),
(3, 'Linux', 5, '从进门到进牢')

2.2 创建相应的包

  • controller、dao(mapper)、pojo(bean)、service

2.3 创建数据库对应的bean/pojo

  • 为了偷懒,可以导入一个第三方jar包lombok
1
2
3
4
5
6
7
Maven依赖导入
<!--lombok:通过注解,快速创建Bean对象-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
Bean/Pojo
@Data //自动产生get set toString方法
@AllArgsConstructor //创建所有有参构造器
@NoArgsConstructor //创建无参构造器
public class Books {
private Integer bookID;
private String bookName;
private Integer bookCounts;
private String detail;
}

2.3 创建接口

  • 在dao包中创建映射接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface BookMapper {
//增
int addBook(Book book);

//删
int deleteBookById(Integer id);

//改
int updateBook(Book book);

//查
Book queryBookById(Integer id);
List<Books> queryAllBook();
}

2.4 创建接口的Mybatis的映射文件

  • 在dao包中创建和接口同名的映射文件
  • 在映射文件中添加方法执行的sql语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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.letere.dao.BookMapper">
<insert id="addBook" parameterType="com.letere.pojo.Books">
insert into book(bookName, bookCounts, detail)
values (#{bookName}, #{bookCounts}, #{detail})
</insert>

<delete id="deleteBookById" parameterType="Integer">
delete from book
where bookID=#{bookID}
</delete>

<update id="updateBook" parameterType="com.letere.pojo.Books"> <!--Books在全局配置文件起了别名-->
update book
set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail}
where bookID=#{bookID}
</update>

<select id="queryBookById" parameterType="Integer" resultType="com.letere.pojo.Books">
select *
from book
where bookID=#{bookID}
</select>

<select id="queryAllBook" resultType="com.letere.pojo.Books">
select *
from book
</select>
</mapper>

2.5 创建Mybatis全局映射文件

  • 在全局配置文件中注册Mapper映射文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>

<!--配置数据源:读取配置文件(交给Spring去做)-->

<!--起别名-->
<typeAliases>
<package name="com.letere.pojo"/>
</typeAliases>

<!--将写好的sql映射文件注册到全局配置文件中-->
<mappers>
<!-- <mapper resource=".\\com\\letere\\dao\\BookMapper.xml" />-->
<mapper class="com.letere.dao.BookMapper" /> <!--接口 和 映射文件 名称一样,可以直接用class来定位-->
</mappers>

</configuration>

2.6 创建Service层的实现类

  • 在Service包创建BookMapper的实现类,用于Controller层调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Service
public class BookService implements BookMapper{

@Autowired
private BookMapper bookMapper;//创建BookMapper对象,来调用其接口


@Override
public int addBook(Book book) {
return bookMapper.addBook(book);
}

@Override
public int deleteBookById(Integer id) {
return bookMapper.deleteBookById(id);
}

@Override
public int updateBook(Book book) {
return bookMapper.updateBook(book);
}

@Override
public Book queryBookById(Integer id) {
return bookMapper.queryBookById(id);
}

@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}

2.7 创建好数据库连接配置文件

  • 在resources文件下创建一个database.properties
    1
    2
    3
    4
    mysql.dirver=com.mysql.cj.jdbc.Driver
    mysql.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=GMT%2B8&useSSL=true&useUnicode=true&characterEncoding=utf8
    mysql.username=root
    mysql.password=123

2.8 创建配置整合文件

  • 将mybatis+spring+springmvc三个框架都整合在一个文件中
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">

<!--将其他配置文件进行关联-->

</beans>

三、整合Spring层

3.1 关于DAO层的Spring配置文件

  • 创建一个spring-dao.xml的配置文件,里面填写配置内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

<!--1.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>

<!--2.数据源
dbcp:半自动化,不能自动连接
c3p0:自动化操作(自动化加载配置文件,并可以自动设置到对象中)
druid:hikari
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mysql.driver}"/>
<property name="jdbcUrl" value="${mysql.url}" />
<property name="user" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />

<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000" />
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2" />
</bean>


<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--绑定数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定Mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!--4.配置dao接口扫描包,动态的实现类Dao接口可以注入到Spring容器中-->
<mybatis-spring:scan base-package="com.letere.dao"/>
<!-- 旧方法 -->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="com.letere.dao"/>-->
<!-- </bean>-->
</beans>

3.2 关于Service层的Spring配置文件

  • 创建一个Spring-service.xml 文件,里面填写配置信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--1.扫描serviec层下的包-->
<context:component-scan base-package="com.letere.service"/>

<!--2.声明事务配置-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>

<!--3.aop事务支持-->

</beans>

3.3 在关联Spring文件导入dao和service的配置文件

  • 在applicationContext.xml中导入spring-dao.xml 和 spring-service.xml配置文件
1
2
3
4
5
6
7
8
9
10
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--将其他配置文件进行关联-->
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>

四、整合SpringMVC层

4.1 添加web支持

  • 右键模块 –> Add FrameWork Support –> JavaSE –> Web Application –> 勾选后选择4.0版本
  • 防止项目发布出现缺少依赖,要手动将依赖引入到发布项目中

4.2 创建springmvc的配置文件

  • 创建spring-mvc.xml配置文件,里面填写配置信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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 https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--扫描contorller包-->
<context:component-scan base-package="com.letere.controller"/>

<!--注解驱动-->
<mvc:annotation-driven/>

<!--静态资源过滤-->
<mvc:default-servlet-handler/>

<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

4.3 将SpringMVC配置导入到Spring关联文件

  • 将spring-mvc.xml导入到applicationContext.xml中
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--Spring核心配置文件-->

<!--将其他配置文件进行关联-->
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>

4.4 配置Web.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<!--SpringMVC自带servlet-->
<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:applicationContext.xml</param-value><!--框架整合中,不仅需要springmvc的配置文件,还需要其他的,所以直接用整合的配置文件-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!--过滤器,解决乱码-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name><!--private String encoding;因为可以为空,所以代码不提示-->
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--Session超时时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
  • 以上是框架整合最基本的的内容了
  • 后面就是Controller 和 JSP页面之间的使用,在SpringMVC中有所讲解,就不展开了