改为批量查询 String hql = "SELECT lc.tchargeclass FROM Tlicensecharge lc WHERE lc.hawkertypecode = :hawkertypecode AND lc.isfixed = :isfixed AND lc.hasfuffly = :hasfuffly AND lc.startyear <= :startyear ORDER BY lc.startyear";
时间: 2024-03-10 20:44:44 浏览: 75
是的,你的修改是正确的。将字符串拼接改为使用命名参数的方式可以避免SQL注入的风险。同时,这种方式也可以提高SQL语句的重用性和可读性。下面是修改后的Java代码示例:
public Tchargeclass findChargeclass(String hawkertypecode, String isfixed, String hasfuffly) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date d = new Date();
String hql = "SELECT lc.tchargeclass FROM Tlicensecharge lc WHERE lc.hawkertypecode = :hawkertypecode AND lc.isfixed = :isfixed AND lc.hasfuffly = :hasfuffly AND lc.startyear <= :startyear ORDER BY lc.startyear";
Map<String, Object> params = new HashMap<>();
params.put("hawkertypecode", hawkertypecode);
params.put("isfixed", isfixed);
params.put("hasfuffly", hasfuffly);
params.put("startyear", sdf.format(d));
List<Tchargeclass> l = basicDAO.find(hql, params);
if(l!=null && l.size()>0){
return l.get(0);
}
return null;
}
在这个修改后的代码中,我们使用了命名参数(:hawkertypecode、:isfixed、:hasfuffly和:startyear)来代替原来的字符串拼接方式。同时,我们使用了一个Map对象来保存参数,这个Map对象会在查询中被传递给basicDAO.find()方法。这样,我们就避免了SQL注入的风险,同时也提高了查询语句的可读性和可维护性。
相关推荐














