欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis-plus基础使用介绍

程序员文章站 2022-07-09 19:16:13
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。官网:mybatis-plus官网1.创建user表及插入数据CREATE TABLE user(id BIGINT(20) NOT NULL COMMENT '主键ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',age INT(11) NULL DEFAULT NULL COMMENT...

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官网:mybatis-plus官网

1.创建user表及插入数据

CREATE TABLE user ( id BIGINT(20) NOT NULL COMMENT '主键ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (id) ); INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com'); 

2.创建一个springboot工程,导入依赖
mybatis-plus基础使用介绍

<?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 https://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.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

3.创建pojo包,新建user类

@Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; private String email; } 

4.创建mapper包,新建UserMapper接口

@Repository public interface UserMapper extends BaseMapper<User> { } 

5.在application.yml中配置数据库信息

spring: datasource: username: root
    password: 123456 url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver 

6.在主启动类添加@MapperScan(“com.mapper”)注解扫描

@SpringBootApplication @MapperScan("com.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 

7.在测试类中测试

@SpringBootTest class DemoApplicationTests { @Autowired private UserMapper userMapper; @Test void contextLoads() { //参数Wrapper是条件构造器,null表示查看全部用户 List<User> users = userMapper.selectList(null); users.forEach(System.out::println); } } 

8.测试结果
mybatis-plus基础使用介绍

9.配置日志
前面已经完成了快速入门,接下来配置日志。
在application.yml中添加配置

# 配置日志
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 

测试输出

Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@50734cea] was not registered for synchronization because synchronization is not active 2020-08-16 11:04:26.166 INFO 40306 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-08-16 11:04:26.381 INFO 40306 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. JDBC Connection [HikariProxyConnection@449966439 wrapping com.mysql.jdbc.JDBC4Connection@608eb42e] will not be managed by Spring ==> Preparing: SELECT id,name,email,age FROM user ==> Parameters: <== Columns: id, name, email, age <== Row: 1, Jone, test1@baomidou.com, 18 <== Row: 2, Jack, test2@baomidou.com, 20 <== Row: 3, Tom, test3@baomidou.com, 28 <== Row: 4, Sandy, test4@baomidou.com, 21 <== Row: 5, Billie, test5@baomidou.com, 24 <== Total: 5 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@50734cea] User(id=1, name=Jone, age=18, email=test1@baomidou.com) User(id=2, name=Jack, age=20, email=test2@baomidou.com) User(id=3, name=Tom, age=28, email=test3@baomidou.com) User(id=4, name=Sandy, age=21, email=test4@baomidou.com) User(id=5, name=Billie, age=24, email=test5@baomidou.com) 2020-08-16 11:04:26.439 INFO 40306 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2020-08-16 11:04:26.442 INFO 40306 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2020-08-16 11:04:26.443 INFO 40306 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' Process finished with exit code 0 

10.测试插入

@Test public void testInsert(){ User user = new User(); user.setName("haha"); user.setAge(6); user.setEmail("libaitian@qq.com"); int insert = userMapper.insert(user);//帮我们自动生成ID System.out.println(insert); System.out.println(user);//id会自动回填 } Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1edac3b4] was not registered for synchronization because synchronization is not active 2020-08-16 11:15:02.161 INFO 40321 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-08-16 11:15:02.387 INFO 40321 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. JDBC Connection [HikariProxyConnection@1641528161 wrapping com.mysql.jdbc.JDBC4Connection@33f81280] will not be managed by Spring ==> Preparing: INSERT INTO user ( id, name, email, age ) VALUES ( ?, ?, ?, ? ) ==> Parameters: 1294835027789930497(Long), haha(String), libaitian@qq.com(String), 6(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1edac3b4] 1 User(id=1294835027789930497, name=haha, age=6, email=libaitian@qq.com) 2020-08-16 11:15:02.446 INFO 40321 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2020-08-16 11:15:02.450 INFO 40321 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2020-08-16 11:15:02.450 INFO 40321 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' Process finished with exit code 0 

从上面结果,我们发现它会为我们自动生成id,数据库插入的id默认值为:全局的唯一id。

主键生成策略
分布式系统唯一id生成:分布式系统唯一id生成
雪花算法:
snowflake是Twitter开源的分布式ID生成算法,结果是一个long型的ID。其核心思想是:使用41bit作为毫秒数,10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),最后还有一个符号位,永远是0。具体实现的代码可以参看https://github.com/twitter/snowflake。

本文地址:https://blog.csdn.net/small_apples/article/details/108033384

相关标签: mybatis