<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssm_demo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3)、创建包路径和目录
4)、在pom.xml文件中集中定义依赖版本号
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.10</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mysql.version>8.0.12</mysql.version>
<druid.version>1.1.6</druid.version>
<slf4j.version>1.6.4</slf4j.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<jackson.version>2.4.2</jackson.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
1)、在pom.xml文件中,引入spring的依赖包
<!-- 整合spring框架(包含springmvc)
这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内)
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
2)、在resources/spring目录下,创建spring的核心配置文件:applicationContext.xml
配置内容如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans>
至此,spring框架搭建完成,可自行测试
前面在添加spring的jar包同时, 将springmvc的jar包也引入了,因此这里不再引入springmvc的jar包
1)、 在resources/spring目录下,创建springmvc的核心配置文件:springmvc-config.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) -->
<mvc:default-servlet-handler/>
<!-- 2.配置注解驱动,用于识别注解(比如@Controller) -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类,
如果扫描到的类上有 @Controller、@Service、@Component等注解,
将会自动将类注册为bean
-->
<context:component-scan base-package="com.mq.controller">
</context:component-scan>
<!-- 4.配置内部资源视图解析器
prefix:配置路径前缀
suffix:配置文件后缀
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2)、在web.xml中配置springmvc
<!-- 配置springmvc, 将所有请求交给springmvc来处理 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc核心配置文件的位置,默认Springmvc的配置文件是在WEB-INF目录下,默认的名字为springmvc-servlet.xml,如果要放在其他目录,则需要指定如下配置:
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml
springmvc
/
3)、配置springmvc乱码处理过滤器
<!-- 乱码处理过滤器 -->
<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>/*
至此,springMVC框架搭建完成,可自行测试
1)、在pom.xml文件中,引入mybatis及相关依赖包
<!-- 整合mybatis框架 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
2)、 在resources/mybatis目录下,创建mybatis的核心配置文件:mybatis-config.xml
配置如下:
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置开发环境 -->
<environments default="develop">
<!-- 这里可以配置多个环境,比如develop,test等 -->
<environment id="develop">
<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
<transactionManager type="JDBC"></transactionManager>
<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm_demo?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
<mappers>
<mapper resource="mybatis/mapper/userMapper.xml"/>
</mappers>
</configuration>
至此,mybatis框架搭建完成,可自行测试
1)、修改mybatis-config.xml文件,将连接池等配置移除,在spring中配置
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 1.配置开发环境 -->
<!-- 1.1.配置事务管理方式:JDBC:将事务交给JDBC管理(推荐) -->
<!-- 1.2.配置数据源,即连接池方式:JNDI/POOLED/UNPOOLED -->
<!-- 2.加载Mapper配置文件,路径以斜杠间隔: xx/xx/../xx.xml -->
</configuration>
2)、在applicationContext.xml中配置druid连接池
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 1.加载jdbc.properties文件的位置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置连接数据库的基本信息 -->
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<!-- 3.整合spring和mybatis框架
将SqlSession等对象的创建交给Spring容器
id值(sqlSessionFactory)是固定值
-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 3.1.指定mybatis核心配置文件的位置 -->
<property name="configLocation"
value="classpath:mybatis/mybatis-config.xml"></property>
<!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!-- 4、定义mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 -->
<property name="basePackage" value="com.mq.dao"/>
</bean>
<!-- 5.配置需要扫描的包(service层):spring自动去扫描 base-package下的类,
如果扫描到的类上有 @Controller、@Service、@Component等注解,
将会自动将类注册为bean(即由spring创建实例)
-->
<context:component-scan base-package="com.mq.service"/>
</beans>
3)、 在resources目录下创建jdbc.properties文件,
将连接数据库的基本信息提取到文件中
db.driverClassName=com.mysql.cj.jdbc.Driver
db.url= jdbc:mysql://localhost:3306/ssm_demo?useSSL=false&serverTimezone=UTC
db.username=root
db.password=root
至此,SSM整合完成
1)、在pom.xml文件中,引入log4j及其他依赖包
<!-- 整合log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Servlet/JSP/JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
2)、 在resources目录下创建log4j.properties文件,
配置内容如下:
log4j.rootLogger=INFO,A1
log4j.logger.cn.yhmis.mapper =DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
至此,SSM整合log4j完成
1. 准备工作
创建执行sql语句,创建数据库表:
-- 创建ssm_demo库
create database if not exists ssm_demo charset utf8;
-- 选择ssm_demo库
use ssm_demo;
-- 创建表
CREATE TABLE `mq_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- 插入测试数据
insert into mq_users values (1, 'MQ1','123456');
insert into mq_users values (2, 'MQ2', '123456');
insert into mq_users values (3, 'MQ3', '123456');
insert into mq_users values (4, 'MQ4', '123');
insert into mq_users values (5, 'MQ5', '123');
package com.mq.pojo;
public class user {
private Integer id;
private String name;
private String password;
public user() { }
public user(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "user [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
3. 在resources/mybatis/mapper目录下创建user实体类的映射文件—UserMapper.xml
<?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">
<!-- 订单表映射文件 namespace值为对应接口的全路径 -->
<mapper namespace="com.mq.dao.UserDao">
<!--
数据表列字段与pojo类属性映射关系
type:指定将查询的结果封装到哪个类pojo对象中
id:指定一个唯一表示resultMap的值
-->
<resultMap type="com.mq.pojo.User" id="userMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
</resultMap>
<!-- 1.查询所有user信息 id值为对应接口中方法的名字
resultMap:指定为resultMap标签的id值
-->
<select id="findAll" resultMap="userMap">
select * from mq_users
</select>
</mapper>
package com.mq.dao;
import java.util.List;
import com.mq.pojo.User;
public interface UserDao {
//查询所有user信息
public List<User> findAll();
}
5. 创建UserService接口,添加查询所有user信息的findAll方法
package com.mq.service;
import java.util.List;
import com.mq.pojo.User;
public interface UserService {
/**
* 1.查询所有user信息
*/
public List<User> findAll();
}
6. 创建UserService接口的实现类UserServiceImpl,实现父接口中的findAll方法,调用dao层的findAll方法查询所有订单信息
package com.mq.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mq.dao.UserDao;
import com.mq.pojo.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired //自动注入(由spring创建mapper对象并为属性赋值)
private UserDao userDao;
@Override
public List<User> findAll() {
//1.调用UserDao的findAll方法,查询所有user信息
List<User> list = userDao.findAll();
//2.将所有信息的List集合返回
return list;
}
}
7. 创建UserController类,提供userList方法:调用service层的findAll方法,查询所有user信息。
package com.mq.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mq.pojo.User;
import com.mq.service.UserService;
@Controller
public class UserController {
@Autowired //自动注入(由spring创建对象并为属性赋值)
private UserService userService;
@RequestMapping("/userList")
public String userList(Model model){
//1.调用UserService层的findAll方法
List<User> list = userService.findAll();
for (User user:list) {
System.out.println(user);
}
//2.将所有list集合存入Model中(存入request域中)
model.addAttribute("list", list);
return "mq_userList";
}
}
<%@ page pageEncoding="UTF-8"%>
<%-- 引入JSTL标签库 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mq_userList.jsp</title>
<style type="text/css">
table,tr,td{
border:2px solid red;
text-align:center;
border-collapse:collapse;
font-size: 20px;
padding:10px;
}
</style>
</head>
<body>
<table>
<c:forEach items="${ list }" var="users">
<tr>
<td>${ users.id }</td>
<td>${ users.name }</td>
<td>${ users.password }</td>
<td><a href="#">删除</a> | <a href="#">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
10. 在浏览器访问 http://localhost:8080/ssm_demo/userList
看到如下界面则说明SSM整合并测试成功
此时控制台输出如下:
------------------------------------谢谢------------------------------------------------------