sql注入问题优化,order by 里面不能用下面这种方式,为了防止sql注入,验证输入的值是否合法,比如只能包含英文,数字和下划线应以英文开头
Map<String, Object> parameters = new HashMap<>();
sql.append(" where (lzbh like :keyword or lzmc like :keyword)");
parameters.put("keyword", "%" + page.getKeyword() + "%");
countSql.append(" qxdm = :qydm");
parameters.put("qydm", page.getQydm());
List<Long> bsmList
sql.append(" where bsm in(:bsmList)");
parameters.put("bsmList", bsmList);
查询时
Query query = entityManager.createNativeQuery(sql.toString());
parameters.forEach(query::setParameter);
数组到排序
notEmptyList.sort(Comparator.comparing(StationQueryVO::getCurrentValue).reversed());
List<HouseBuildingExcelDTO> excelDTOList = ModelMapperUtil.getStrictModelMapper().map(list, new TypeToken<List<HouseBuildingExcelDTO>>() { }.getType()); CaseReportDO db = ModelMapperUtil.getStrictModelMapper().map(req, CaseReportDO.class);
pg数据库 date格式化
to_char(date_time,'yyyymmddHH24MIss')
List<LandCoverDO> dbList = iLandCoverMapper.findByGaMarkId(id);
if(dbList == null || dbList.isEmpty()){
return null;
}
LandCoverDTO dto = new LandCoverDTO();
//分组
Map<String, List<LandCoverDO>> grouping = dbList.stream().collect(Collectors.groupingBy(LandCoverDO::getDlmc));
List<NameDoubleValueVO> stat = new ArrayList<>();
//遍历分组里面的数据
grouping.forEach((dlmc, landCoverDOList) -> {
double totalArea = landCoverDOList.stream().mapToDouble(LandCoverDO::getGa_gdjmj).sum();
stat.add(new NameDoubleValueVO(dlmc, totalArea));
});
//排序
stat.sort(Comparator.comparing(NameDoubleValueVO::getValue).reversed());
提取对象数组里面的某个字段 List<String> lastTask = lastTasks.stream() .map(HistoricActivityInstance::getActivityId) .limit(1) .collect(Collectors.toList()); base64 private static final org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64(); fastjson解析null值问题: 解决 null的属性不显示问题 JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue); mysql 的sql 里面 的 where 1=1 在最后的查询语句里面会被优化调。但是这个where 1=1 可能会造成查询的性能问题。比如只有 where 1=1 ,后面没有其他的查询条件了,会造成全表扫描,影响查询性能
mybatis-plus里面使用动态查询
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class CustomJdbcService {
private final JdbcTemplate jdbcTemplate;
public CustomJdbcService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 查询数据返回 Map
public List<Map<String, Object>> executeCustomSql(String sql) {
// 注意防止 SQL 注入
if(StringUtils.isBlank(sql)){
return null;
}
return jdbcTemplate.queryForList(sql);
}
}
在其他类里面引用,比如在 QuestionRecordServiceImpl 类里面使用
private final CustomJdbcService customJdbcService;
public QuestionRecordServiceImpl(CustomJdbcService customJdbcService) {
this.customJdbcService = customJdbcService;
}
在没有被注入成bean的类里面想使用其他bean类
TaskService taskService = StaticApplicationContext.getBean(TaskService.class);
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class StaticApplicationContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
StaticApplicationContext.context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}
list对象数组给某个字段求和
totalArea = collect.stream()
.mapToDouble(RegionAreaDataVO::getValue)
.sum();
分页查询的问题,一般使用的分页查询都是limit offset去做分页查询。但是当数据量很大的时候,查询效率就非常慢。比如 limit 10 offset 100000,查询的时候就会扫描100010条数据,导致查询慢。
解决办法是,把主键id设置成int类型的(自增),然后修改查询语句。每次查询的时候将前一次查询出来的数据的最后一条数据的id传递给查询接口。这样的话就不能跳页查询了
SELECT * FROM szdt_rk_jbxxb where id > 15384193 ORDER BY bsm asc limit 10
如果在代码里面想使用HashMap来做临时缓存。最好别用HashMap来做。因为没有过期时间,可能导致内存溢出。可以用Guava Cache来替换HashMap,或者Ehchace
浙公网安备 33010602011771号