0%

SSM三大框架整合案列

一.ssm整合结构图

二.基础环境搭建

(1)创建数据表结构

1
2
3
4
5
6
create database ssm;
create table account(
id int primary key auto_increment,
name varchar(100),
money double(7,2)
);

(2)创建web项目结构如下

1
<!--more-->

(3)整合需要jar包并导入

下载整合jar

(4)创建实体类

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
public class Account implements Serializable {
private static final long serialVersionUID = 209835590469052576L;

private Integer id;

private String name;

private Object money;


public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Object getMoney() {
return money;
}

public void setMoney(Object money) {
this.money = money;
}

}

(5)创建dao(或mapper叫法不同)

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 描述
*
* @version 1.0
* @Title: AccountMapper
* @author: ZHANGBO
* @Description: AccountMapper
* @date 2022/03/19 04:24:27
*/
public interface AccountMapper {
public List<Account> findAll();//查找所有账号
}

(6)创建Service接口和实现了类

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 描述
*
* @version 1.0
* @Title: AccountService
* @author: ZHANGBO
* @Description: AccountService接口
* @date 2022/03/19 04:28:29
*/
public interface AccountService {
public List<Account> queryAll();//查找所有账号
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountServiceImpl
* @date 2022/03/19 04:29:59
*/
public class AccountServiceImpl implements AccountService {
@Override
public List<Account> queryAll() {
return null;
}
}

三.搭建Spring环境

(1)创建spring核心配置文件:applicatContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

(2)开启包扫描

1
2
<!--开启包扫描-->
<context:component-scan base-package="com.ssm.demo"></context:component-scan>

(3)配置哪些注解不扫描

1
2
3
4
5
<!--开启包扫描  dao  service需要交给spring扫描 controller由springmvc扫描需要排除注解扫描-->
<context:component-scan base-package="com.ssm.demo">
<!--排除不需要扫描的注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

(4)service层添加注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountServiceImpl
* @date 2022/03/19 04:29:59
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Override
public List<Account> queryAll() {
System.out.println("AccountServiceImpl开始执行queryAll方法");
return null;
}
}

(6)编写测试类测试业务层是否正常

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
/**
* 描述
*
* @version 1.0
* @Title: Test
* @author: ZHANGBO
* @Description: 测试类
* @date 2022/03/19 04:48:58
*/
public class Test {
/**
* 描述 测试业务层代码
* @param
* @return void
* @author zhangbo
* @date 2022/3/19 04:49:29
* @version 1.0
*/
public void testService(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicatContext.xml");
AccountService accountService=(AccountService)context.getBean("accountService");
accountService.queryAll();
}

}

四.搭建SpringMVC环境

(1)创建springMvc核心配置文件:springmvc.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
<?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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--配置文件知识,引用于该网址:https://www.cnblogs.com/afeng2010/p/10133797.html-->
<!--开启springmvc注解模式
访问路径与方法的匹配可以通过注解配置
若不加DispatcherServlet则无法区分请求是资源文件还是mvc的注解,而导致controller的请求报404错误
自动扫描到的@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求-->
<!--自动扫描相关的包-->
<context:component-scan base-package="com.ssm.demo.controller">
<!--配置只扫描Controller-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--处理静态文件的过滤-->
<mvc:default-servlet-handler/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!--注解支持-->
<mvc:annotation-driven>
<!--消息转换器:不使用默认的jackson组件-->
<mvc:message-converters register-defaults="true">
<!-- 配置FastJson工具中的消息转换器类-->
<bean id="fastJsonConverters" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 消息转换器类中的字段赋值,支持的枚举类型-->
<property name="supportedMediaTypes" >
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>

(2)web.xml配置SpringMvc的servlet拦截

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"?>
<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-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 表示容器再启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

(3)编写AccountController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountController
* @date 2022/03/19 04:43:01
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(){
return "/account/list";
}
}

(4)编写jsp页面测试

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2022/3/19
Time: 3:40
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>
<a href="<%=request.getContextPath()%>/account/list">查看账户列表</a>
</body>
</html>

hello.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试页面</title>
</head>
<body>
<h1 style="color: red">HelloSpringMvc</h1>
</body>
</html>

(5)将项目部署到tomcat启动测试

五.spring整合springMvc

(1)在web.xml配置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
<?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">
<!--spring监听器 监听项目启动加载spring配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicatContext.xml</param-value>
</context-param>
<!--Springmvc配置-->
<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:springmvc.xml</param-value>
</init-param>
<!-- 表示容器再启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

(2)在AccountController注入AccountService并调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountController
* @date 2022/03/19 04:43:01
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(){
accountService.queryAll();
return "/account/list";
}
}

(3)启动项目访问页面测试开始发可以调用业务层代码

六.spring整合mybatis

参考之前spring整合mybatis步骤即可

(1)最终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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启包扫描 dao service需要交给spring扫描 controller由springmvc扫描需要排除注解扫描-->
<context:component-scan base-package="com.ssm.demo">
<!--排除不需要扫描的注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 加载数据库配置信息 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 连接池对象 -->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 1:连接池 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 2:绑定MyBatis总配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 3:配置别名扫描 -->
<property name="typeAliasesPackage" value="com.bogedev.spring_mybatis.entity"/>
<!-- 4:加载mapper文件 -->
<property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/>
</bean>
<!-- 创建mapper, 因为之前没有写mapper的实现类,mybatis帮我们创建的代理类
所以要告诉Spring,帮我们创建mapper的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.demo.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--配置事务管理器,以 JDBC 为例-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"></property>
</bean>
<!--注解式事务管理-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

(2)在service实现类中注入mapper接口并调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountServiceImpl
* @date 2022/03/19 04:29:59
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public List<Account> queryAll() {
System.out.println("AccountServiceImpl开始执行queryAll方法");
return accountMapper.findAll();
}
}

(2)在controller中将调用业务层查到的数据返回页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountController
* @date 2022/03/19 04:43:01
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(Model model){
List list=accountService.queryAll();
model.addAttribute("accountList",list);
return "/account/list";
}
}

(3)页面取出数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2022/3/19
Time: 5:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试页面</title>
</head>
<body>
<h1 style="color: red">账户列表</h1>
${accountList}
</body>
</html>

(4)启动项目测试

七.整合log4j日志

(1)下载log4j需要的日志包

  • log4j-api-2.17.1.jar

  • log4j-core-2.17.1.jar

  • log4j-slf4j18-impl-2.17.1.jar

Log4j2 提供了以下 2 个适配器:

  • log4j-slf4j-impl 应该与 SLF4J 1.7.x 版本或更早版本一起使用。
  • log4j-slf4j18-impl 应该与 SLF4J 1.8.x 版本或更高版本一起使用。

因此,我们向项目中引入的 slf4j-api-xxx.jar 必须为 1.8.x 及以上版本

(2)创建log4j需要的配置文件

内容如下

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"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- Configuration 后面的 status 用于设置 log4j2 自身内部的信息输出,可以不设置,当设置成 trace 时,可以看到 log4j2 内部各种详细输出-->
<configuration status="INFO">
<!--先定义所有的 appender-->
<appenders>
<!--输出日志信息到控制台-->
<console name="Console" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</console>
</appenders>
<!--然后定义 logger,只有定义了 logger 并引入的 appender,appender 才会生效-->
<!--root:用于指定项目的根日志,如果没有单独指定 Logger,则会使用 root 作为默认的日志输出-->
<loggers>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>

(3)打印日志测试

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
package com.ssm.demo.service.impl;

import com.ssm.demo.entity.Account;
import com.ssm.demo.mapper.AccountMapper;
import com.ssm.demo.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 描述
*
* @version 1.0
* @Title:
* @author: ZHANGBO
* @Description: AccountServiceImpl
* @date 2022/03/19 04:29:59
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
Logger log=LoggerFactory.getLogger(AccountServiceImpl.class);
@Autowired
private AccountMapper accountMapper;
@Override
public List<Account> queryAll() {
log.info("AccountServiceImpl开始执行queryAll方法");
return accountMapper.findAll();
}
}

恭喜完成了SSM项目的整合