springboot整合mybatisplus

  • Post author:
  • Post category:其他




POM:

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.5.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.4</version>
</dependency>

如👇图所示,

mybatis-plus-boot-starter

已经引入了jdbc 所以无需在引入 jdbc的依赖

image-20220325101552156



application.yaml:

只需要 填写数据源配置即可 。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/kaoyan?serverTimezone=UTC
    password: admin
    username: root
    driver-class-name: com.mysql.cj.jdbc.Driver



POJO:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName(value = "user") // 指定查询的表名为 user 
public class UserPlus {
    // 以下为 mybatis-plus测试
    private Integer id;
    private String name;
    private Integer age;
    private String email;
}



mapper接口:

特别简洁

package com.hoan.demo03242.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hoan.demo03242.bean.UserPlus;

public interface UserPlusMapper extends BaseMapper<UserPlus> {
}



包扫描:

image-20220325101955093



测试:

@SpringBootTest
class Demo03242ApplicationTests {

    @Autowired
    UserPlusMapper userPlusMapper;

    @Test
    void contextLoads() {
        UserPlus user = userPlusMapper.selectById(1);
        System.out.println(user.toString());
    }
}

image-20220325104147506



版权声明:本文为qq_44220795原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。