主要用Mybatis-Plus实现数据库curd操作
1、学习内容
- mp是什么
- mybatis的一个增强的工具,只做增强,不做改变
- mp实现添加修改删除查询
- mp自动填充 乐观锁
- mp逻辑删除
- mp分页查询
2、Mybatis-Plus 入门
案例:实现mp来进行数据查询
2.1 创建数据库
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.2 创建一个Springboot项目
2.3导入相关的依赖
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2.4 配置数据库信息
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
2.5 实体类
User实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper(重点使要继承BaseMapper)
/**
* 使UserMapper 继承 BaseMapper 可以调用 BaseMapper 中的CRUD方法
*/
@Repository
public interface UserMapper extends BaseMapper<User> {
}
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
/**
* @MapperScan 的目的使为了找到动态生成的mapper对象
*/
@SpringBootApplication
@MapperScan("com.jcl.demomptest.mapper")
public class DemomptestApplication {
public static void main(String[] args) {
SpringApplication.run(DemomptestApplication.class, args);
}
}
DemomptestApplicationTests
package com.jcl.demomptest;
import com.jcl.demomptest.entity.User;
import com.jcl.demomptest.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class DemomptestApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void findAll(){
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
@Test
void contextLoads() {
}
}
查询结果
在application.properties 加入查看日志的配置
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl