查看此文章前强烈建议先看这篇文章:Java江湖路 | 专栏目录
该文章纪录的是SpringBoot快速集成MyBatis Plus,每一步都有记录,争取每一位看该文章的小伙伴都能操作成功。达到自己想要的效果~
MyBatis-Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MybatisPlus可以节省大量时间,所有的CRUD代码都可以自动化完成
特性
MyBatisPlus官方网站:https://baomidou.com/
1、先准备好数据库环境
CREATE DATABASE mybatisplus;
USE mybatisplus;
CREATE TABLE eric_user(
`id` INT AUTO_INCREMENT,
`name` VARCHAR(255) COMMENT '名称',
PRIMARY KEY (`id`)
);
INNER INTO eric_user(`name`) VALUES('Eric'),('blue');
INSERT INTO `eric_user`(`id`,`name`) VALUES
(1,'Eric'),
(2,'Jack'),
(3,'Lucy');
3、引入MyBatisPlus相关依赖(因为要操作数据库,所以这里也引入了MySQL依赖)
<!--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>
</dependency>
4、修改配置文件
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT%2B8
username: root
password: root
#开启日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1、创建一个实体类
package com.eric.springbootmybatisplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @author Eric
* @date 2023-08-01 15:38
*/
@Data
@TableName(value = "eric_user")
public class EricUser {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "name")
private String name;
}
2、Mapper层
package com.eric.springbootmybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.eric.springbootmybatisplus.entity.EricUser;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Eric
* @date 2023-08-01 15:39
*/
@Mapper
public interface EricUserMapper extends BaseMapper<EricUser> {
}
3、service层
package com.eric.springbootmybatisplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.eric.springbootmybatisplus.entity.EricUser;
/**
* @author Eric
* @date 2023-08-01 15:40
*/
public interface EricUserService extends IService<EricUser> {
}
impl
package com.eric.springbootmybatisplus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eric.springbootmybatisplus.entity.EricUser;
import com.eric.springbootmybatisplus.mapper.EricUserMapper;
import com.eric.springbootmybatisplus.service.EricUserService;
import org.springframework.stereotype.Service;
/**
* @author Eric
* @date 2023-08-01 15:42
*/
@Service
public class EricUserServiceImpl extends ServiceImpl<EricUserMapper, EricUser> implements EricUserService {
}
4、controller
package com.eric.springbootmybatisplus.controller;
import com.eric.springbootmybatisplus.entity.EricUser;
import com.eric.springbootmybatisplus.service.EricUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Eric
* @date 2023-08-01 15:43
*/
@RestController
@RequestMapping("/eric")
public class EricUserController {
@Autowired
private EricUserService userService;
@GetMapping("")
public List<EricUser> getEricUserList(){
List<EricUser> list = userService.list();
return list;
}
}
此时运行项目,访问 IP+端口/接口地址
例如:http://127.0.0.1:8080/eric
怎么样,是不是特别的方便和简单~