MyBatis Plus 快速开始

此文章借鉴MyBatis官网,使用gradle构建项目,使用oracle数据库

本文代码地址见github仓库

NOTES

在测试这个案例时碰到一个坑,就是数据库表名 与 核心表名重复的时候,查询会报错 找不到表名。此处修改user表为users表了

  1. build.gradle文件中配置依赖
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.2.0'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
  1. 配置 application.yml
spring:
  datasource:
    url: jdbc:oracle:thin:@192.168.3.161:1521:jc
    username: jc
    password: 123456
    driver-class-name: oracle.jdbc.driver.OracleDriver
    maximum-pool-size: 30
  1. 编写实体类
@Data
public class Users {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
  1. 编写Mapper类
@Mapper
public interface UserMapper extends BaseMapper<Users> {
}
  1. 编写测试类
@SpringBootTest(classes = App.class)
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<Users> userList = userMapper.selectList(null);
        Assert.isTrue(userList.size()==5, "结果集不为5条");
        userList.forEach(System.out::println);
    }
}

项目结构:

image-20211219223218948

转载请注明 原文地址

posted @ 2021-12-19 22:34  WhaleFall541  阅读(56)  评论(0编辑  收藏  举报