18 changed files with 313 additions and 20 deletions
@ -0,0 +1,56 @@ |
|||
package com.project.base.config; |
|||
|
|||
import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
|||
import com.baomidou.mybatisplus.core.metadata.TableInfo; |
|||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
|||
import org.apache.ibatis.executor.keygen.NoKeyGenerator; |
|||
import org.apache.ibatis.mapping.MappedStatement; |
|||
import org.apache.ibatis.mapping.SqlSource; |
|||
|
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* 自定义选装件:MySQL 批量 Upsert |
|||
*/ |
|||
public class InsertBatchOnDuplicateKeyUpdate extends AbstractMethod { |
|||
|
|||
protected InsertBatchOnDuplicateKeyUpdate() { |
|||
super("batchUpsert"); // 方法名
|
|||
} |
|||
|
|||
@Override |
|||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { |
|||
final String methodName = "batchUpsert"; |
|||
|
|||
// 1. 准备 INSERT 字段
|
|||
String columnScript = "(" + tableInfo.getKeyColumn() + "," + |
|||
tableInfo.getFieldList().stream() |
|||
.map(TableFieldInfo::getColumn) |
|||
.collect(Collectors.joining(",")) + ")"; |
|||
|
|||
// 2. 准备 VALUES 部分 (修复重点!)
|
|||
String valuesScript = "(#{item." + tableInfo.getKeyProperty() + "}," + |
|||
tableInfo.getFieldList().stream() |
|||
.map(i -> { |
|||
// 【核心修复】检查该字段是否有 TypeHandler (如 JacksonTypeHandler)
|
|||
if (i.getTypeHandler() != null) { |
|||
// 生成格式如:#{item.depIdPath,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler}
|
|||
return "#{item." + i.getProperty() + ",typeHandler=" + i.getTypeHandler().getName() + "}"; |
|||
} |
|||
return "#{item." + i.getProperty() + "}"; |
|||
}) |
|||
.collect(Collectors.joining(",")) + ")"; |
|||
|
|||
// 3. 准备 ON DUPLICATE KEY UPDATE 部分
|
|||
String updateScript = tableInfo.getFieldList().stream() |
|||
.filter(i -> !i.getColumn().equals("create_time")) |
|||
.map(i -> i.getColumn() + " = VALUES(" + i.getColumn() + ")") |
|||
.collect(Collectors.joining(",")) + ", update_time = NOW()"; |
|||
|
|||
String sql = String.format("<script>INSERT INTO %s %s VALUES <foreach collection=\"list\" item=\"item\" separator=\",\">%s</foreach> ON DUPLICATE KEY UPDATE %s</script>", |
|||
tableInfo.getTableName(), columnScript, valuesScript, updateScript); |
|||
|
|||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); |
|||
return this.addInsertMappedStatement(mapperClass, modelClass, methodName, sqlSource, new NoKeyGenerator(), null, null); |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
package com.project.base.config; |
|||
|
|||
import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
|||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; |
|||
import com.baomidou.mybatisplus.core.metadata.TableInfo; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Configuration |
|||
public class MybatisPlusConfig extends DefaultSqlInjector { |
|||
|
|||
@Override |
|||
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) { |
|||
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo); |
|||
methodList.add(new InsertBatchOnDuplicateKeyUpdate()); // 确保这里添加了你的自定义方法
|
|||
return methodList; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
package com.project.base.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import org.apache.ibatis.annotations.Param; |
|||
import java.util.List; |
|||
|
|||
public interface BatchUpsertMapper<T> extends BaseMapper<T> { |
|||
/** |
|||
* 自定义全局批量 Upsert |
|||
*/ |
|||
int batchUpsert(@Param("list") List<T> list); |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
package com.project.ding.controller; |
|||
|
|||
|
|||
import com.project.ding.utils.DingUserSyncUtil; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
@RestController |
|||
@RequestMapping("/ding") |
|||
public class SyncController { |
|||
@Autowired |
|||
private DingUserSyncUtil dingUserSyncUtil; |
|||
|
|||
/** |
|||
* 手动触发同步接口 |
|||
* 强刷:/sync/all?force=true |
|||
*/ |
|||
@GetMapping("/sync") |
|||
public String triggerSync(@RequestParam(defaultValue = "false") boolean force) { |
|||
return dingUserSyncUtil.triggerSync(force); |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
package com.project.ding.domain.service; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.project.ding.domain.entity.DepartmentEntity; |
|||
import com.project.ding.domain.entity.UserEntity; |
|||
|
|||
public interface DepartmentBaseService extends IService<DepartmentEntity> { |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
package com.project.ding.domain.service.impl; |
|||
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|||
import com.project.ding.domain.entity.DepartmentEntity; |
|||
import com.project.ding.domain.service.DepartmentBaseService; |
|||
import com.project.ding.mapper.DepartmentMapper; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
@Service |
|||
public class DepartmentBaseServiceImpl extends ServiceImpl<DepartmentMapper, DepartmentEntity> implements DepartmentBaseService { |
|||
} |
|||
@ -1,9 +1,11 @@ |
|||
package com.project.ding.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.project.base.mapper.BatchUpsertMapper; |
|||
import com.project.ding.domain.entity.DepartmentEntity; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
@Mapper |
|||
public interface DepartmentMapper extends BaseMapper<DepartmentEntity> { |
|||
public interface DepartmentMapper extends BaseMapper<DepartmentEntity>, BatchUpsertMapper<DepartmentEntity> { |
|||
|
|||
} |
|||
|
|||
@ -1,7 +1,8 @@ |
|||
package com.project.ding.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.project.base.mapper.BatchUpsertMapper; |
|||
import com.project.ding.domain.entity.UserEntity; |
|||
|
|||
public interface UserMapper extends BaseMapper<UserEntity> { |
|||
public interface UserMapper extends BaseMapper<UserEntity> , BatchUpsertMapper<UserEntity> { |
|||
} |
|||
|
|||
|
After Width: | Height: | Size: 15 KiB |
Loading…
Reference in new issue