Springboot+MybatisPlus入门案例(postman测试)

news/2024/11/3 1:18:42/

 一、项目框架

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 {/*** 分页插件*/@Beanpublic 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 {@Autowiredprivate 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 {@Autowiredprivate UserMapper userMapper;@Overridepublic void addUser(User user) {userMapper.insert(user);}@Overridepublic void deleteUserById(Long id) {userMapper.deleteById(id);}@Overridepublic void updateUser(User user) {userMapper.updateById(user);}@Overridepublic User getUserById(Long id) {return userMapper.selectById(id);}@Overridepublic List<User> getAllUser() {return userMapper.selectList(null);}@Overridepublic 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 mapperPUBLIC "-//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_userwhere id = #{id}</select></mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//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,此工程用的是MySQL8username: rootpassword: "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 TransferSource Server         : localhostSource Server Type    : MySQLSource Server Version : 80033 (8.0.33)Source Host           : localhost:3306Source Schema         : testTarget Server Type    : MySQLTarget Server Version : 80033 (8.0.33)File Encoding         : 65001Date: 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 {@Autowiredprivate UserMapper userMapper;@Testpublic void selectAll() {List<User> users = userMapper.selectList(null);for (User user : users) {System.out.println(user);}}@Testpublic void findUserById() {User user = userMapper.myFindUserById(2);System.out.println(user);}/*** 插入数据*/@Testpublic 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 批量删除*/@Testpublic 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);}/*** 分页*/@Testpublic void testPage() {QueryWrapper<User> wrapper = new QueryWrapper<User>();wrapper.eq("age", 20); //查询条件,age=20IPage<User> page = new Page<>(1, 2);IPage<User> userIPage = userMapper.selectPage(page, wrapper);System.out.println("数据总条数:" + userIPage.getTotal());System.out.println("总页数:" + userIPage.getPages());}/*** 修改*/@Testpublic 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()}/*** 模糊查询*/@Testpublic 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拼接条件*/@Testpublic 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的人。@Testpublic 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));}}


http://www.ppmy.cn/news/1450512.html

相关文章

TensorFlow框架介绍-深度学习

TensorFlow是一个开源机器学习框架&#xff0c;由Google开发并维护。它是用于构建和训练机器学习模型的一个强大工具。TensorFlow的核心概念是张量&#xff08;Tensor&#xff09;和计算图&#xff08;Graph&#xff09;。 张量是多维数组的一种泛化形式&#xff0c;可以是标量…

富格林:细节决定能否安全出金

富格林悉知&#xff0c;投资者都希望在现货黄金交易市场中获利&#xff0c;但并非所有投资者都能实现获利的心愿&#xff0c;有时候忽略一些细节问题也会影响最终的投资效果。投资者应该注重细节实现安全出金才是我们进行投资的最终目标。下面富格林将总结一些注重细节实现安全…

基于SSM的“一汽租车辆共享平台”的设计与实现(源码+数据库+文档+PPT)

基于SSM的“一汽租车辆共享平台”的设计与实现&#xff08;源码数据库文档PPT) 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SSM 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 登录界面 租车界面 订单管理界面 财务报表界面 理赔界面 …

CMUS狮身人面像(六)-调整语音识别准确性

调整语音识别准确性 精度差的原因测试数据库设置运行测试 语音识别的准确性并不总是很高。 首先&#xff0c;重要的是要了解您的准确性是否只是低于预期&#xff0c;还是总体上非常低。如果总体精度非常低&#xff0c;则您很可能错误配置了解码器。如果低于预期&#xff0c;可…

android studio 4.2.1运行java文件报错

当运行某个带main函数的java文件报这个错误的时候 Could not create task :app:Test.main(). > SourceSet with name main not found. 解决办法&#xff1a;在工程的.idea下的.gradlew.xml文件下添加 <option name"delegatedBuild" value"false"…

querySelectorAll与getElementsByTagName等区别

1&#xff0c;querySelector 获取的是nodeList,可直妆forEach循环 2&#xff0c;getElement获取到的是HTMLCollection&#xff0c;需要使用Array.from转换为数组后才可遍历&#xff0c;当然&#xff0c;可以直接用for&#xff0c;不需要转换 3&#xff0c;querySelector获取到…

C++中list的使用

文章目录 一、 list简介二、 构造函数1. 默认构造函数2. 拷贝构造3. 迭代器区间初始化4. 插入n个值为x的数据5. 代码示例 三、 容量和元素访问1. empty()2. size()3. max_size()3. back()4. front()5. 代码示例 四、 增删查改1. push_back()2. push_front()3. emplace_back()4.…

Spark持久化、broadcast广播变量和accumulator累加器

持久化操作 什么是持久化&#xff0c;为什么要持久化 Spark中最重要的功能之一是跨操作在内存中持久化&#xff08;或缓存&#xff09;数据集。当您持久化RDD时&#xff0c;每个节点将其计算的任何分区存储在内存中&#xff0c;并在该数据集&#xff08;或从该数据集派生的数…