项目:SpringBoot整合Mybatis的增删改查
1.pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>11-spring-boot-springmvc-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--JDBC-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
2.application.properties文件:
#项目端口配置
server.port=8080
server.address=0.0.0.0
#Mysql数据源配置
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.bjsxt.pojo
3.mapper文件:
public interface UsersMapper {
//添加用户
void insertUser(Users users);
//查询用户
List<Users> selectUsers();
//更新前之根据用户ID查询用户
Users selectUserByUserid(int userid);
//更新用户
void updateUser(Users users);
//删除用户
void delectUser(int userid);
}
<?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.bjsxt.mapper.UsersMapper">
<!--添加-->
<insert id="insertUser" parameterType="users">
insert into users(username,userage) values (#{username},#{userage})
</insert>
<!--查询所有-->
<select id="selectUsers" resultType="users">
select * from users
</select>
<!--根据ID查询-->
<select id="selectUserByUserid" resultType="users">
select * from users where userid=#{value}
</select>
<!--更新-->
<update id="updateUser" parameterType="users">
update users set username=#{username},userage=#{userage} where userid=#{userid}
</update>
<!--删除-->
<delete id="delectUser" >
delete from users where userid=#{value}
</delete>
</mapper>
4.service、serviceimpl
public interface UsersService {
//添加用户
void addUser(Users users);
//查询用户
List<Users> findAll();
//根据ID查询用户
Users findUserById(int userid);
//更新
void updateUser(Users users);
//删除
void removeUser(int userid);
}
package com.bjsxt.service.impl;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
@Override
public List<Users> findAll() {
return this.usersMapper.selectUsers();
}
@Override
public Users findUserById(int userid) {
return this.usersMapper.selectUserByUserid(userid);
}
@Override
public void updateUser(Users users) {
this.usersMapper.updateUser(users);
}
@Override
public void removeUser(int userid) {
this.usersMapper.delectUser(userid);
}
}
5.controller:
package com.bjsxt.controller;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/*用户*/
@RequestMapping("/addUsers")
public String addUsers(Users users){
this.usersService.addUser(users);
return "redirect:/users/findAll";
}
/*查询所有用户*/
@RequestMapping("/findAll")
public String findAll(Model model){
List<Users> list = this.usersService.findAll();
model.addAttribute("list",list);
return "show";
}
/*根据ID查询用户*/
@RequestMapping("/findById")
public String findById(Model model,int userid){
Users user = this.usersService.findUserById(userid);
model.addAttribute("user",user);
return "update";
}
/*更新用户*/
@RequestMapping("/updateUser")
public String updateUser(Model model,Users users){
this.usersService.updateUser(users);
return "redirect:/users/findAll";
}
/*删除用户*/
@RequestMapping("/delUser")
public String delUser(Model model,int userid){
this.usersService.removeUser(userid);
return "redirect:/users/findAll";
}
}
6.html:写在resource/templates文件下,通过templates渲染
add.html:
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/users/addUsers}" method="post">
用户姓名:<input type="text" name="username"><br>
用户年龄:<input type="text" name="userage"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
show.html:
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1px" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>操作</th>
</tr>
<tr th:each="user :${list}">
<td th:text="${user.userid}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.userage}"></td>
<td><a th:href="@{/users/findById(userid=${user.userid})}"> 更新</a>
<a th:href="@{/users/delUser(userid=${user.userid})}"> 删除</a>
</td>
</tr>
</table>
</body>
</html>
update.html:
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>更改回显</title>
</head>
<body>
<form th:action="@{/users/updateUser}" method="post">
<input type="hidden" th:field="${user.userid}">
用户姓名:<input type="text" name="username" th:field="${user.username}"><br>
用户年龄:<input type="text" name="userage" th:field="${user.userage}"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
7.启动类:
package com.bjsxt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.bjsxt.mapper") //@MapperScan 用户扫描MyBatis的Mapper
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}