Browse Source

新增 ActiveRecord 模式操作

pull/1/head
Yangkai.Shen 5 years ago
parent
commit
f99ec8e2bc
5 changed files with 132 additions and 1 deletions
  1. +43
    -0
      spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java
  2. +15
    -0
      spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java
  3. +3
    -1
      spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql
  4. +6
    -0
      spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql
  5. +65
    -0
      spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java

+ 43
- 0
spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java View File

@@ -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;

/**
* <p>
* 角色实体类
* </p>
*
* @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<Role> {
/**
* 主键
*/
private Long id;

/**
* 角色名
*/
private String name;

/**
* 主键值,ActiveRecord 模式这个必须有,否则 xxById 的方法都将失效!
* 即使使用 ActiveRecord 不会用到 RoleMapper,RoleMapper 这个接口也必须创建
*/
@Override
protected Serializable pkVal() {

return this.id;
}
}

+ 15
- 0
spring-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java View File

@@ -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;

/**
* <p>
* RoleMapper
* </p>
*
* @author yangkai.shen
* @date Created in 2019/9/14 14:06
*/
public interface RoleMapper extends BaseMapper<Role> {
}

+ 3
- 1
spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql View File

@@ -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 (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');
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,'普通用户');

+ 6
- 0
spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql View File

@@ -11,3 +11,9 @@ CREATE TABLE `orm_user` (
`last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间',
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; ) 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 系列示例表';

+ 65
- 0
spring-boot-demo-orm-mybatis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java View File

@@ -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;

/**
* <p>
* Role
* </p>
*
* @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<Role>().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<Role>().lambda().eq(Role::getId, 2));
Assert.assertEquals("普通用户", role.getName());
List<Role> 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<Role>().lambda().eq(Role::getName, "普通用户")));
}
}

Loading…
Cancel
Save