springBoot整合mybatis-plus

简介

MyBatis-Plus是一个Mybatis增强版工具,在MyBatis上扩充了其它功能没有改变基本功能,也就是只做封装增强不做改变,为了简化开发提交效率而存在,不需要写XML等sql语句就可实现

官方文档:https://mp.baomidou.com/guide/

工程搭建

1.创建springboot项目

2.依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
            <scope>compile</scope>
        </dependency>
        <!--Mybatis-Plus 注意版本-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--数据库连接驱动 连接配置修改时间-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

3.准备数据库和数据

CREATE TABLE `user_test` (
  `id` int DEFAULT NULL,
  `username` varchar(60) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `age` int DEFAULT NULL,
  `tel` int DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  `version` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;


INSERT INTO `rich`.`user_test`(`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) VALUES (1, '张三', 18, 180, NULL, NULL, NULL);
INSERT INTO `rich`.`user_test`(`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) VALUES (2, '李四', 20, 137, NULL, NULL, NULL);

4.yaml配置

spring:
  datasource:
    #    驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/rich?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

5.创建实体类

@Data
public class UserTest {

    private int id;

    private String username;

    private int age;

    private int tel;

    private Date create_time;


    private Date update_time;

    private int version;

}

6.创建Mapper接口

直接继承BaseMapper

public interface UserTestMapper extends BaseMapper<UserTest> {

}

7.创建controller

@RestController
@RequestMapping("/userTest")
public class UserTestController {

    @Autowired
    private UserTestMapper userTestMapper;

    @RequestMapping("/list")
    public List<UserTest> queryUserTest(){
        List<UserTest> userTests = userTestMapper.selectList(null);
        return userTests;
    }


}

8.测试

参考文章:

SpringBoot整合MyBatis-Plus各种使用点超级详细_q736317048的博客-CSDN博客_springbootmybatisplus整合

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA菜鸟程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值