Skip to content

Commit

Permalink
Finish configuring mybatis-plus
Browse files Browse the repository at this point in the history
In this commit, we add the `mybatis-plus` dependency to the `pom.xml`
file, and configure the `mybatis-plus` in the `application-dev.yml`
file. We also add the `UserMapper` interface and `UserPO` class to the
project for testing. The `UserMapper` interface is used to define the
database operations (the real operations will generated by
`mybatis-plus`), and the `UserPO` class is used to define the user
entity. We use `spring-boot-test` to test this function (I've add one
item in the `t_user` table).

This solved #28.
  • Loading branch information
Kaiser-Yang committed Aug 20, 2024
1 parent a486dfa commit 157992a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/cmipt/gcs/GcsApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package edu.cmipt.gcs;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("edu.cmipt.gcs.dao")
public class GcsApplication {

public static void main(String[] args) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/edu/cmipt/gcs/dao/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.cmipt.gcs.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import edu.cmipt.gcs.pojo.UserPO;

public interface UserMapper extends BaseMapper<UserPO> {
}
11 changes: 11 additions & 0 deletions src/main/java/edu/cmipt/gcs/pojo/UserPO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package edu.cmipt.gcs.pojo;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("t_user")
public record UserPO(@TableId Long pkUserId, String username, String email, String userPassword, LocalDateTime gmtCreated,
LocalDateTime gmtUpdated, LocalDateTime gmtDeleted) {
}
1 change: 0 additions & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
debug: true
spring:
datasource:
druid:
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
debug: true
spring:
datasource:
druid:
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.cmipt.gcs.controller.dao;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.baomidou.mybatisplus.core.toolkit.Assert;

import edu.cmipt.gcs.dao.UserMapper;
import edu.cmipt.gcs.pojo.UserPO;

@SpringBootTest
public class UserMapperTest {

@Autowired
private UserMapper userMapper;

@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<UserPO> userList = userMapper.selectList(null);
Assert.isTrue(1 == userList.size(), "");
userList.forEach(System.out::println);
}
}

0 comments on commit 157992a

Please sign in to comment.