mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
test: 测试 Active Record 联表查询。
This commit is contained in:
parent
8f04e8a75c
commit
2f8ef0662e
@ -16,6 +16,7 @@
|
||||
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
import com.mybatisflex.annotation.Column;
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
|
||||
@ -36,6 +37,7 @@ public class Role implements Comparable<Role> {
|
||||
private String roleKey;
|
||||
private String roleName;
|
||||
|
||||
@Column(ignore = true)
|
||||
private List<UserVO> userVOS;
|
||||
|
||||
public Integer getRoleId() {
|
||||
|
||||
@ -17,7 +17,9 @@
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
import com.mybatisflex.annotation.Id;
|
||||
import com.mybatisflex.annotation.RelationManyToMany;
|
||||
import com.mybatisflex.annotation.Table;
|
||||
import com.mybatisflex.core.activerecord.Model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -29,15 +31,26 @@ import java.util.List;
|
||||
*/
|
||||
|
||||
@Table("tb_user")
|
||||
public class User {
|
||||
public class User extends Model<User> {
|
||||
|
||||
@Id
|
||||
private Integer userId;
|
||||
private String userName;
|
||||
private String password;
|
||||
//@Column(ignore = true)
|
||||
|
||||
@RelationManyToMany(
|
||||
selfField = "userId",
|
||||
targetField = "roleId",
|
||||
joinTable = "tb_user_role",
|
||||
joinSelfColumn = "user_id",
|
||||
joinTargetColumn = "role_id"
|
||||
)
|
||||
private List<Role> roleList;
|
||||
|
||||
public static User create() {
|
||||
return new User();
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
@ -19,11 +19,15 @@ package com.mybatisflex.test.mapper;
|
||||
import com.mybatisflex.core.mybatis.Mappers;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.test.model.Good;
|
||||
import com.mybatisflex.test.model.User;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static com.mybatisflex.test.model.table.GoodTableDef.GOOD;
|
||||
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
||||
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
|
||||
import static com.mybatisflex.test.model.table.UserTableDef.USER;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
@ -105,4 +109,19 @@ class ActiveRecordTest {
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRelation() {
|
||||
User user1 = User.create().select(USER.ALL_COLUMNS, ROLE.ALL_COLUMNS)
|
||||
.leftJoin(USER_ROLE).as("ur").on(USER_ROLE.USER_ID.eq(USER.USER_ID))
|
||||
.leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID))
|
||||
.where(USER.USER_ID.eq(2))
|
||||
.one();
|
||||
|
||||
User user2 = User.create()
|
||||
.where(USER.USER_ID.eq(2))
|
||||
.oneWithRelations();
|
||||
|
||||
Assertions.assertEquals(user1.toString(), user2.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user