一、项目框架
pom.xml依赖
<?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>
<groupId>cn.yh</groupId>
<artifactId>yh-mp-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简化代码的工具包,针对实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis‐plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</project>
config包-分页插件配置文件
MybatisPlusConfig
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
controller层
package org.example.controller;
import org.example.pojo.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/addUser")
public void addUser(@RequestBody User user) {
userService.addUser(user);
}
@DeleteMapping("/deleteUserById/{id}")
public void deleteUserById(@PathVariable Long id) {
userService.deleteUserById(id);
}
@PutMapping("/updateUser")
public void updateUser(@RequestBody User user) {
userService.updateUser(user);
}
@GetMapping("/getUserById")
public User getUserById(@RequestParam Long id) {
return userService.getUserById(id);
}
@GetMapping("/getAllUser")
public List<User> getAllUser() {
return userService.getAllUser();
}
@PostMapping("/addUserList")
public void addUserList(@RequestBody List<User> users) {
userService.addUserList(users);
}
}
Mapper层
UserMapper
public interface UserMapper extends BaseMapper<User> {
User myFindUserById(Integer id); //自定义的查询方法,目的是练习传统Mybatis的使用
}
Pojo实体类
User
package org.example.pojo;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_user") //指定数据库表
public class User {
/**
* 指定主键
* <p>
* 使用该主键生成策略:@TableId(value = "id",type = IdType.ID_WORKER) 要注意下面内容
* 如果数据库id字段是bigint类型,那么java实体类中属性类型为java.long.Long,Integer位数远远不够了
*/
@TableId(value = "id")
private Long id;
// @TableField("user_name") //mybatis-plus默认开启驼峰匹配,可不写@TableField("user_name")
private String userName;
// @TableField("password")
private String password;
// @TableField("age")
private Integer age;
// @TableField("birthday")
private LocalDateTime birthday; //数据库中日期类型为datetime没有时分秒
}
service层
UserService
package org.example.service;
import org.example.pojo.User;
import java.util.List;
public interface UserService {
void addUser(User user);
void deleteUserById(Long id);
void updateUser(User user);
User getUserById(Long id);
List<User> getAllUser();
void addUserList(List<User> users);
}
UserService实现类UserServiceImp
package org.example.service;
import org.example.mapper.UserMapper;
import org.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.insert(user);
}
@Override
public void deleteUserById(Long id) {
userMapper.deleteById(id);
}
@Override
public void updateUser(User user) {
userMapper.updateById(user);
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public List<User> getAllUser() {
return userMapper.selectList(null);
}
@Override
public void addUserList(List<User> users) {
for (User user : users) {
userMapper.insert(user);
}
}
}
引导类/启动类
Application
@MapperScan("org.example.mapper") 扫描对应包下的接口并加到Spring的Ioc容器中。
package org.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
resources配置
UserMapper
<?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层对应的接口名-->
<mapper namespace="org.example.mapper.UserMapper">
<!--id是mapper层对应的接口名中对应的方法名-->
<select id="myFindUserById" resultType="User">
select *
from tb_user
where id = #{id}
</select>
</mapper>
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">
<configuration>
<!--设置驼峰匹配,MybatisPlus默认开启驼峰匹配-->
<!-- <settings>-->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/>-->
<!-- </settings>-->
</configuration>
application.yml
server:
port: 8082 #指定端口号
spring:
application: #服务名
name: local-mp
#数据源,连接自己电脑上的数据库
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #MySQL8版本及以上要加cj,此工程用的是MySQL8
username: root
password: "123456" #注意url有问题时可以尝试加时区,url中的test为数据库名
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
#日志
logging:
level:
root: debug
#设置mybatis-plus
mybatis-plus:
config-location: classpath:mybatis/mybatis-config.xml #配置文件
mapper-locations: classpath:mybatis/mapper/*.xml #设置mapper层对应的接口对应的mapper.xml的路径
type-aliases-package: org.example.pojo #设置别名,mapper.xml中resultType="cn.yh.mp.pojo.User"可省去包,即resultType="User"
#开启自动驼峰映射,注意:配置configuration.map-underscore-to-camel-case则不能配置config-location,可写到mybatis-config.xml中
# configuration:
# map-underscore-to-camel-case: true
二、Sql
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80033 (8.0.33)
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80033 (8.0.33)
File Encoding : 65001
Date: 30/04/2024 00:23:33
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int NULL DEFAULT NULL,
`birthday` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1784973424016379907 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (1784972199497822210, 'jack', '123', 25, '2024-04-29 00:00:00');
INSERT INTO `tb_user` VALUES (1784972897228660738, 'tom', '123', 24, '2024-04-29 00:00:00');
INSERT INTO `tb_user` VALUES (1784973068603641857, '优雅修改', 'password2', 30, '2024-04-29 00:00:00');
SET FOREIGN_KEY_CHECKS = 1;
三、Postman准备
postman准备
以person举例,在本项目中为user,步骤都一样,名字不同而已。
创建-Collections
创建请求-Request
请求介绍
1 新增:
请求方式:Post 请求路径:http://localhost:8082/user/addUser
传递数据:
{
"userName": "tom2",
"password": "123",
"age": 24,
"birthday": "2024-04-29T15:59:26.302"
}
2 删除:
请求方式:DELETE 请求路径:http://localhost:8082/user/deleteUserById/1784972199497822210
3 修改:
请求方式:Put 请求路径:http://localhost:8082/user/updateUser
{
"id": 1784973068603641857,
"userName": "优雅修改",
"password": "password2",
"age": 30,
"birthday": "2024-04-29T15:59:26.302"
}
4 查询:
请求方式:Get 请求路径:http://localhost:8082/user/getUserById?id=1784972897228660738
四、测试类
不想通过postman测试的,也可以在Test包中创建测试类自测(单元测试),测试类自测不通过controller及service。
MapperPlusTest
import cn.yh.mp.mapper.UserMapper;
import cn.yh.mp.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MapperPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void selectAll() {
List<User> users = userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
@Test
public void findUserById() {
User user = userMapper.myFindUserById(2);
System.out.println(user);
}
/**
* 插入数据
*/
@Test
public void testInsert() {
User user = new User();
user.setAge(20);
user.setEmail("test@yh.cn");
user.setName("曹操");
user.setUserName("caocao");
user.setPassword("123456");
userMapper.insert(user);
}
/**
* 根据id 批量删除
*/
@Test
public void testDeleteByIds() {
//根据id集合批量删除 DELETE FROM tb_user WHERE ID IN ( ? , ? , ? )
int result = this.userMapper.deleteBatchIds(Arrays.asList(1, 2, 4));
System.out.println("result = " + result);
}
/**
* 分页
*/
@Test
public void testPage() {
QueryWrapper<User> wrapper = new QueryWrapper<User>();
wrapper.eq("age", 20); //查询条件,age=20
IPage<User> page = new Page<>(1, 2);
IPage<User> userIPage = userMapper.selectPage(page, wrapper);
System.out.println("数据总条数:" + userIPage.getTotal());
System.out.println("总页数:" + userIPage.getPages());
}
/**
* 修改
*/
@Test
public void testUpdate() {
User user = new User();
user.setId(5L);
user.setAge(20);
user.setEmail("test@yh.cn");
user.setName("曹操");
user.setUserName("caocao");
user.setPassword("123");
userMapper.updateById(user);
// userMapper.update()
}
/**
* 模糊查询
*/
@Test
public void testLike() {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
// wrapper.like(User::getName, "曹"); //%曹%
wrapper.likeRight(User::getName, "曹"); //曹%
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
userMapper.selectList(wrapper);
}
/**
* 测试or,默认and拼接条件
*/
@Test
public void testOr() {
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName, "曹丕");
lambdaQueryWrapper.or(); //添加or条件
lambdaQueryWrapper.eq(User::getAge, 22);
/**
* SELECT id,user_name,password,name,age,email,birthday FROM tb_user
* WHERE name = ? OR age = ?
*/
List<User> userList = userMapper.selectList(lambdaQueryWrapper);
userList.forEach(System.out::println);
}
//代码功能:在name为"张三", "李四", "王五"中 筛选password=123 或者age>20的人。
@Test
public void testLogicSelect() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.and(wp -> wp
.eq("password", "123")
.or()
.ge("age", 20))
.in("name", "张三", "李四", "王五");
/**
* SELECT id,user_name,password,name,age,email,birthday FROM tb_user
* WHERE password = ? OR age >= ? AND name IN (?,?,?)
* 在 MySQL 中,AND 的优先级高于 OR。这意味着当一个查询语句中同时包含 AND 和 OR 运算符 时,AND 将首先执行,然后才是OR。
*/
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(user -> System.out.println(user));
}
}