0%

SpringMVC--整合ssm

一、整合前的准备

1)导入依赖和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
<!--    依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring...-->
<dependencies>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!-- mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

2)创建数据库

3)在idea中连接数据库

4)在项目下创建下列几个包

  1. controller
  2. dao
  3. pojo
  4. service

二、Mybatis部分

1)在res文件夹中创建mybatis-config.xml和database.properties文件,mybatis-config.xml为mybatis的核心配置文件,database.properties为数据库连接信息配置文件

1
2
3
4
5
6
7
8
9
10
11
12
mybatis-config
<?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去做-->
<!--类型别名:用于扫描com.muchlab.pojo包下所有的类-->
<typeAliases>
<package name="com.muchlab.pojo"/>
</typeAliases>
</configuration>
1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=root
jdbc.password=123456

2) 在pojo创建实体类Books

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.muchlab.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;

public Books(int bookID, String bookName, int bookCounts, String detail) {
this.bookID = bookID;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}
}

3)在dao包创建BookMapper接口,该接口可在mybatis中形成多个方法和sql查询语句的映射关系。dao层一般都是外层调用数据库的接口,一般都是一些原子性操作,比如简单的增删改查。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.muchlab.dao;

import com.muchlab.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
//add
int addBook(Books books);
//delete
int deleteBookById(@Param("bookID") int id);
//update
int updateBook(Books books);
//queryitem
Books queryBookById(@Param("bookID") int id);
//queryall
List<Books> queryAllBook();
//queryBookByName
List<Books> queryBookByName(@Param("bookName")String bookName);
}

4)在dao包创建BookMapper.xml,该文件为mybatis的映射文件。

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace指明了该mapper文件所映射的接口类型-->
<mapper namespace="com.muchlab.dao.BookMapper">
<!--id为接口中的方法名,parameterType为方法的参数类型,resultType为方法的返回类型-->
<insert id="addBook" parameterType="Books">
insert into ssmbuild.books (bookName, bookCounts, detail) values
(#{bookName}, #{bookCounts}, #{detail});
</insert>
<delete id="deleteBookById" parameterType="int">
delete from ssmbuild.books where bookID = #{bookID}
</delete>
<update id="updateBook" parameterType="Books">
update ssmbuild.books set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail} where bookID=#{bookID};
</update>
<select id="queryBookById" resultType="Books">
select * from ssmbuild.books where bookID = #{bookID};
</select>
<select id="queryAllBook" resultType="Books">
select * from ssmbuild.books;
</select>
<select id="queryBookByName" resultType="Books">
select * from ssmbuild.books where bookName like "%"#{bookName}"%";
</select>
</mapper>

5)在service包创建BookService接口,该接口为具体业务的接口,再创建一个BookServiceImpl实现BookService接口。业务层一般用来调用dao层,通过较复杂的逻辑来满足用户的需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BookService
package com.muchlab.service;

import com.muchlab.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookService {
//add
int addBook(Books books);
//delete
int deleteBookById( int id);
//update
int updateBook(Books books);
//queryitem
Books queryBookById(int id);
//queryall
List<Books> queryAllBook();
//queryBookByName
List<Books> queryBookByName(String bookName);
}
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
BookServiceImpl
package com.muchlab.service;

import com.muchlab.dao.BookMapper;
import com.muchlab.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public class BookServiceImpl implements BookService{
private BookMapper bookMapper;
//后面将在spring的业务配置文件中使用依赖注入的方式实现bookMapper接口
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}

@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}

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

@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}

@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}

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

public List<Books> queryBookByName(String bookName){
return bookMapper.queryBookByName(bookName);
}
}

三、Spring部分

1)在res文件夹下创建applicationContext.xml、spring-dao.xml、spring-service.xml文件,这三个文件分别为整个项目、dao和service的配置文件,并将这三个文件添加到同一个SpringApplicationContext中

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="springmvc-servlet.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-dao.xml"/>
</beans>

该窗口在项目结构中可打开

2)spring-dao.xml,用于连接数据源和实现Dao接口,并将他们注入到spring容器中,由spring统一管理

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
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2导入c3p0连接池
dbcp:半自动化操作,不能自动连接
c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中)
druid:
hikari:-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 关联数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置dao接口扫描包,动态实现Dao接口并注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的dao包-->
<property name="basePackage" value="com.muchlab.dao"/>
</bean>
</beans>

3)spring-service.xml,用于将所有的服务类实现,并注入到spring容器中,在该文件中可以添加spring为我们提供好的服务,比如事务管理、日志服务、身份认证等等,都是以aop的方式进行横向切入的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1扫描service下的包-->
<context:component-scan base-package="com.muchlab.service"/>
<!-- 2将所有的服务类实现,并注入到spring容器中,也可以通过注解实现-->
<bean class="com.muchlab.service.BookServiceImpl" id="bookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!-- 3声明事务管理,自动管理增删改查,比如自动提交事务等-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4aop事务支持-->
</beans>

四、SpringMvc部分

1)为项目添加web支持

2)配置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">
<!-- DispatcherServlet,用于拦截匹配的请求,Servlet拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数-->
<init-param>
<!--绑定spring配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--表示匹配根路径下的全部请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 乱码过滤,该过滤为spring提供的,用于解决乱码问题-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Session时限15分钟-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

3)在res下创建springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1注解驱动-->
<mvc:annotation-driven/>
<!-- 2静态资源过滤-->
<mvc:default-servlet-handler/>
<!-- 3扫描包:controller-->
<context:component-scan base-package="com.muchlab.controller"/>
<!-- 4视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

4)注意,在启动项目前必须在项目结构中添加所依赖的jar包,不然服务器会找不到资源从而报错

至此,SpringMvc,Spring和Mybatis的整合就结束了。

-------------本文结束感谢您的阅读-------------