Skip to content

Commit f99ec8e

Browse files
committed
✨ 新增 ActiveRecord 模式操作
1 parent 3bc30d6 commit f99ec8e

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.xkcoding.orm.mybatis.plus.entity;
2+
3+
import com.baomidou.mybatisplus.annotation.TableName;
4+
import com.baomidou.mybatisplus.extension.activerecord.Model;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
import lombok.experimental.Accessors;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* <p>
13+
* 角色实体类
14+
* </p>
15+
*
16+
* @author yangkai.shen
17+
* @date Created in 2019/9/14 14:04
18+
*/
19+
@Data
20+
@TableName("orm_role")
21+
@Accessors(chain = true)
22+
@EqualsAndHashCode(callSuper = true)
23+
public class Role extends Model<Role> {
24+
/**
25+
* 主键
26+
*/
27+
private Long id;
28+
29+
/**
30+
* 角色名
31+
*/
32+
private String name;
33+
34+
/**
35+
* 主键值,ActiveRecord 模式这个必须有,否则 xxById 的方法都将失效!
36+
* 即使使用 ActiveRecord 不会用到 RoleMapper,RoleMapper 这个接口也必须创建
37+
*/
38+
@Override
39+
protected Serializable pkVal() {
40+
41+
return this.id;
42+
}
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.xkcoding.orm.mybatis.plus.mapper;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.xkcoding.orm.mybatis.plus.entity.Role;
5+
6+
/**
7+
* <p>
8+
* RoleMapper
9+
* </p>
10+
*
11+
* @author yangkai.shen
12+
* @date Created in 2019/9/14 14:06
13+
*/
14+
public interface RoleMapper extends BaseMapper<Role> {
15+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001');
2-
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002');
2+
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002');
3+
4+
INSERT INTO `orm_role`(`id`,`name`) VALUES (1,'管理员'),(2,'普通用户');

spring-boot-demo-orm-mybatis-plus/src/main/resources/db/schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ CREATE TABLE `orm_user` (
1111
`last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间',
1212
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间'
1313
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表';
14+
15+
DROP TABLE IF EXISTS `orm_role`;
16+
CREATE TABLE `orm_role` (
17+
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键',
18+
`name` VARCHAR(32) NOT NULL UNIQUE COMMENT '角色名'
19+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.xkcoding.orm.mybatis.plus.activerecord;
2+
3+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4+
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
5+
import com.xkcoding.orm.mybatis.plus.SpringBootDemoOrmMybatisPlusApplicationTests;
6+
import com.xkcoding.orm.mybatis.plus.entity.Role;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.util.List;
12+
13+
/**
14+
* <p>
15+
* Role
16+
* </p>
17+
*
18+
* @author yangkai.shen
19+
* @date Created in 2019/9/14 14:19
20+
*/
21+
@Slf4j
22+
public class ActiveRecordTest extends SpringBootDemoOrmMybatisPlusApplicationTests {
23+
/**
24+
* 测试 ActiveRecord 插入数据
25+
*/
26+
@Test
27+
public void testActiveRecordInsert() {
28+
Role role = new Role();
29+
role.setName("VIP");
30+
Assert.assertTrue(role.insert());
31+
// 成功直接拿会写的 ID
32+
log.debug("【role】= {}", role);
33+
}
34+
35+
/**
36+
* 测试 ActiveRecord 更新数据
37+
*/
38+
@Test
39+
public void testActiveRecordUpdate() {
40+
Assert.assertTrue(new Role().setId(1L).setName("管理员-1").updateById());
41+
Assert.assertTrue(new Role().update(new UpdateWrapper<Role>().lambda().set(Role::getName, "普通用户-1").eq(Role::getId, 2)));
42+
}
43+
44+
/**
45+
* 测试 ActiveRecord 查询数据
46+
*/
47+
@Test
48+
public void testActiveRecordSelect() {
49+
Assert.assertEquals("管理员", new Role().setId(1L).selectById().getName());
50+
Role role = new Role().selectOne(new QueryWrapper<Role>().lambda().eq(Role::getId, 2));
51+
Assert.assertEquals("普通用户", role.getName());
52+
List<Role> roles = new Role().selectAll();
53+
Assert.assertTrue(roles.size() > 0);
54+
log.debug("【roles】= {}", roles);
55+
}
56+
57+
/**
58+
* 测试 ActiveRecord 删除数据
59+
*/
60+
@Test
61+
public void testActiveRecordDelete() {
62+
Assert.assertTrue(new Role().setId(1L).deleteById());
63+
Assert.assertTrue(new Role().delete(new QueryWrapper<Role>().lambda().eq(Role::getName, "普通用户")));
64+
}
65+
}

0 commit comments

Comments
 (0)