@Bean public GlobalConfiguration globalConfiguration() { GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector()); // conf.setLogicDeleteValue("-1"); // conf.setLogicNotDeleteValue("1"); // conf.setIdType(2); // conf.setMetaObjectHandler(new H2MetaObjectHandler()); return conf; }
@Bean(name = "basisGlobalConfig") public GlobalConfiguration globalConfig( @Value("${mybatisPlus.globalConfig.idType}") Integer idType, //主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; @Value("${mybatisPlus.globalConfig.dbColumnUnderline}") Boolean dbColumnUnderline, //驼峰下划线转换 @Value("${mybatisPlus.globalConfig.isCapitalMode}") Boolean isCapitalMode //数据库大写下划线转换 ) { log.info("初始化GlobalConfiguration"); GlobalConfiguration globalConfig = new GlobalConfiguration(); // 主键类型 if ( !BlankUtil.isBlank(idType)) { globalConfig.setIdType(idType); } // 驼峰下划线转换 if ( !BlankUtil.isBlank(dbColumnUnderline)) { globalConfig.setDbColumnUnderline(dbColumnUnderline); } // 数据库大写下划线转换 if ( !BlankUtil.isBlank(isCapitalMode)) { globalConfig.setCapitalMode(isCapitalMode); } return globalConfig; }
public void deleteAllUser(Class<?> mapperClass, Class<?> modelClass, TableInfo table) { /* 执行 SQL ,动态 SQL 参考类 SqlMethod */ String sql = "delete from " + table.getTableName(); /* mapper 接口方法名一致 */ String method = "deleteAll"; SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); this.addDeleteMappedStatement(mapperClass, method, sqlSource); }
public GlobalConfiguration convertGlobalConfiguration() throws ClassNotFoundException, IllegalAccessException, InstantiationException { GlobalConfiguration globalConfiguration = new GlobalConfiguration(); if (StringUtils.isNotEmpty(this.getIdentifierQuote())) { globalConfiguration.setIdentifierQuote(this.getIdentifierQuote()); globalConfiguration.setLogicDeleteValue(this.getLogicDeleteValue()); globalConfiguration.setLogicNotDeleteValue(this.getLogicNotDeleteValue()); globalConfiguration.setSqlInjector((ISqlInjector) Class.forName(this.getSqlInjector()).newInstance()); globalConfiguration.setMetaObjectHandler((MetaObjectHandler) Class.forName(this.getMetaObjectHandler()).newInstance()); globalConfiguration.setKeyGenerator((IKeyGenerator) Class.forName(this.getKeyGenerator()).newInstance()); globalConfiguration.setIdType(this.getIdType()); globalConfiguration.setDbColumnUnderline(this.getDbColumnUnderline()); globalConfiguration.setFieldStrategy(this.getFieldStrategy()); globalConfiguration.setRefresh(this.getRefreshMapper()); globalConfiguration.setCapitalMode(this.getCapitalMode());
GlobalConfiguration config = new GlobalConfiguration(); config.setIdType(2); config.setLogicDeleteValue("1"); config.setLogicNotDeleteValue("0"); config.setDbType("mysql"); config.setDbColumnUnderline(true); config.setSqlInjector(new AutoSqlInjector()); config.setMetaObjectHandler(new BaseMetaObjectHandler()); sessionFactory.setGlobalConfig(config);
GlobalConfiguration globalConfig = new GlobalConfiguration(); globalConfig.setDbColumnUnderline(true); globalConfig.setDbType(DBType.MYSQL.name()); globalConfig.setIdType(3); mybatisPlus.setGlobalConfig(globalConfig); MybatisConfiguration mc = new MybatisConfiguration();
GlobalConfiguration globalConfig = new GlobalConfiguration(); globalConfig.setDbType(DBType.MYSQL.name());//数据库类型 globalConfig.setIdType(IdType.ID_WORKER_STR.getKey()); globalConfig.setSqlInjector(new AutoSqlInjector());
@Override protected String sqlSelectColumns(TableInfo table, boolean entityWrapper) { StringBuilder columns = new StringBuilder(); if (null != table.getResultMap()) { List<TableFieldInfo> fieldList = table.getFieldList(); int _size = 0; if (null != fieldList) { if (StringUtils.isNotEmpty(table.getKeyProperty())) { if (table.isKeyRelated()) { columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(table.getKeyProperty())); } else { columns.append(sqlWordConvert(table.getKeyProperty())); while (iterator.hasNext()) { TableFieldInfo fieldInfo = iterator.next(); if (StringUtils.isEmpty(fieldInfo.getEl()) || fieldInfo.getEl().equals(fieldInfo.getProperty())) { String wordConvert = sqlWordConvert(fieldInfo.getProperty()); if (fieldInfo.getColumn().equals(wordConvert)) { columns.append(wordConvert); } else { columns.append(fieldInfo.getColumn()); columns.append(" AS ").append(wordConvert); String wordConvert = sqlWordConvert(fieldInfo.getProperty()); if (fieldInfo.getColumn().equals(wordConvert)) { columns.append(wordConvert);
Map<String, String> cachedFieldsMap = new HashMap<>(); TableInfo tableInfo = TableInfoHelper.getTableInfo(modelClass); String keyProperty = tableInfo.getKeyProperty(); cachedFieldsMap.put(keyProperty, tableInfo.getKeyColumn()); for (TableFieldInfo tableFieldInfo : tableInfo.getFieldList()) { cachedFieldsMap.put(tableFieldInfo.getProperty(), tableFieldInfo.getColumn());
/** * <p> * 注入更新 SQL 语句 * </p> * * @param mapperClass * @param modelClass * @param table */ protected void injectUpdateByIdSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) { SqlMethod sqlMethod = selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID; String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table, "et."), table.getKeyColumn(), "et." + table.getKeyProperty(), "<if test=\"et instanceof java.util.Map\">" + "<if test=\"et.MP_OPTLOCK_VERSION_ORIGINAL!=null\">" + "and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}" + "</if>" + "</if>"); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource); }
@Transactional(rollbackFor = Exception.class) @Override public boolean insertOrUpdateAllColumn(T entity) { if (null != entity) { Class<?> cls = entity.getClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (StringUtils.checkValNull(idVal)) { return insertAllColumn(entity); } else { /* * 更新成功直接返回,失败执行插入逻辑 */ return updateAllColumnById(entity) || insertAllColumn(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; }
/** * <p> * 使用对象封装的setsqlselect * </p> * * @param column * 字段 * @return */ public Wrapper<T> setSqlSelect(Column... column) { if (ArrayUtils.isNotEmpty(column)) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < column.length; i++) { if (column[i] != null) { String col = column[i].getColumn(); String as = column[i].getAs(); if (StringUtils.isEmpty(col)) { continue; } builder.append(col).append(as); if (i < column.length - 1) { builder.append(","); } } } this.sqlSelect = builder.toString(); } return this; }
/** * 获取SqlStatement * * @param sqlMethod * @return */ protected String sqlStatement(SqlMethod sqlMethod) { return SqlHelper.table(currentModelClass()).getSqlStatement(sqlMethod.getMethod()); }
public GlobalConfiguration convertGlobalConfiguration() throws ClassNotFoundException, IllegalAccessException, InstantiationException { GlobalConfiguration globalConfiguration = new GlobalConfiguration(); if (StringUtils.isNotEmpty(this.getIdentifierQuote())) { globalConfiguration.setIdentifierQuote(this.getIdentifierQuote()); globalConfiguration.setLogicDeleteValue(this.getLogicDeleteValue()); globalConfiguration.setLogicNotDeleteValue(this.getLogicNotDeleteValue()); globalConfiguration.setSqlInjector((ISqlInjector) Class.forName(this.getSqlInjector()).newInstance()); globalConfiguration.setMetaObjectHandler((MetaObjectHandler) Class.forName(this.getMetaObjectHandler()).newInstance()); globalConfiguration.setKeyGenerator((IKeyGenerator) Class.forName(this.getKeyGenerator()).newInstance()); globalConfiguration.setIdType(this.getIdType()); globalConfiguration.setDbColumnUnderline(this.getDbColumnUnderline()); globalConfiguration.setFieldStrategy(this.getFieldStrategy()); globalConfiguration.setRefresh(this.getRefreshMapper()); globalConfiguration.setCapitalMode(this.getCapitalMode()); globalConfiguration.setSqlParserCache(this.getSqlParserCache());
@Override public boolean insertOrUpdate(T entity, Class<?> cls) { if (null != entity) { TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (null != tableInfo && com.baomidou.mybatisplus.toolkit.StringUtils .isNotEmpty(tableInfo.getKeyProperty())) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (com.baomidou.mybatisplus.toolkit.StringUtils.checkValNull(idVal)) { return insert(entity); } else { /* * 更新成功直接返回,失败执行插入逻辑 */ return updateById(entity) || insert(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; } }
/** * <p> * TableId 注解存在更新记录,否插入一条记录 * </p> * * @param entity 实体对象 * @return boolean */ @Transactional(rollbackFor = Exception.class) @Override public boolean insertOrUpdate(T entity) { if (null != entity) { Class<?> cls = entity.getClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (StringUtils.checkValNull(idVal)) { return insert(entity); } else { /* * 更新成功直接返回,失败执行插入逻辑 */ return updateById(entity) || insert(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; }
@Override public boolean insertOrUpdate(T entity) { if (null != entity) { Class<?> cls = entity.getClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); if (tableInfo == null) { tableInfo = TableInfoHelper.getTableInfo(cls.getSuperclass()); } if (null != tableInfo && com.baomidou.mybatisplus.toolkit.StringUtils .isNotEmpty(tableInfo.getKeyProperty())) { Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); if (com.baomidou.mybatisplus.toolkit.StringUtils.checkValNull(idVal)) { return insert(entity); } else { /* * 更新成功直接返回,失败执行插入逻辑 */ return updateById(entity) || insert(entity); } } else { throw new MybatisPlusException("Error: Can not execute. Could not find @TableId."); } } return false; }