一.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项目结构如下

(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
|
public interface AccountMapper { public List<Account> findAll(); }
|
(6)创建Service接口和实现了类
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public interface AccountService { public List<Account> queryAll(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
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
| <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
|
@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
|
public class Test {
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">
<context:component-scan base-package="com.ssm.demo.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> <mvc:message-converters register-defaults="true"> <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"> <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> <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
|
@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"> <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> <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> <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
|
@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"> <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> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="typeAliasesPackage" value="com.bogedev.spring_mybatis.entity"/> <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/> </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.demo.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <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
|
@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
|
@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需要的日志包
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"?>
<configuration status="INFO"> <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> <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;
@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项目的整合