项目上有很多对多操作的场景,建对象,建mapper接口,设置查询条件,查询多对多数据等都是一项极为耗时耗力的工作。因此,搓了这款集建表-保存-查询-设置条件等操作于一体的组件。原理简单,利用$标签动态作些操作。如下xml配置。
<mapper namespace="com.xxx.future.support.dependence.mapper.ManyRelationMapper">
<!--查找记录-->
<select id="findRelation" resultType="com.xxx.future.support.dependence.manyrelation.Relation"
parameterType="com.xxx.future.support.dependence.manyrelation.RelationFind">
select ${targetKey} as targetId,${sourceKey} as sourceId from ${tableName} where 1=1
<if test="sourceIds!=null and sourceIds.size>0">
and ${sourceKey} in
<foreach collection="sourceIds" separator="," item="sourceId" open="(" close=")">
#{sourceId}
</foreach>
</if>
<if test="targetIds!=null and targetIds.size>0">
and ${targetKey} in
<foreach collection="targetIds" separator="," item="targetId" open="(" close=")">
#{targetId}
</foreach>
</if>
</select>
<!--删除记录-->
<delete id="clearRelation" parameterType="com.xxx.future.support.dependence.manyrelation.RelationDelete">
delete FROM ${tableName}
<where>
<if test="sourceIds!=null and sourceIds.size>0">
AND ${sourceKey} IN
<foreach collection="sourceIds" separator="," open="(" close=")" item="sId">
#{sId}
</foreach>
</if>
<if test="targetIds!=null and targetIds.size>0">
AND ${targetKey} IN
<foreach collection="targetIds" separator="," open="(" close=")" item="tId">
#{tId}
</foreach>
</if>
<if test="targetApply!=null">
AND ${targetApply}
</if>
<if test="sourceApply!=null">
AND ${sourceApply}
</if>
</where>
</delete>
<!--mysql批量插入-->
<insert id="saveRelation" databaseId="mysql"
parameterType="com.xxx.future.support.dependence.manyrelation.RelationSave">
INSERT INTO ${tableName}
(
${sourceKey},
${targetKey}
)
VALUES
<foreach collection="relations" item="item" separator=",">
(
#{item.sourceId},
#{item.targetId}
)
</foreach>
</insert>
<!--oracle批量插入-->
<insert id="saveRelation" databaseId="oracle"
parameterType="com.xxx.future.support.dependence.manyrelation.RelationSave">
INSERT ALL
<foreach collection="relations" item="item" separator=" ">
INTO ${tableName}
(
${sourceKey},
${targetKey}
)
VALUES
(
#{item.sourceId},
#{item.targetId}
)
</foreach>
SELECT 1 FROM dual
</insert>
<!--检查表是否存在-->
<select id="testExistsTable" resultType="int">
select count(*) from ${tableName}
</select>
<!--mysql库建表-->
<update id="createRelationTable" databaseId="mysql"
parameterType="com.xxx.future.support.dependence.manyrelation.RelationTable">
CREATE TABLE ${tableName} (
${sourceKey} varchar(60),
${targetKey} varchar(60),
CONSTRAINT pk_${tableName} PRIMARY KEY(${sourceKey},${targetKey})
)
</update>
<!--oracle库建表-->
<update id="createRelationTable" databaseId="oracle"
parameterType="com.xxx.future.support.dependence.domain.entity.RelationTable">
CREATE TABLE ${tableName} (
${sourceKey} varchar2(60),
${targetKey} varchar2(60),
CONSTRAINT pk_${tableName} PRIMARY KEY(${sourceKey},${targetKey})
)
</update>
</mapper>
工具类:工具ZYManyHelper怎么封装的就不细看了。太多了。都是一个拼参数,都是调用xml sql里面的查询,删除,保存,建表操作。看了上面xml配置就知道了。
public interface RelationBee {
// tableName.columnName
String sourceTable();
// tableName.columnName
String targetTable();
default String sourceTableName() {
String sourceTable = sourceTable();
String[] split = sourceTable.split("\\.");
return split[0];
}
default String sourceColumnName() {
String sourceTable = sourceTable();
String[] split = sourceTable.split("\\.");
return split[1];
}
default String targetColumnName() {
String targetTable = targetTable();
String[] split = targetTable.split("\\.");
return split[1];
}
default String targetTableName() {
String targetTable = targetTable();
String[] split = targetTable.split("\\.");
return split[0];
}
default void joinOne(String sourceId, String targetId) {
ZYManyHelper.joinOne(this, sourceId, targetId);
}
default void join(String sourceId, List<String> targetIds) {
ZYManyHelper.joinBySourceId(this, sourceId, targetIds);
}
default void joinByTargetId(String targetId, List<String> sourceIds) {
ZYManyHelper.joinByTargetId(this, targetId, sourceIds);
}
default void onlyJoin(List<String> sourceIds, List<String> targetIds) {
ZYManyHelper.join(this, sourceIds, targetIds);
}
default void onlyJoin(String sourceId, List<String> targetIds) {
if (ZYStrUtils.isNotNull(sourceId)) {
ZYManyHelper.join(this, Collections.singletonList(sourceId), targetIds);
}
}
default void onlyJoin(List<String> sourceIds, String targetId) {
if (ZYStrUtils.isNotNull(targetId)) {
ZYManyHelper.join(this, sourceIds, Collections.singletonList(targetId));
}
}
default void clearByTargetIdAndSourceApply(String targetId, String sourceApply) {
ZYManyHelper.clearByTargetIdsAndSourceApply(this, Collections.singletonList(targetId), sourceApply);
}
default void clearBySourceIdAndTargetApply(String sourceIds, String targetApply) {
ZYManyHelper.clearBySourceIdsAndTargetApply(this, Collections.singletonList(sourceIds), targetApply);
}
default void clearByTargetIdsAndSourceApply(List<String> targetIds, String sourceApply) {
ZYManyHelper.clearByTargetIdsAndSourceApply(this, targetIds, sourceApply);
}
default void clearBySourceIdsAndTargetApply(List<String> sourceIds, String targetApply) {
ZYManyHelper.clearBySourceIdsAndTargetApply(this, sourceIds, targetApply);
}
default void clearBySourceId(String sourceId) {
ZYManyHelper.clearBySourceId(this, sourceId);
}
default void clearBySourceIds(List<String> sourceIds) {
ZYManyHelper.clearBySourceIds(this, sourceIds);
}
default void clearByTargetId(String targetId) {
ZYManyHelper.clearByTargetId(this, targetId);
}
default void clearByTargetIds(List<String> targetIds) {
ZYManyHelper.clearByTargetIds(this, targetIds);
}
default void clearBySourceIdAndTargetIds(String sourceId, List<String> targetIds) {
ZYManyHelper.clearBySourceIdAndTargetIds(this, sourceId, targetIds);
}
default void clearByTargetIdAndSourceIds(String targetId, List<String> sourceIds) {
ZYManyHelper.clearByTargetIdAndSourceIds(this, targetId, sourceIds);
}
default Map<String, List<String>> findSourceTargetsMap(List<String> sourceIds) {
return ZYManyHelper.findSourceTargetsMap(this, sourceIds);
}
default Map<String, List<String>> findTargetSourcesMap(List<String> targetIds) {
return ZYManyHelper.findTargetSourcesMap(this, targetIds);
}
default Map<String, List<String>> findSourceTargetsMap() {
return ZYManyHelper.findSourceTargetsMap(this);
}
default Map<String, List<String>> findTargetSourcesMap() {
return ZYManyHelper.findTargetSourcesMap(this);
}
default List<String> findTargetIds(String sourceId) {
return ZYManyHelper.findTargetIds(this, sourceId);
}
default List<String> findTargetIds(List<String> sourceIds) {
return ZYManyHelper.findTargetIds(this, sourceIds);
}
default List<String> findSourceIds(String targetId) {
return ZYManyHelper.findSourceIds(this, targetId);
}
default List<String> findSourceIds(List<String> targetIds) {
return ZYManyHelper.findSourceIds(this, targetIds);
}
// select role where id in (select role_id from relation_role_user where user_id='1')
default void setWrapper(LambdaQueryWrapper<?> wrapper, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, targetId);
}
// select role where id in (select role_id from relation_role_user where user_id='1')
default void setWrapper(LambdaQueryWrapper<?> wrapper, String sourceColumn, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, sourceColumn, targetId, false);
}
// select role where id in (select role_id from relation_role_user where user_id='1')
default void setWrapper(QueryWrapper<?> wrapper, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, targetId);
}
// select role where id in (select role_id from relation_role_user where user_id in ('1'))
default void setInLambdaWrapper(LambdaQueryWrapper<?> wrapper, List<String> targetIds) {
ZYManyHelper.applyInRelation(this, wrapper, targetIds);
}
// select role where id in (select role_id from relation_role_user where user_id in ('1'))
default void setInLambdaWrapper(LambdaQueryWrapper<?> wrapper, String sourceColumn, List<String> targetIds) {
ZYManyHelper.applyInRelation(this, wrapper, sourceColumn, targetIds, false);
}
// select role where id in (select role_id from relation_role_user where user_id in ('1'))
default void setInWrapper(QueryWrapper<?> wrapper, List<String> targetIds) {
ZYManyHelper.applyInRelation(this, wrapper, targetIds);
}
// select role where id in (select role_id from relation_role_user where user_id in ('1'))
default void setInWrapper(QueryWrapper<?> wrapper, String sourceColumn, List<String> targetIds) {
ZYManyHelper.applyInRelation(this, wrapper, sourceColumn, targetIds, false);
}
// select role where id in (select role_id from relation_role_user where user_id='1')
default void setWrapper(QueryWrapper<?> wrapper, String sourceColumn, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, sourceColumn, targetId, false);
}
// select role where id not in (select role_id from relation_role_user where user_id='1')
default void setExcludeWrapper(LambdaQueryWrapper<?> wrapper, String targetId) {
ZYManyHelper.applyExcludeRelation(this, wrapper, targetId);
}
// select role where id not in (select role_id from relation_role_user where user_id='1')
default void setExcludeWrapper(LambdaQueryWrapper<?> wrapper, String sourceColumn, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, sourceColumn, targetId, true);
}
// select role where id not in (select role_id from relation_role_user where user_id='1')
default void setExcludeWrapper(QueryWrapper<?> wrapper, String targetId) {
ZYManyHelper.applyExcludeRelation(this, wrapper, targetId);
}
// select role where id not in (select role_id from relation_role_user where user_id='1')
default void setExcludeWrapper(QueryWrapper<?> wrapper, String sourceColumn, String targetId) {
ZYManyHelper.applyRelation(this, wrapper, sourceColumn, targetId, true);
}
// select user where id not in (select user_id from relation_role_user where role_id='1')
default void setExcludeTargetWrapper(QueryWrapper<?> wrapper, String sourceId) {
ZYManyHelper.applyExcludeTargetRelation(this, wrapper, sourceId);
}
// select user where id not in (select user_id from relation_role_user where role_id='1')
default void setExcludeTargetWrapper(LambdaQueryWrapper<?> wrapper, String sourceId) {
ZYManyHelper.applyExcludeTargetRelation(this, wrapper, sourceId);
}
// select user where id in (select user_id from relation_role_user where role_id='1')
default void setTargetWrapper(QueryWrapper<?> wrapper, String sourceId) {
ZYManyHelper.applyTargetRelation(this, wrapper, sourceId);
}
// select user where id in (select user_id from relation_role_user where role_id='1')
default void setTargetWrapper(LambdaQueryWrapper<?> wrapper, String sourceId) {
ZYManyHelper.applyTargetRelation(this, wrapper, sourceId);
}
}
调用枚举定义:
public enum SecurityRelation implements RelationBee {
// 角色快捷菜单关系
RELATION_ROLE_QUICK_MENU("role.role_id", "quick_menu.quick_menu_id"),
// 角色用户关系
RELATION_ROLE_USER("role.role_id", "user.user_id"),
// 角色APP菜单关系
RELATION_ROLE_APP_MENU("role.role_id", "app_menu.app_menu_id"),
// 角色菜单关系
RELATION_ROLE_MENU("role.role_id", "menu.menu_id");
private String sourceTable;
private String targetTable;
SecurityRelation(String sourceTable, String targetTable) {
this.sourceTable = sourceTable;
this.targetTable = targetTable;
}
@Override
public String sourceTable() {
return sourceTable;
}
@Override
public String targetTable() {
return targetTable;
}
}
在数据库中的数据效果如下图:
代码中的调用还是蛮舒服的,基本一条代码搞定,项目中,使用该方法节省了大量的体力劳动和时间,代码也较清爽干净,取得了不错的效果。如下图:
// 查找登用户有哪些角色id
List<String> roleIds = SecurityRelation.RELATION_ROLE_USER.findSourceIds(loginUserId);
// 查找登录用户s有哪些角色ids
List<String> roleIds = SecurityRelation.RELATION_ROLE_USER.findSourceIds(userIds);
// 查找登录用户s有哪些角色对应关系
Map<String, List<String>> userRoleIds = SecurityRelation.RELATION_ROLE_USER.findTargetSourcesMap(userIds);
// 查找角色有哪些用户
List<String> userIds = SecurityRelation.RELATION_ROLE_USER.findTargetIds(role.getId());
// 清理角色用户
SecurityRelation.RELATION_ROLE_USER.clearByTargetIdAndSourceIds(userId, roleIds);
//关连角色用户
SecurityRelation.RELATION_ROLE_USER.onlyJoin(roleIds, userId);
// 设置查询条件
SecurityRelation.RELATION_ROLE_USER.setTargetWrapper(wrapper, role.getId());
// 根据角色删除
SecurityRelation.RELATION_ROLE_USER.clearBySourceIds(roleIds);
// 根据用户删除
SecurityRelation.RELATION_ROLE_USER.clearByTargetId(userId);
// 全量关连
SecurityRelation.RELATION_ROLE_USER.join(roleId, userIds);