提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
如何快速上手使用一次Mybatis-plus?
话不多说 直接开始
官方文档
https://baomidou.com/pages/226c21/#%E7%BC%96%E7%A0%81
零、配置实体类(和自己数据库表对应)
package com.example.jsr303.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* 学习测试JSR303
*/
@Data
@TableName("test")
public class TestEntity implements Serializable {
//忽略掉 设置序列化和反序列化的serialVersionUID
//想了解一下可以去这篇博客 https://blog.csdn.net/qq_45503106/article/details/107950914
private static final long serialVersionUID = 1L;
/**
* test表主键ID
*/
@TableId
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 邮箱
*/
private String email;
}
一定记得使用@TableName 绑定你这个数据库的表
否则按照 数据库名+实体类 名去找你的表
一般都找不到
一、导入依赖
<dependencies>
<!-- web mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql链接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok简化插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- myabtis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
二、配置配置文件yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jsr303?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
server:
port: 8082
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三 配置Mapper Service Controller三层架构
Mapper层
package com.example.jsr303.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.jsr303.entity.TestEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestDao extends BaseMapper<TestEntity> {
}
Service层
package com.example.jsr303.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.jsr303.entity.TestEntity;
import org.springframework.stereotype.Service;
public interface TestService extends IService<TestEntity> {
}
ServiceImpl层
package com.example.jsr303.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jsr303.dao.TestDao;
import com.example.jsr303.entity.TestEntity;
import com.example.jsr303.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {
}
Controller层
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/list")
public List<TestEntity> selectAll(){
return testService.list();
}
}
常用的Service接口官方文档
https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F%A3