From f99ec8e2bc825b0bb5db4738bca183f8b0c39647 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 14 Sep 2019 14:52:01 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=20ActiveRecord?= =?UTF-8?q?=20=E6=A8=A1=E5=BC=8F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orm/mybatis/plus/entity/Role.java | 43 ++++++++++++ .../orm/mybatis/plus/mapper/RoleMapper.java | 15 +++++ .../src/main/resources/db/data.sql | 4 +- .../src/main/resources/db/schema.sql | 6 ++ .../plus/activerecord/ActiveRecordTest.java | 65 +++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java create mode 100644 spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java create mode 100644 spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java diff --git a/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java b/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java new file mode 100644 index 0000000..074fc0e --- /dev/null +++ b/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java @@ -0,0 +1,43 @@ +package com.xkcoding.orm.mybatis.plus.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + *

+ * 角色实体类 + *

+ * + * @author yangkai.shen + * @date Created in 2019/9/14 14:04 + */ +@Data +@TableName("orm_role") +@Accessors(chain = true) +@EqualsAndHashCode(callSuper = true) +public class Role extends Model { + /** + * 主键 + */ + private Long id; + + /** + * 角色名 + */ + private String name; + + /** + * 主键值,ActiveRecord 模式这个必须有,否则 xxById 的方法都将失效! + * 即使使用 ActiveRecord 不会用到 RoleMapper,RoleMapper 这个接口也必须创建 + */ + @Override + protected Serializable pkVal() { + + return this.id; + } +} diff --git a/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java b/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java new file mode 100644 index 0000000..0240849 --- /dev/null +++ b/spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java @@ -0,0 +1,15 @@ +package com.xkcoding.orm.mybatis.plus.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.xkcoding.orm.mybatis.plus.entity.Role; + +/** + *

+ * RoleMapper + *

+ * + * @author yangkai.shen + * @date Created in 2019/9/14 14:06 + */ +public interface RoleMapper extends BaseMapper { +} diff --git a/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql b/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql index 3ab2b59..05fdd3b 100644 --- a/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql +++ b/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql @@ -1,2 +1,4 @@ INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); -INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); \ No newline at end of file +INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); + +INSERT INTO `orm_role`(`id`,`name`) VALUES (1,'管理员'),(2,'普通用户'); diff --git a/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql b/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql index 22804e5..609c4b6 100644 --- a/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql +++ b/spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql @@ -11,3 +11,9 @@ CREATE TABLE `orm_user` ( `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; + +DROP TABLE IF EXISTS `orm_role`; +CREATE TABLE `orm_role` ( + `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '角色名' +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; diff --git a/spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java b/spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java new file mode 100644 index 0000000..426b0ef --- /dev/null +++ b/spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java @@ -0,0 +1,65 @@ +package com.xkcoding.orm.mybatis.plus.activerecord; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import com.xkcoding.orm.mybatis.plus.SpringBootDemoOrmMybatisPlusApplicationTests; +import com.xkcoding.orm.mybatis.plus.entity.Role; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + *

+ * Role + *

+ * + * @author yangkai.shen + * @date Created in 2019/9/14 14:19 + */ +@Slf4j +public class ActiveRecordTest extends SpringBootDemoOrmMybatisPlusApplicationTests { + /** + * 测试 ActiveRecord 插入数据 + */ + @Test + public void testActiveRecordInsert() { + Role role = new Role(); + role.setName("VIP"); + Assert.assertTrue(role.insert()); + // 成功直接拿会写的 ID + log.debug("【role】= {}", role); + } + + /** + * 测试 ActiveRecord 更新数据 + */ + @Test + public void testActiveRecordUpdate() { + Assert.assertTrue(new Role().setId(1L).setName("管理员-1").updateById()); + Assert.assertTrue(new Role().update(new UpdateWrapper().lambda().set(Role::getName, "普通用户-1").eq(Role::getId, 2))); + } + + /** + * 测试 ActiveRecord 查询数据 + */ + @Test + public void testActiveRecordSelect() { + Assert.assertEquals("管理员", new Role().setId(1L).selectById().getName()); + Role role = new Role().selectOne(new QueryWrapper().lambda().eq(Role::getId, 2)); + Assert.assertEquals("普通用户", role.getName()); + List roles = new Role().selectAll(); + Assert.assertTrue(roles.size() > 0); + log.debug("【roles】= {}", roles); + } + + /** + * 测试 ActiveRecord 删除数据 + */ + @Test + public void testActiveRecordDelete() { + Assert.assertTrue(new Role().setId(1L).deleteById()); + Assert.assertTrue(new Role().delete(new QueryWrapper().lambda().eq(Role::getName, "普通用户"))); + } +}