mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
commit
6853b3527c
@ -16,26 +16,38 @@
|
||||
在自定义方言中,重写 `forSelectByQuery` 方法,这个方法是用于构建返回根据 `QueryWrapper` 查询的方法, 以下是示例代码:
|
||||
|
||||
```java
|
||||
public class MyPermissionDialect extends CommonsDialectImpl{
|
||||
public class MyPermissionDialect extends CommonsDialectImpl {
|
||||
|
||||
@Override
|
||||
public String forSelectByQuery(QueryWrapper queryWrapper) {
|
||||
|
||||
|
||||
//获取当前用户信息,为 queryWrapper 添加额外的条件
|
||||
queryWrapper.and("...");
|
||||
|
||||
|
||||
return supper.buildSelectSql(queryWrapper);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
特别的,如果您使用的是 `Oracle` 数据库,需要继承 `OracleDialect` 类,示例:
|
||||
|
||||
```java
|
||||
public class MyOraclePermissionDialect extends OracleDialect {
|
||||
|
||||
// 这个构造器只有在 Oracle 12C 的版本下才需要添加。
|
||||
// public MyOraclePermissionDialect() {
|
||||
// super(LimitOffsetProcessor.DERBY);
|
||||
// }
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
在项目启动时,通过 `DialectFactory` 注册 `MyPermissionDialect`:
|
||||
|
||||
```java
|
||||
DialectFactory.registerDialect(DbType.MYSQL, new MyPermissionDialect());
|
||||
DialectFactory.registerDialect(DbType.MYSQL,new MyPermissionDialect());
|
||||
```
|
||||
|
||||
|
||||
**常见问题1:通过重写 `IDialect` 后,所有的查询都添加了条件,但是有些表不需要条件如何做?**
|
||||
|
||||
>答:可以通过 CPI 获取 QueryWrapper 查询了哪些表,然后进行动态处理。例如 `List<QueryTable> tables = CPI.getQueryTables(queryWrapper)`,然后进一步对
|
||||
|
||||
@ -77,7 +77,7 @@ public class Account extends BaseEntity<String, Long, String> {
|
||||
", userName='" + userName + '\'' +
|
||||
", age=" + age +
|
||||
", birthday=" + birthday +
|
||||
", list=" + roles +
|
||||
", roles=" + roles +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class AccountVO extends IdEntity<Long> {
|
||||
|
||||
private Integer age;
|
||||
private Date birthday;
|
||||
|
||||
private Gender gender;
|
||||
private RoleVO2<String, String> role;
|
||||
|
||||
public RoleVO2<String, String> getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(RoleVO2<String, String> role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" +
|
||||
"id=" + id +
|
||||
", age=" + age +
|
||||
", birthday=" + birthday +
|
||||
", roleName=" + role +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
* @since 2023-06-15
|
||||
*/
|
||||
public class RoleKey<T> {
|
||||
|
||||
private T roleKey;
|
||||
|
||||
public T getRoleKey() {
|
||||
return roleKey;
|
||||
}
|
||||
|
||||
public void setRoleKey(T roleKey) {
|
||||
this.roleKey = roleKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return roleKey.toString();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mybatisflex.test.model;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
* @since 2023-06-15
|
||||
*/
|
||||
public class RoleVO2<T, K> {
|
||||
|
||||
private T roleName;
|
||||
private RoleKey<K> roleKey;
|
||||
|
||||
public T getRoleName() {
|
||||
return roleName;
|
||||
}
|
||||
|
||||
public void setRoleName(T roleName) {
|
||||
this.roleName = roleName;
|
||||
}
|
||||
|
||||
public RoleKey<K> getRoleKey() {
|
||||
return roleKey;
|
||||
}
|
||||
|
||||
public void setRoleKey(RoleKey<K> roleKey) {
|
||||
this.roleKey = roleKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RoleName{" +
|
||||
"roleName=" + roleName +
|
||||
", roleKey=" + roleKey +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,7 @@ package com.mybatisflex.test.mapper;
|
||||
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.test.model.Account;
|
||||
import com.mybatisflex.test.model.AccountVO;
|
||||
import com.mybatisflex.test.model.Gender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -25,6 +26,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
|
||||
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
|
||||
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
|
||||
|
||||
/**
|
||||
* @author 王帅
|
||||
* @since 2023-06-13
|
||||
@ -63,6 +68,27 @@ class AccountMapperTest {
|
||||
accountMapper.selectListByQuery(QueryWrapper.create()).forEach(System.err::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testListString() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.select(ACCOUNT.ALL_COLUMNS, ROLE.ROLE_NAME.as("roles"))
|
||||
.from(ACCOUNT)
|
||||
.leftJoin(USER_ROLE).on(USER_ROLE.USER_ID.eq(ACCOUNT.ID))
|
||||
.leftJoin(ROLE).on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID));
|
||||
accountMapper.selectListByQuery(queryWrapper).forEach(System.err::println);
|
||||
accountMapper.selectListByQueryAs(queryWrapper, AccountVO.class).forEach(System.err::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenericEntity() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.select(ACCOUNT.ALL_COLUMNS, ROLE.ALL_COLUMNS)
|
||||
.from(ACCOUNT)
|
||||
.leftJoin(USER_ROLE).on(USER_ROLE.USER_ID.eq(ACCOUNT.ID))
|
||||
.leftJoin(ROLE).on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID));
|
||||
accountMapper.selectListByQueryAs(queryWrapper, AccountVO.class).forEach(System.err::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEnum() {
|
||||
Account account = new Account();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user