亲宝软件园·资讯

展开

springboot整合mybatisplus

允许部分艺术家先富起来1 人气:0

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

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容! 

加载全部内容

相关教程
猜你喜欢
用户评论