myBatisPlus-概述和基本使用
概述
官方地址:https://mp.baomidou.com/guide/
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
基本使用
创建示例表
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 )
);
紧接着插入示例数据 SQL 如下
INSERT INTO USER ( id, NAME, age, email )
VALUES
( 1, '张三', 18, 'zs@qq.com' ),
( 2, '李四', 20, 'ls@qq.com' ),
( 3, '王五', 28, 'ww@qq.com' ),
( 4, '赵六', 21, 'zl@qq.com' ),
( 5, '周七', 24, 'zq@qq.com' );
创建 Spring Boot 工程
可参考:https://www.cnblogs.com/BNTang/articles/13772661.html
当然也可以使用创建 Spring Boot 的初始化工具如下图,我使用的就是这种方式
导入所需依赖
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.it6666</groupId>
<artifactId>mybatisPlus_Proa</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
在配置文件当中, 添加数据库配置信息,如下修改 application.properties
logging.pattern.console=%clr(%5p) %clr(-){faint} %clr(%-80.80logger{79}){cyan} %clr(:) %m%n
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
创建实体类
/**
* @author BNTang
*/
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写 Mapper 接口
/**
* @author BNTang
**/
@Repository
public interface UserMapper extends BaseMapper<User> {
}
在启动类当中, 添加 mapperScan
/**
* @author BNTang
*/
@MapperScan("top.it6666.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
添加测试类
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void selectAll() {
// 查询所有
List<User> users = userMapper.selectList(null);
// 打印结果
users.forEach(System.out::println);
}
}
配置 SQL 输出日志
也就是配置输出执行的 SQL
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
如上的实体类中我们是没有指定一个注解的,那个注解就是 TableName
是用来指定当前实体类对应的是那一张表,如果不指定默认就是以类名的首字母小写来作为表名,如果有则成功,没有运行则会报错