springboot整合mybatisplus

POM:

<dependency>
  <groupId>com.baomidougroupId>
  <artifactId>mybatis-plus-boot-starterartifactId>
  <version>3.5.1version>
dependency>
<dependency>
  <groupId>mysqlgroupId>
  <artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
  <groupId>org.mybatis.spring.bootgroupId>
  <artifactId>mybatis-spring-boot-starterartifactId>
  <version>2.1.4version>
dependency>

如图所示,mybatis-plus-boot-starter 已经引入了jdbc 所以无需在引入 jdbc的依赖

springboot整合mybatisplus_第1张图片

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> {
}

包扫描:

springboot整合mybatisplus_第2张图片

测试:

@SpringBootTest
class Demo03242ApplicationTests {

    @Autowired
    UserPlusMapper userPlusMapper;

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

springboot整合mybatisplus_第3张图片

你可能感兴趣的:(leetcode,矩阵,算法)