【SSM】整合SSM

SSM整合

基础配置

1、新建Maven项目 并添加web支持(SpringMVC层需要)

2、导入依赖包

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
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<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.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>

3、Maven静态资源过滤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

4、建立ssm项目基本结构与配置框架

  • indi.zhihuali.pojo
  • indi.zhihuali.controller
  • indi.zhihuali.mapper
  • indi.zhihuali.service
  • mybatis-config.xml
1
2
3
4
5
6
7
<?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>

</configuration>
  • applicationContext.xml
1
2
3
4
5
6
7
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

mybatis层

  1. db.properties

    1
    2
    3
    4
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=1234
  2. 在IDEA中关联数据库

  3. 编写实体类(Lombok)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Books {
    private int bookID;
    private String bookName;
    private int bookCount;
    private String detail;
    }
  1. 编写Mapper接口

    1
    2
    3
    4
    5
    6
    7
    public interface BooksMapper {
    public int addBook(Books books);
    public int deleteBookById(@Param("bookId") int id);
    public int updateBook(Books books);
    public int queryBookById(@Param("bookId") int id);
    public List<Books> queryAllBook();
    }
  2. 补齐mybatis-config.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <typeAliases>
    <package name="indi.zhihuali.pojo"/>
    </typeAliases>
    <mappers>
    <!--每一个mapper.xml都需要在mybatis-config中配置-->
    <mapper class="indi.zhihuali.mapper.BooksMapper"/>
    <!--或者
    <mapper class="indi/zhihuali/mapper/BooksMapper"/>-->
    </mappers>
  1. 编写Mapper.xml (导入Mybatis的包)

    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"?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="indi.zhihuali.mapper.BooksMapper">
    <insert id="addBook" parameterType="books">
    insert into books(bookName,bookCounts,detail) values(#{bookName},#{bookCounts},#{detail})
    </insert>
    <delete id="deleteBook" parameterType="int">
    delete from books where bookID = #{bookID};
    </delete>
    <update id="updateBook" parameterType="books">
    update books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID}
    </update>
    <select id="queryBookById" resultType="books">
    select * from books where bookID = #{bookId}
    </select>
    <select id="queryAllBook" resultType="books">
    select * from books;
    </select>
    </mapper>
  2. 编写service层的接口与实现类

    1
    2
    3
    4
    5
    6
    7
    public interface BooksService {
    public int addBook(Books books);
    public int deleteBookById(int id);
    public int updateBook(Books books);
    public int queryBookById(int id);
    public List<Books> queryAllBook();
    }
    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
    public class BooksServiceImpl implements BooksService {
    private BooksMapper booksMapper;

    public void setBooksMapper(BooksMapper booksMapper) {
    this.booksMapper = booksMapper;
    }

    public int addBook(Books books) {
    return booksMapper.addBook(books);
    }

    public int deleteBookById(int id) {
    return booksMapper.deleteBookById(id);
    }

    public int updateBook(Books books) {
    return booksMapper.updateBook(books);
    }

    public int queryBookById(int id) {
    return booksMapper.queryBookById(id);
    }

    public List<Books> queryAllBook() {
    return booksMapper.queryAllBook();
    }
    }

Spring层

1、spring整合mapper层

spring-mapper.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
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.关联数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2. 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.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>

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

<!-- 配置mapper接口扫描包 动态实现了Dao接口可以注入到Spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 扫描dao -->
<property name="basePackage" value="indi.zhihuali.mapper"/>

</bean>

</beans>

注意:在将spring的时候配置mapper接口时,因为接口不是类不能直接注入,而通常使用的是mapperImpl实现类来实现注入

在spring中可以采用

  • 实现Mapper接口 并在impl类中私有定义SqlSessionTemplate并为其添加set方法 来获取并使用SqlSessionTemplate
  • 实现Mapper接口并继承sqlSessionDaoSupport 直接通过 getSession() 来获取并使用SqlSessionTemplate

现在这里采用下面的类来实现mapper的扫描注入(spring通过反射实现)

xmlorg.mybatis.spring.mapper.MapperScannerConfigurer

2、spring整合service层

spring-service.xml

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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 1、扫描service下的包-->
<context:component-scan base-package="indi.zhihuali.service"/>
<!-- 2、将所有的业务类注入spring 可以通过配置或者注解实现 -->
<bean id="BooksServiceImpl" class="indi.zhihuali.service.BooksServiceImpl">
<!-- 保证在其他相关xml文件中已经配置 此处的booksMapper就已经在spring-dao.xml中配置好了-->
<!-- <property name="basePackage" value="indi.zhihuali.mapper"/> -->
<property name="booksMapper" ref="booksMapper"></property>
</bean>
<!-- 3.声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4.如果有aop织入,继续配置-->
</beans>

两种关联xml文件方式:

  1. 选其中一个xml文件作为总的 来囊括剩余分xml文件 通常为applicationContext.xml

upload successful

  1. 将xml文件添加到同一个上下文中

upload successful

3、applicationContext 导入spring-mapper.xml和spring-service.xml

1
2
3
4
5
6
7
8
9
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mapper.xml"/>

</beans>

SpringMVC层

1、给maven项目添加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
41
<?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>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.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>

<!-- 乱码过滤-->
<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过期时间 : 15mins-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>

</web-app>

3、配置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
26
27
28
<?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
ttp://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
https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 不走注解时: 映射器 适配器 视图解析器-->
<!-- 下面以使用注解的方法进行配置 -->

<!-- 1、注解驱动 -->
<mvc:annotation-driven/>
<!-- 2、静态资源过滤 -->
<mvc:default-servlet-handler/>
<!-- 3、扫描包 -->
<context:component-scan base-package="indi.zhihuali.controller"/>
<!-- 4、视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

4、applicationContext 导入spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mapper.xml"/>
<import resource="classpath:spring-mvc.xml"/>

</beans>

5、编写Controller层 测试运行ssm项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
@RequestMapping("/book")
public class BooksController {
// controller 调用 service层
@Autowired
@Qualifier(value = "booksServiceImpl")
private BooksService booksService;
// 查询全部的书籍并返回一个书籍展示页面
@RequestMapping("/allBooks")
public String list(Model model){
List<Books> books = booksService.queryAllBook();
model.addAttribute("list",books);
return "allBooks";
}
}

6、WEB-INF目录下生成jsp目录并新建Controller跳转的页面名

1
return "allBooks"

7、设置index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%--
Created by IntelliJ IDEA.
User: zhihua.Li
Date: 2020/8/30
Time: 14:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<!--生成超链接 点击进入controller 从而实现到 allBooks.jsp 的跳转-->
<h3>
<a href="${pageContext.request.contextPath}/book/allBooks">进入书籍页面</a>
</h3>
</body>
</html>

报错:

1、

upload successful

upload successful

必须在Project Structure处为WEB-INF处添加lib并导入所有jar包

2、

upload successful

upload successful

原因:在spring-mapper.xml中配置了

upload successful

删掉即可

3、

“org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 76; schema_reference.4: 无法读取方案文档 ‘ttp:/www.springframework.org/schema/beans/spring-beans.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 xsd:schema

原因:在某个xml文件中 https:// 写成 ttps://

upload successful

4、NoSuchBean…..Exception(老师的报错:视频20.查询书记功能)

排错思路:

  • 问题:bean不存在
  • 步骤:

    1. 查看bean是否注入成功

      spring-service.xml中

      1
      <bean id="booksServiceImpl" class="indi.zhihuali.service.BooksServiceImpl">

      可见注入成功

    2. Junit测试 进一步确定错误在哪里 测试没有问题

    3. 问题就一定不在底层 是spring出了问题

    4. SpringMVC 整合时没有调用到service层的bean:

      1. applicationContext.xml没有注入bean
      2. web.xml中配置错误 没有绑定spring-service.xml