1、准备数据库中的表及表中的数据
/* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.6.11 : Database - mp ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`mp` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `mp`; /*Table structure for table `tbl_employee` */ DROP TABLE IF EXISTS `tbl_employee`; CREATE TABLE `tbl_employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(50) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `age` int(11) DEFAULT NULL, `version` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8; /*Data for the table `tbl_employee` */ insert into `tbl_employee`(`id`,`last_name`,`email`,`gender`,`age`,`version`) values (1,'Tom','[email protected]','1',22,1),(2,'Jerry','[email protected]','0',25,1),(3,'Black','[email protected]','1',30,1),(4,'White','[email protected]','0',35,1),(14,'小邹','[email protected]','0',19,1),(18,'jack','[email protected]','1',22,1),(22,'abc','[email protected]','1',23,2); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
2、初始化一个springboot项目
如何初始化一个springboot项目?
点击https://blog.csdn.net/Zou_05/article/details/115869941
注:勾选Lombok插件,简化开发
3、在pom.xml中加入mybatis-plus及mysql数据库的依赖
com.baomidou mybatis-plus-boot-starter 3.4.2 mysql mysql-connector-java 6.0.5 com.alibaba druid 1.1.10
4、将application.properties文件修改为application.yml,并添加mysql数据库的连接信息(mp为我的数据库名)
server: port: 8081 servlet: context-path: / spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver
5、编写实体类(使用lombok简化代码)
@TableName指定数据库的表名
@TableId(type = IdType.AUTO)指定id为自增
package com.zou.demo1.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.Version; import lombok.Data; /** * @author: 邹祥发 * @date: 2021/4/21 08:49 */ @Data @TableName("tbl_employee") public class User { @TableId(type = IdType.AUTO) private Integer id; private String lastName; private String email; private Integer gender; private Integer age; @Version private Integer version; }
6、编写Mapper类
package com.zou.demo1.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zou.demo1.entity.User; /** * @author: 邹祥发 * @date: 2021/4/21 08:55 */ public interface UserMapper extends BaseMapper{ }
7、因测试需要加入一些依赖,完整的pom.xml如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.5 com.zou demo1 0.0.1-SNAPSHOT demo1 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.4.2 mysql mysql-connector-java 6.0.5 com.alibaba druid 1.1.10 junit junit org.springframework.boot spring-boot-test org.springframework spring-test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
8、进行功能测试
注:@MapperScan(“com.zou.demo1.mapper”)指定扫描的mapper包
(1)查询测试:
package com.zou.demo1.test; import com.zou.demo1.entity.User; import com.zou.demo1.mapper.UserMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; /** * @author: 邹祥发 * @date: 2021/4/21 08:57 */ @RunWith(SpringRunner.class) @SpringBootTest @MapperScan("com.zou.demo1.mapper") public class SampleTest { @Autowired private UserMapper userMapper; @Test public void selectTest() { System.out.println(("----- selectAll method test ------")); ListuserList = userMapper.selectList(null); Assert.assertEquals(7, userList.size()); userList.forEach(System.out::println); } }
测试结果:
(2)插入测试:
@Test public void insertTest(){ System.out.println(("----- insertOne method test ------")); User user = new User(); user.setLastName("uzi"); user.setEmail("[email protected]"); user.setGender(0); user.setAge(24); user.setVersion(1); int i = userMapper.insert(user); System.out.println(i); }
测试结果:
(3)更新测试: 注:不能设置version的值(因为没有引入乐观锁的插件),否则会抛出mybatis-plus乐观锁异常
具体配置请点击:
https://mp.baomidou.com/guide/interceptor-optimistic-locker.html#optimisticlockerinnerinterceptor
@Test public void updateTest(){ System.out.println(("----- updateOneById method test ------")); User user = new User(); user.setId(24); user.setLastName("mlxg"); user.setEmail("[email protected]"); user.setGender(1); user.setAge(23); int i = userMapper.updateById(user); System.out.println(i); }
测试结果:
(4)删除测试:
@Test public void deleteTest(){ System.out.println(("----- deleteOneById method test ------")); int i = userMapper.deleteById(24); System.out.println(i); }
测试结果:
到此这篇关于使用springboot整合mybatis-plus实现数据库的增删查改示例的文章就介绍到这了,更多相关springboot mybatis-plus增删查改内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!