楼主的使用环境jpa+hibernate
报错:could not locate named parameter
请检查:
1.大小写与数据库字段严格对应
2.冒号
在java中的框架中,dao层进行sql语句拼接的时候会用到,“=:”的的用途?
1、防止sql注入:为防止sql注入的情况,通常在sql语句组装的时候,不会直接传值。
通过”=:变量名“的方式,在具体调用的时候传入参数,是防止sql注入的写法
2、提高性能
3.别忘了setParameter
parameterMap.put("organId", organId);
在hibernate中有两种传参方式
- 方式一:
select
h.regionid regionid,
r.region_name regionidName,
h.pubdate pubdate,
xlcl.f_gtDayAmount(h.regionid,
h.pubdate,
0) localAmount,
XLCL.f_gtDayAmount(h.regionid,
h.pubdate,
1) otherAmount,
XLCL.f_gtDayAmount(h.regionid,
h.pubdate,
2) interAmount,
sum(staynum) totalAmount
from
xlcldata.mfs_history_day h
inner join
xlclgis.mfb_region r
on h.regionid=r.id
where
h.areacode is not null
and r.pid=0
and h.pubdate=?
group by
h.regionid,
h.pubdate,
r.region_name
order by
totalAmount asc
忽略sql中的逻辑关系,有没有发现sql中的问号?那么在sql中传参查询时利用setParameter方法进行传参,具体如下:
query.setParameter("0",queryDate);//queryDate为日期对象
- 方式二:
把sql语句中问号那一行改为
and h.pubdate=:dateTime
那么传参方式为:
query.setParameter("dateTime",queryDate);
在实际工程项目中,第一种传参方法要多一些