SSM整合SpringMVC + Spring + Mybatis整合详细教程
Posted 小新要变强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合SpringMVC + Spring + Mybatis整合详细教程相关的知识,希望对你有一定的参考价值。
前言
本文为 SpringMVC + Spring + Mybatis整合教程 相关详细介绍,从数据库数据准备
、相关依赖包添加
、创建项目基本结构和配置框架
,到Mybatis层的编写
、Spring层的编写
、SpringMVC层的编写
,再到前端页面编写
等,最后进行运行与测试
,按步骤对SSM框架整合进行详细的解析~
📌博主主页:小新要变强 的主页
👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~
👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~
👉Java微服务开源项目可参考:企业级Java微服务开源项目(开源框架,用于学习、毕设、公司项目、私活等,减少开发工作,让您只关注业务!)
目录
文章标题
一、数据库数据准备
运行以下sql脚本:
CREATE DATABASE `ssm`;
USE `ssm`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `ssm`.`users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户名',
`sex` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '性别,0男,1女',
`tel` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '联系电话',
`mail` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮件',
`status` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '状态,0正常,1禁用',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
INSERT INTO `users`(`user_name`, `sex`, `tel`, `mail`, `status`) VALUES
('admin', '0', '123456', '', '0'),
('user1', '0', '123456', '', '0');
创建一个名为ssm的数据库,并在此数据库中创建users数据表,表中包含的属性:“用户名”,“性别”,“联系电话”,“邮件”,“状态”。
二、添加相关依赖包
🍀新建一个Maven项目(名称叫ssm-user),并添加web支持后,添加以下pom依赖。
pom.xml:
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<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.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies>
🍀进行Maven资源过滤设置
pom.xml:
<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>
三、创建项目基本结构和配置框架
- com.pojo
- com.dao
- com.service
- com.controller
四、Mybatis层的编写
🍀(1)添加数据库配置文件 database.properties,方便管理
database.properties:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/ssm?serverTimezone=UTC&&useUnicode=true&&characterEncoding=utf8&&useSSL=false
jdbc.username=root
jdbc.password=root
🍀(2)添加实体Users,使用lombok来创建get/set方法
Users.java:
package com.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 用户实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users
private Integer userId;
private String userName;
private String sex;
private String tel;
private String mail;
private String status;
🍀(3)编写Dao层的 Mapper 接口
UsersMapper.java:
package com.dao;
import com.pojo.Users;
import java.util.List;
/**
* 用户 mapper
*/
public interface UsersMapper
/**
* 查询所有用户
*/
List<Users> queryAll();
/**
* 根据ID查询用户
*/
Users queryById(int id);
/**
* 添加用户
*/
int addUser(Users users);
/**
* 修改用户
*/
int updateUser(Users users);
/**
* 根据ID删除用户
*/
int deleteById(int id);
🍀(4)编写接口对应的 Mapper.xml 文件
UsersMapper.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">
<mapper namespace="com.dao.UsersMapper">
<resultMap id="users" type="com.pojo.Users">
<result column="user_id" property="userId" />
<result column="user_name" property="userName" />
<result column="sex" property="sex" />
<result column="tel" property="tel" />
<result column="mail" property="mail" />
<result column="status" property="status" />
</resultMap>
<select id="queryAll" resultMap="users">
SELECT * FROM `users`
</select>
<select id="queryById" resultMap="users">
SELECT * FROM `users`
where user_id = #id
</select>
<insert id="addUser">
INSERT INTO `users`(`user_name`, `sex`, `tel`, `mail`, `status`)
VALUES
(#userName, #sex, #tel, #mail, #status)
</insert>
<update id="updateUser">
UPDATE `users` SET
`user_name` = #userName,
`sex` = #sex,
`tel` = #tel,
`mail` = #mail
WHERE `user_id` = #userId
</update>
<delete id="deleteById">
delete from users where user_id=#userId
</delete>
</mapper>
🍀(5)编写Service层的接口和实现类
UsersService.java:
package com.service;
import com.pojo.Users;
import java.util.List;
public interface UsersService
/**
* 查询所有用户
*/
List<Users> queryAll();
/**
* 根据ID查询用户
*/
Users queryById(int id);
/**
* 添加用户
*/
int addUser(Users users);
/**
* 修改用户
*/
int updateUser(Users users);
/**
* 根据ID删除用户
*/
int deleteById(int id);
UsersServiceImpl.java:
package com.service.impl;
import com.dao.UsersMapper;
import com.pojo.Users;
import com.service.UsersService;
import java.util.List;
public class UsersServiceImpl implements UsersService
//调用dao层的操作,设置一个set接口,方便Spring管理
private UsersMapper usersMapper;
public void setUsersMapper(UsersMapper usersMapper)
this.usersMapper = usersMapper;
@Override
public List<Users> queryAll()
return usersMapper.queryAll();
@Override
public Users queryById(int id)
return usersMapper.queryById(id);
@Override
public int addUser(Users users)
users.setStatus("0");
return usersMapper.addUser(users);
@Override
public int updateUser(Users users)
return usersMapper.updateUser(users);
@Override
public int deleteById(int id)
return usersMapper.deleteById(id);
五、Spring层的编写
🍀编写Spring整合Mybatis的相关的配置文件
配置Spring整合MyBatis,我们这里数据源使用c3p0连接池。
spring-dao.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: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
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:config/database.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<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以上是关于SSM整合SpringMVC + Spring + Mybatis整合详细教程的主要内容,如果未能解决你的问题,请参考以下文章
04_SSM框架整合(Spring+SpringMVC+MyBatis)
SSM整合SpringMVC + Spring + Mybatis整合详细教程
后端SSM(Spring + SpringMVC + Mybatis)框架整合
转SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)