概述
任务描述
1.创建字段和方法数据字典父类;
2.创建字段和方法数据字典默认类;
第一步、创建字段和方法数据字典父类AbstractDictMap
package com.common.platform.base.dict;
import java.util.HashMap;
public abstract class AbstractDictMap {
protected HashMap<String, String> dictory = new HashMap<>();
protected HashMap<String, String> fieldWarpperDictory = new HashMap<>();
public AbstractDictMap() {
put("id", "主键id");
init();
initBeWrapped();
}
/**
* 初始化字段英文名称和中文名称对应的字典
*/
public abstract void init();
/**
* 初始化需要被包装的字段(例如:性别为1:男,2:女,需要被包装为汉字)
*/
protected abstract void initBeWrapped();
public String get(String key) {
return this.dictory.get(key);
}
public void put(String key, String value) {
this.dictory.put(key, value);
}
public String getFieldWarpperMethodName(String key) {
return this.fieldWarpperDictory.get(key);
}
public void putFieldWrapperMethodName(String key, String methodName) {
this.fieldWarpperDictory.put(key, methodName);
}
}
第二步、创建系统默认字段和方法数据字典类SystemDict
package com.common.platform.base.dict;
public class SystemDict extends AbstractDictMap {
@Override
public void init() {
}
@Override
protected void initBeWrapped() {
}
}
MyBatis组件
任务描述
(1)创建数据范围类;
(2)创建Mybatis拦截器类;
(3)创建Mybatis-Plus自动填充配置类;
(4)创建Mybatis配置类;
第一步、创建数据范围类DataScope
package com.common.platform.base.config.database;
import java.util.List;
/**
* 数据范围
*/
public class DataScope {
/**
* 限制范围的字段名称
*/
private String scopeName = "deptId";
/**
* 具体的数据范围
*/
private List<Long> deptIds;
public DataScope() {
}
public DataScope(List<Long> deptIds) {
this.deptIds = deptIds;
}
public DataScope(String scopeName, List<Long> deptIds) {
this.scopeName = scopeName;
this.deptIds = deptIds;
}
public List<Long> getDeptIds() {
return deptIds;
}
public void setDeptIds(List<Long> deptIds) {
this.deptIds = deptIds;
}
public String getScopeName() {
return scopeName;
}
public void setScopeName(String scopeName) {
this.scopeName = scopeName;
}
}
第二步、创建Mybatis拦截器DataScopeInterceptor
package com.common.platform.base.config.database;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class DataScopeInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement)metaStatementHandler.getValue("delegate.mappedStatement");
if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType()))
return invocation.proceed();
Connection connection = (Connection)invocation.getArgs()[0];
DbType dbType = JdbcUtils.getDbType(connection.getMetaData().getURL());
BoundSql boundSql = (BoundSql)metaStatementHandler.getValue("delegate.boundSql");
String originalSql = boundSql.getSql();
Object parameterObject = boundSql.getParameterObject();
DataScope dataScope = findDataScopeObject(parameterObject);
if (dataScope == null)
return invocation.proceed();
String scopeName = dataScope.getScopeName();
List<Long> deptIds = dataScope.getDeptIds();
String join = CollectionUtil.join(deptIds, ",");
if (dbType != null && dbType.equals(DbType.POSTGRE_SQL)) {
originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope."" + scopeName + "" in (" + join + ")";
} else {
originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
}
metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
return invocation.proceed();
}
public DataScope findDataScopeObject(Object parameterObj) {
if (parameterObj instanceof DataScope)
return (DataScope)parameterObj;
if (parameterObj instanceof Map)
for (Object val : ((Map)parameterObj).values()) {
if (val instanceof DataScope)
return (DataScope)val;
}
return null;
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {}
}
第三步、创建Mybatis-Plus自动填充类AutoFullHandler
package com.common.platform.base.config.database;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import java.util.Date;
/**
* 自定义sql字段填充器
*/
public class AutoFullHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//判断是否存在指定字段
if(metaObject.hasGetter(getCreateTimeFieldName())){
//获取当前值
Object createTime = getFieldValByName(getCreateTimeFieldName(), metaObject);
if (createTime == null) {
setFieldValByName(getCreateTimeFieldName(), new Date(), metaObject);
}
}
//判断是否存在指定字段
if(metaObject.hasGetter(getCreateUserFieldName())){
//获取当前值
Object createUser = getFieldValByName(getCreateUserFieldName(), metaObject);
if (createUser == null) {
//获取当前登录用户
Object accountId = getUserUniqueId();
setFieldValByName(getCreateUserFieldName(), accountId, metaObject);
}
}
}
@Override
public void updateFill(MetaObject metaObject) {
//判断是否存在指定字段
if(metaObject.hasGetter(getUpdateTimeFieldName())){
setFieldValByName(getUpdateTimeFieldName(), new Date(), metaObject);
}
//判断是否存在指定字段
if(metaObject.hasGetter(getUpdateUserFieldName())){
//获取当前值
Object updateUser = getFieldValByName(getUpdateUserFieldName(), metaObject);
if (updateUser == null) {
//获取当前登录用户
Object accountId = getUserUniqueId();
setFieldValByName(getUpdateUserFieldName(), accountId, metaObject);
}
}
}
/**
* 获取创建时间字段的名称(非数据库中字段名称)
*/
protected String getCreateTimeFieldName() {
return "createTime";
}
/**
* 获取创建用户字段的名称(非数据库中字段名称)
*/
protected String getCreateUserFieldName() {
return "createUser";
}
/**
* 获取更新时间字段的名称(非数据库中字段名称)
*/
protected String getUpdateTimeFieldName() {
return "updateTime";
}
/**
* 获取更新用户字段的名称(非数据库中字段名称)
*/
protected String getUpdateUserFieldName() {
return "updateUser";
}
/**
* 获取用户唯一id(注意默认获取的用户唯一id为空,如果想填写则需要继承本类)
*/
protected Object getUserUniqueId() {
return "";
}
}
第四步、创建Mybatis配置类MybatisPlusConfig
package com.common.platform.base.config.database;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* MyBatis 配置启动类
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.datasource", name = {"url"})
public class MybatisPlusConfig {
@Bean
/**
* 分页
*/
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
@ConditionalOnMissingBean
/**
* 数据范围
*/
public DataScopeInterceptor dataScopeInterceptor() {
return new DataScopeInterceptor();
}
@Bean
/**
* 乐观锁
*/
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
}
最后
以上就是端庄眼睛为你收集整理的公共字段和方法数据字典组件与MyBatis组件的全部内容,希望文章能够帮你解决公共字段和方法数据字典组件与MyBatis组件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复