runsql
CustomSqlMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.runshi.runshi.cms.mapper.CustomSqlMapper">
<select id="runPageSql" resultType="java.util.Map" parameterType="java.lang.String">
${sqlStr}
</select>
<select id="runListSql" resultType="java.util.Map" parameterType="java.lang.String">
${sqlStr}
</select>
<select id="runSingleSql" resultType="java.util.Map" parameterType="java.lang.String">
${sqlStr}
</select>
</mapper>
CustomSqlMapper
@Mapper
public interface CustomSqlMapper extends RunshiBaseMapper<CmsArticleCategory> {
IPage<Map<String, Object>> runPageSql(Page page, @Param(value="sqlStr") String sqlStr);
List<Map<String, Object>> runListSql(@Param(value="sqlStr") String sqlStr);
Map<String, Object> runSingleSql(@Param(value="sqlStr") String sqlStr);
}
CustomSqlService
public interface CustomSqlService extends IService<CmsArticleCategory> {
IPage<Map<String, Object>> runPageSql(Page page, String sqlStr);
List<Map<String, Object>> runListSql(String sqlStr);
Map<String, Object> runSingleSql(String sqlStr);
}
CustomSqlServiceImpl
@Service
public class CustomSqlServiceImpl extends ServiceImpl<CustomSqlMapper, CmsArticleCategory> implements CustomSqlService {
@Override
public IPage runPageSql(Page page, String sqlStr) {
IPage dataList = baseMapper.runPageSql(page, sqlStr);
//dataList.getRecords()
List<Map<String,Object>> lst = new ArrayList<>();
lst =dataList.getRecords();
dataList.setRecords(lst.stream().map(MapUtil::toCamelCaseMap).collect(Collectors.toList()));
return dataList;
}
@Override
public List<Map<String, Object>> runListSql(String sqlStr) {
List<Map<String,Object>> lst = baseMapper.runListSql(sqlStr);
return lst.stream().map(MapUtil::toCamelCaseMap).collect(Collectors.toList());
}
@Override
public Map<String, Object> runSingleSql(String sqlStr) {
if (sqlStr.indexOf(" limit ") == -1) {
sqlStr += " limit 1 ";
}
return getUnderToHump(baseMapper.runSingleSql(sqlStr));
}
private Map<String, Object> getUnderToHump(Map<String, Object> map) {
if (Objects.isNull(map)) {
return map;
}
List<Map.Entry<String, Object>> list = map.entrySet().stream().collect(Collectors.toList());
Map<String, Object> paramMap = new HashMap<>();
for (Map.Entry<String, Object> entry : list) {
if (entry.getKey().indexOf("_") == -1) {
Object value = getValue(entry);
paramMap.put(entry.getKey(), value);
continue;
}
String[] split = entry.getKey().split("_");
StringBuilder result = new StringBuilder();
result.append(split[0]);
for (int i=1;i<split.length;i++) {
result.append(split[i].substring(0,1).toUpperCase()).append(split[i].substring(1));
}
paramMap.put(result.toString(), getValue(entry));
}
return paramMap;
}
private Object getValue(Map.Entry<String, Object> entry) {
Object value = entry.getValue();
if (entry.getValue() instanceof List) {
List listValue =(List) entry.getValue();
List result = new ArrayList();
for (Object o: listValue) {
Object convert = null;
if (o instanceof Map) {
Map mapList =(Map) o;
convert = getUnderToHump(mapList);
}
if (o instanceof Long) {
convert = String.valueOf(o);
}
result.add(convert);
}
value = result;
}
if (entry.getValue() instanceof Map) {
Map linkedHashMap =(Map) entry.getValue();
value = getUnderToHump(linkedHashMap);
}
if (entry.getValue() instanceof JsonArray) {
JsonArray value1 = (JsonArray) entry.getValue();
JsonArray transValue = new JsonArray();
for (JsonElement o:value1) {
if (o instanceof JsonObject) {
JsonObject jsonObject = o.getAsJsonObject();
Map underToHump = getUnderToHump(new Gson().fromJson(jsonObject.toString(), Map.class));
transValue.add(JsonParser.parseString(new Gson().toJson(underToHump)));
} else {
transValue.add(o.toString());
}
}
value = transValue;
}
if (entry.getValue() instanceof JsonObject) {
JsonObject jsonObject = (JsonObject) entry.getValue();
value = getUnderToHump( new Gson().fromJson(jsonObject.toString(),Map.class));
}
return value;
}
}
择善人而交,择善书而读,择善言而听,择善行而从。