Skip to content

Commit bc650c5

Browse files
committed
jdbcDao ValueObject batchUpdate
1 parent 2693a3b commit bc650c5

File tree

15 files changed

+197
-79
lines changed

15 files changed

+197
-79
lines changed

core/src/main/java/com/robin/core/base/dao/IjdbcDao.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public interface IjdbcDao {
141141
*/
142142
List<Map<String, Object>> queryBySql(String sqlstr, Object... obj) throws DAOException;
143143
Map<String,Object> getBySql(String querySQL,Object ... objects) throws DAOException;
144-
144+
int countByNameParam(String nameSql,Map<String,Object> paramMap);
145+
<T extends BaseObject> int batchUpdate(List<T> list,Class<T> clazz);
145146
/**
146147
* Query With PageQuery
147148
*

core/src/main/java/com/robin/core/base/dao/JdbcDao.java

+52
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.Lists;
1919
import com.robin.core.base.dao.handler.MetaObjectHandler;
2020
import com.robin.core.base.dao.util.*;
21+
import com.robin.core.base.datameta.DataBaseColumnMeta;
2122
import com.robin.core.base.exception.DAOException;
2223
import com.robin.core.base.exception.MissingConfigException;
2324
import com.robin.core.base.exception.QueryConfgNotFoundException;
@@ -26,6 +27,7 @@
2627
import com.robin.core.base.reflect.ReflectUtils;
2728
import com.robin.core.base.spring.SpringContextHolder;
2829
import com.robin.core.base.util.Const;
30+
import com.robin.core.base.util.IUserUtils;
2931
import com.robin.core.base.util.LicenseUtils;
3032
import com.robin.core.convert.util.ConvertUtil;
3133
import com.robin.core.fileaccess.meta.DataCollectionMeta;
@@ -50,6 +52,9 @@
5052

5153
import javax.sql.DataSource;
5254
import java.io.Serializable;
55+
import java.sql.PreparedStatement;
56+
import java.sql.SQLException;
57+
import java.sql.Timestamp;
5358
import java.util.*;
5459

5560
@Slf4j
@@ -282,6 +287,16 @@ public <T extends BaseObject> int countByCondition(Class<T> type, FilterConditio
282287
throw new DAOException(ex);
283288
}
284289
}
290+
public int countByNameParam(String nameSql,Map<String,Object> paramMap){
291+
try {
292+
if (log.isDebugEnabled()) {
293+
log.debug("count sql", nameSql);
294+
}
295+
return getNamedJdbcTemplate().queryForObject(nameSql,paramMap,Integer.class);
296+
}catch (Exception ex){
297+
throw new DAOException(ex);
298+
}
299+
}
285300

286301

287302
@Override
@@ -564,6 +579,43 @@ public <T extends BaseObject, P extends Serializable> P createVO(T obj, Class<P>
564579
}
565580
return retObj;
566581
}
582+
public <T extends BaseObject> int batchUpdate(List<T> list,Class<T> clazz){
583+
try{
584+
Assert.isTrue(!CollectionUtils.isEmpty(list),"");
585+
List<FieldContent> fields = AnnotationRetriever.getMappingFieldsCache(clazz);
586+
AnnotationRetriever.EntityContent<? extends BaseObject> tableDef = AnnotationRetriever.getMappingTableByCache(clazz);
587+
Map<String, DataBaseColumnMeta> columnMetaMap = EntityMappingUtil.returnMetaMap(clazz, sqlGen, this, tableDef);
588+
String insertSql=EntityMappingUtil.getInsertSqlIgnoreValue(clazz, sqlGen, this, fields);
589+
MetaObjectHandler handler = SpringContextHolder.getBean(MetaObjectHandler.class);
590+
int[][] rs=getJdbcTemplate().batchUpdate(insertSql, list, 1000, (ps, t) -> {
591+
if (!ObjectUtils.isEmpty(handler)) {
592+
Map<String, FieldContent> fieldContentMap = AnnotationRetriever.getMappingFieldsMapCache(clazz);
593+
handler.insertFill(new MetaObject(t, fieldContentMap));
594+
}
595+
int pos=1;
596+
try {
597+
for (FieldContent content : fields) {
598+
if (!content.isIncrement() && !content.isSequential()) {
599+
Object obj=content.getGetMethod().invoke(t);
600+
if(!ObjectUtils.isEmpty(obj)) {
601+
ps.setObject(pos, obj);
602+
}else{
603+
DataBaseColumnMeta columnMeta=Optional.ofNullable(columnMetaMap.get(content.getFieldName().toLowerCase())).orElse(columnMetaMap.get(content.getFieldName().toUpperCase()));
604+
ps.setNull(pos,columnMeta.getDataType());
605+
}
606+
pos++;
607+
}
608+
}
609+
610+
}catch (Exception ex){
611+
ex.printStackTrace();
612+
}
613+
});
614+
return Arrays.stream(rs[0]).sum();
615+
}catch (Exception ex){
616+
throw new DAOException(ex);
617+
}
618+
}
567619

568620
@Override
569621
public <T extends BaseObject> int updateVO(T obj, List<FilterCondition> conditions) throws DAOException {

core/src/main/java/com/robin/core/base/dao/util/EntityMappingUtil.java

+24
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ else if (content.isSequential()) {
155155
}
156156
return insertSegment;
157157
}
158+
public static String getInsertSqlIgnoreValue(Class<? extends BaseObject> clazz, BaseSqlGen sqlGen, JdbcDao jdbcDao,List<FieldContent> fields){
159+
AnnotationRetriever.EntityContent<? extends BaseObject> tableDef = AnnotationRetriever.getMappingTableByCache(clazz);
160+
StringBuilder buffer = new StringBuilder();
161+
buffer.append(Const.SQL_INSERTINTO);
162+
if (tableDef.getSchema() != null && !tableDef.getSchema().isEmpty()) {
163+
buffer.append(sqlGen.getSchemaName(tableDef.getSchema())).append(".");
164+
}
165+
buffer.append(tableDef.getTableName());
166+
StringBuilder fieldBuffer = new StringBuilder();
167+
StringBuilder valueBuffer = new StringBuilder();
168+
for (FieldContent content : fields) {
169+
if(content.isPrimary()){
170+
if(content.isSequential()){
171+
fieldBuffer.append(content.getFieldName()).append(",");
172+
valueBuffer.append(sqlGen.getSequenceScript(content.getSequenceName())).append(",");
173+
}
174+
}else{
175+
fieldBuffer.append(content.getFieldName()).append(",");
176+
valueBuffer.append("?,");
177+
}
178+
}
179+
buffer.append("(").append(fieldBuffer.substring(0, fieldBuffer.length() - 1)).append(") values (").append(valueBuffer.substring(0, valueBuffer.length() - 1)).append(")");
180+
return buffer.toString();
181+
}
158182

159183
public static Map<String, DataBaseColumnMeta> returnMetaMap(Class<? extends BaseObject> clazz, BaseSqlGen sqlGen, JdbcDao jdbcDao, AnnotationRetriever.EntityContent<? extends BaseObject> tableDef) throws SQLException {
160184
Map<String, DataBaseColumnMeta> columnMetaMap;

core/src/main/java/com/robin/core/base/service/BaseAnnotationJdbcService.java

+10
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ public List<V> queryByCondition(FilterCondition filterConditions){
363363
}
364364
return pageQuery.getRecordSet();
365365
}
366+
@Override
367+
@Transactional(rollbackFor = RuntimeException.class)
368+
public int batchUpdate(List<V> list){
369+
try {
370+
return jdbcDao.batchUpdate(list,type);
371+
}catch (DAOException ex){
372+
throw new ServiceException(ex);
373+
}
374+
}
375+
366376
public int countByCondition(FilterCondition filterCondition){
367377
try {
368378
return getJdbcDao().countByCondition(type,filterCondition);

core/src/main/java/com/robin/core/base/service/IBaseAnnotationJdbcService.java

+1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ public interface IBaseAnnotationJdbcService<V extends BaseObject, P extends Seri
6767
V getByField(String fieldName,Const.OPERATOR oper,Object... fieldValues) throws ServiceException;
6868
V getByField(PropertyFunction<V,?> function,Const.OPERATOR oper,Object... fieldValues) throws ServiceException;
6969
int countByCondition(FilterCondition filterCondition);
70+
int batchUpdate(List<V> list);
7071
}

core/src/main/java/com/robin/core/base/service/SpringAutoCreateService.java

+5
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ private static <T> T executeInTransaction(Function<JdbcDao, T> action) {
450450
}
451451
}
452452

453+
@Override
454+
public int batchUpdate(List<B> list) {
455+
return jdbcDao.batchUpdate(list,potype);
456+
}
457+
453458
public static JdbcDao getJdbcDao() {
454459
return jdbcDao;
455460
}

core/src/main/java/com/robin/core/hardware/MachineIdUtils.java

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.robin.core.base.shell.CommandLineExecutor;
44

55
import java.io.*;
6+
import java.lang.reflect.Field;
7+
import java.lang.reflect.Method;
68
import java.math.BigInteger;
79
import java.time.LocalDateTime;
810
import java.time.ZoneOffset;
@@ -20,6 +22,7 @@ public static String getMachineId() throws RuntimeException {
2022
BufferedReader reader = null;
2123
try {
2224
if (isWindows()) {
25+
disableAccessWarnings();
2326
machineId = WinRegistry.readString(WinRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGuid");
2427
} else if (isLinux()) {
2528

@@ -58,6 +61,26 @@ public static String getMachineId() throws RuntimeException {
5861
}
5962
}
6063
}
64+
@SuppressWarnings("unchecked")
65+
public static void disableAccessWarnings() {
66+
try {
67+
Class unsafeClass = Class.forName("sun.misc.Unsafe");
68+
Field field = unsafeClass.getDeclaredField("theUnsafe");
69+
field.setAccessible(true);
70+
Object unsafe = field.get(null);
71+
72+
Method putObjectVolatile =
73+
unsafeClass.getDeclaredMethod("putObjectVolatile", Object.class, long.class, Object.class);
74+
Method staticFieldOffset = unsafeClass.getDeclaredMethod("staticFieldOffset", Field.class);
75+
76+
Class loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
77+
Field loggerField = loggerClass.getDeclaredField("logger");
78+
Long offset = (Long)staticFieldOffset.invoke(unsafe, loggerField);
79+
putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
80+
} catch (Exception ignored) {
81+
}
82+
}
83+
6184
public static String getCPUSerial() throws RuntimeException{
6285
String serial="";
6386
try {

core/src/main/java/com/robin/core/hardware/WinRegistry.java

+10-25
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,25 @@ public class WinRegistry {
3333

3434
static {
3535
try {
36-
regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
37-
new Class[]{long.class, byte[].class, int.class});
36+
regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",new Class[]{long.class, byte[].class, int.class});
3837
regOpenKey.setAccessible(true);
39-
regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
40-
new Class[]{long.class});
38+
regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",new Class[]{long.class});
4139
regCloseKey.setAccessible(true);
42-
regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
43-
new Class[]{long.class, byte[].class});
40+
regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",new Class[]{long.class, byte[].class});
4441
regQueryValueEx.setAccessible(true);
45-
regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
46-
new Class[]{long.class, int.class, int.class});
42+
regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",new Class[]{long.class, int.class, int.class});
4743
regEnumValue.setAccessible(true);
48-
regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
49-
new Class[]{long.class});
44+
regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",new Class[]{long.class});
5045
regQueryInfoKey.setAccessible(true);
51-
regEnumKeyEx = userClass.getDeclaredMethod(
52-
"WindowsRegEnumKeyEx", new Class[]{long.class, int.class,
53-
int.class});
46+
regEnumKeyEx = userClass.getDeclaredMethod("WindowsRegEnumKeyEx", new Class[]{long.class, int.class,int.class});
5447
regEnumKeyEx.setAccessible(true);
55-
regCreateKeyEx = userClass.getDeclaredMethod(
56-
"WindowsRegCreateKeyEx", new Class[]{long.class,
57-
byte[].class});
48+
regCreateKeyEx = userClass.getDeclaredMethod("WindowsRegCreateKeyEx", new Class[]{long.class, byte[].class});
5849
regCreateKeyEx.setAccessible(true);
59-
regSetValueEx = userClass.getDeclaredMethod(
60-
"WindowsRegSetValueEx", new Class[]{long.class,
61-
byte[].class, byte[].class});
50+
regSetValueEx = userClass.getDeclaredMethod("WindowsRegSetValueEx", new Class[]{long.class,byte[].class, byte[].class});
6251
regSetValueEx.setAccessible(true);
63-
regDeleteValue = userClass.getDeclaredMethod(
64-
"WindowsRegDeleteValue", new Class[]{long.class,
65-
byte[].class});
52+
regDeleteValue = userClass.getDeclaredMethod("WindowsRegDeleteValue", new Class[]{long.class, byte[].class});
6653
regDeleteValue.setAccessible(true);
67-
regDeleteKey = userClass.getDeclaredMethod(
68-
"WindowsRegDeleteKey", new Class[]{long.class,
69-
byte[].class});
54+
regDeleteKey = userClass.getDeclaredMethod("WindowsRegDeleteKey", new Class[]{long.class, byte[].class});
7055
regDeleteKey.setAccessible(true);
7156
} catch (Exception e) {
7257
throw new RuntimeException(e);

estool/src/main/java/com/robin/es/service/ESRepositoryService.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ private void doQuery(PageQuery<Map<String, Object>> page, List<V> retList, Searc
378378
SearchHit[] hits = response.getHits().getHits();
379379
page.setRecordCount(Integer.valueOf(String.valueOf(response.getHits().getTotalHits().value)));
380380
if (!ObjectUtils.isEmpty(hits) && hits.length > 0) {
381-
retList = new ArrayList<>();
382381
for (SearchHit hit : hits) {
383382
Map<String, Object> map = hit.getSourceAsMap();
384383
Iterator<Map.Entry<String, Object>> citer = map.entrySet().iterator();
@@ -550,6 +549,11 @@ protected Map<String, String> settingMap() {
550549
return settingsMap;
551550
}
552551

552+
@Override
553+
public int batchUpdate(List<V> list) {
554+
return 0;
555+
}
556+
553557
protected boolean indexExists() {
554558
try {
555559
return client.indices().exists(new GetIndexRequest(entityContent.getTableName()), RequestOptions.DEFAULT);

example/base-example/src/main/java/com/robin/basis/dto/SysOrgDTO.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.robin.basis.model.system.SysOrg;
44
import lombok.Data;
5-
import org.springframework.beans.BeanUtils;
65

76
import java.util.ArrayList;
87
import java.util.List;
@@ -11,12 +10,12 @@
1110
public class SysOrgDTO {
1211
private Long id;
1312
private String label;
14-
private Long upOrgId;
13+
private Long pid;
1514
private List<SysOrgDTO> children=new ArrayList<>();
1615
public static SysOrgDTO fromVO(SysOrg org){
1716
SysOrgDTO dto=new SysOrgDTO();
1817
dto.setId(org.getId());
19-
dto.setUpOrgId(org.getUpOrgId());
18+
dto.setPid(org.getPid());
2019
dto.setLabel(org.getOrgName());
2120
return dto;
2221
}

example/base-example/src/main/java/com/robin/basis/model/system/SysOrg.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class SysOrg extends BaseObject
3232
@MappingField(value ="org_status")
3333
private String orgStatus;
3434
@MappingField(value ="up_org_id")
35-
private Long upOrgId;
35+
private Long pid;
3636
@MappingField(value ="org_code")
3737
private String orgCode;
3838
@MappingField(value ="org_name")

example/base-example/src/main/java/com/robin/basis/service/system/SysOrgService.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717

1818
import com.robin.basis.dto.SysOrgDTO;
1919
import com.robin.basis.model.system.SysOrg;
20+
import com.robin.basis.model.user.SysUserOrg;
2021
import com.robin.core.base.service.BaseAnnotationJdbcService;
2122
import com.robin.core.base.service.IBaseAnnotationJdbcService;
2223
import com.robin.core.base.util.Const;
2324
import org.springframework.context.annotation.Scope;
2425
import org.springframework.stereotype.Component;
26+
import org.springframework.transaction.annotation.Transactional;
27+
import org.springframework.util.Assert;
2528
import org.springframework.util.CollectionUtils;
2629

30+
import java.util.ArrayList;
31+
import java.util.HashMap;
2732
import java.util.List;
2833
import java.util.Map;
2934
import java.util.function.Function;
@@ -49,22 +54,39 @@ public String getSubIdByParentOrgId(Long orgId){
4954
return builder.substring(0,builder.length()-1);
5055
}
5156
}
57+
@Transactional(rollbackFor = RuntimeException.class)
58+
public int joinOrg(Long orgId,List<Long> uids){
59+
Assert.isTrue(!CollectionUtils.isEmpty(uids),"");
60+
Map<String,Object> map=new HashMap<>();
61+
map.put("ids",uids);
62+
Integer existCount=jdbcDao.countByNameParam("select count(1) from t_sys_user_info where id in (:ids) and user_status='1'",map);
63+
Assert.isTrue(existCount==uids.size(),"");
64+
List<SysUserOrg> list=new ArrayList<>();
65+
for(Long uid:uids){
66+
SysUserOrg userOrg=new SysUserOrg();
67+
userOrg.setUserId(uid);
68+
userOrg.setOrgId(orgId);
69+
userOrg.setStatus(Const.VALID);
70+
list.add(userOrg);
71+
}
72+
return jdbcDao.batchUpdate(list,SysUserOrg.class);
73+
}
5274
public List<SysOrgDTO> getOrgTree(Long pid){
5375
List<SysOrgDTO> orgList=queryAll().stream().filter(f->Const.VALID.equals(f.getOrgStatus())).map(SysOrgDTO::fromVO).collect(Collectors.toList());
54-
Map<Long,List<SysOrgDTO>> parentMap=orgList.stream().collect(Collectors.groupingBy(SysOrgDTO::getUpOrgId));
76+
Map<Long,List<SysOrgDTO>> parentMap=orgList.stream().collect(Collectors.groupingBy(SysOrgDTO::getPid));
5577
Map<Long,SysOrgDTO> idMap=orgList.stream().collect(Collectors.toMap(SysOrgDTO::getId, Function.identity()));
5678
SysOrgDTO root = new SysOrgDTO();
5779
idMap.put(0L, root);
5880
for(SysOrgDTO dto:orgList){
59-
idMap.get(dto.getUpOrgId()).getChildren().add(dto);
81+
idMap.get(dto.getPid()).getChildren().add(dto);
6082
walkOrg(idMap,parentMap,dto);
6183
}
6284
return idMap.get(pid).getChildren();
6385
}
6486
private void walkOrg(Map<Long,SysOrgDTO> idMap,Map<Long,List<SysOrgDTO>> parentMap,SysOrgDTO dto) {
6587
if (!CollectionUtils.isEmpty(parentMap.get(dto.getId()))) {
6688
for (SysOrgDTO childs : parentMap.get(dto.getId())) {
67-
idMap.get(dto.getUpOrgId()).getChildren().add(childs);
89+
idMap.get(dto.getPid()).getChildren().add(childs);
6890
walkOrg(idMap, parentMap, childs);
6991
}
7092
}

0 commit comments

Comments
 (0)