From 157992af75f91d1c0050a927ea120e5354fb871e Mon Sep 17 00:00:00 2001 From: Kaiser-Yang <624626089@qq.com> Date: Tue, 20 Aug 2024 20:44:39 +0800 Subject: [PATCH] Finish configuring mybatis-plus 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. --- pom.xml | 5 ++++ .../java/edu/cmipt/gcs/GcsApplication.java | 2 ++ .../java/edu/cmipt/gcs/dao/UserMapper.java | 8 ++++++ src/main/java/edu/cmipt/gcs/pojo/UserPO.java | 11 ++++++++ src/main/resources/application-dev.yml | 1 - src/main/resources/application-test.yml | 1 - .../gcs/controller/dao/UserMapperTest.java | 27 +++++++++++++++++++ 7 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/main/java/edu/cmipt/gcs/dao/UserMapper.java create mode 100644 src/main/java/edu/cmipt/gcs/pojo/UserPO.java create mode 100644 src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java diff --git a/pom.xml b/pom.xml index a45a587..3769d6e 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,11 @@ org.springframework.boot spring-boot-starter-jdbc + + com.baomidou + mybatis-plus-spring-boot3-starter + 3.5.7 + diff --git a/src/main/java/edu/cmipt/gcs/GcsApplication.java b/src/main/java/edu/cmipt/gcs/GcsApplication.java index d2c8096..7788609 100644 --- a/src/main/java/edu/cmipt/gcs/GcsApplication.java +++ b/src/main/java/edu/cmipt/gcs/GcsApplication.java @@ -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) { diff --git a/src/main/java/edu/cmipt/gcs/dao/UserMapper.java b/src/main/java/edu/cmipt/gcs/dao/UserMapper.java new file mode 100644 index 0000000..d9e648f --- /dev/null +++ b/src/main/java/edu/cmipt/gcs/dao/UserMapper.java @@ -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 { +} diff --git a/src/main/java/edu/cmipt/gcs/pojo/UserPO.java b/src/main/java/edu/cmipt/gcs/pojo/UserPO.java new file mode 100644 index 0000000..f38aff8 --- /dev/null +++ b/src/main/java/edu/cmipt/gcs/pojo/UserPO.java @@ -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) { +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index f8dd622..2ea84be 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,4 +1,3 @@ -debug: true spring: datasource: druid: diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index f8dd622..2ea84be 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -1,4 +1,3 @@ -debug: true spring: datasource: druid: diff --git a/src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java b/src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java new file mode 100644 index 0000000..09bbe61 --- /dev/null +++ b/src/test/java/edu/cmipt/gcs/controller/dao/UserMapperTest.java @@ -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 userList = userMapper.selectList(null); + Assert.isTrue(1 == userList.size(), ""); + userList.forEach(System.out::println); + } +}