此文章借鉴MyBatis官网,使用gradle构建项目,使用oracle数据库
本文代码地址见github仓库
NOTES:
在测试这个案例时碰到一个坑,就是数据库表名 与 核心表名重复的时候,查询会报错 找不到表名。此处修改user
表为users
表了
- 在
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'
}
- 配置
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
- 编写实体类
@Data
public class Users {
private Long id;
private String name;
private Integer age;
private String email;
}
- 编写Mapper类
@Mapper
public interface UserMapper extends BaseMapper<Users> {
}
- 编写测试类
@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);
}
}
项目结构:
转载请注明 原文地址