forked from xkcoding/spring-boot-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...g-boot-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/entity/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 15 additions & 0 deletions
15
...-demo-orm-mybatis-plus/src/main/java/com/xkcoding/orm/mybatis/plus/mapper/RoleMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> { | ||
} |
4 changes: 3 additions & 1 deletion
4
spring-boot-demo-orm-mybatis-plus/src/main/resources/db/data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', '[email protected]', '17300000001'); | ||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', '[email protected]', '17300000002'); | ||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', '[email protected]', '17300000002'); | ||
|
||
INSERT INTO `orm_role`(`id`,`name`) VALUES (1,'管理员'),(2,'普通用户'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...batis-plus/src/test/java/com/xkcoding/orm/mybatis/plus/activerecord/ActiveRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, "普通用户"))); | ||
} | ||
} |