标题我也不知道怎么起才好,但是我描述一下我的功能场景:
我用数组接到组参数值,在hibernate 我准备用 IN 作为where条件,
代码如下:
String strSql = " FROM DPourRecord WHERE wheel_code in (?)";
List paramsList = new ArrayList();
paramsList.add(paramsStr);
try {
list = getHibernateTemplate().find(strSql,paramsList.toArray());
System.out.println("*******"+list.size());
} catch (RuntimeException e) {
e.printStackTrace();
log.error("find wheels by wheelcodes is failed!");
}但是这样就成功不了,后来我把数组的值拼接了一个类型这样的字符串:
in ('12','13','14')
也就是说:把数组组合成这样的 '12','13','14'形式,程序不出错了,但是查询不出结果来,如果将sql 直接通过sql 界面工具 执行,比如
select * from tableName where colName1 in ('12','13','14') 就能查找出正确的结果集请各位大侠帮助一下!另外,在使用spring hibernate 过程中批量 更新和插入,大家有没有好的建议,在网上大家都不推荐使用hibernate 去做批量操作,从效率讲,不如直接使用jdbc ,可是如果抛开效率的话,(因为我本身的批量数据量也不会太大),大家有好的解决方案可以供我借鉴和参考的嘛?谢谢大家了!欢迎赐教!
我用数组接到组参数值,在hibernate 我准备用 IN 作为where条件,
代码如下:
String strSql = " FROM DPourRecord WHERE wheel_code in (?)";
List paramsList = new ArrayList();
paramsList.add(paramsStr);
try {
list = getHibernateTemplate().find(strSql,paramsList.toArray());
System.out.println("*******"+list.size());
} catch (RuntimeException e) {
e.printStackTrace();
log.error("find wheels by wheelcodes is failed!");
}但是这样就成功不了,后来我把数组的值拼接了一个类型这样的字符串:
in ('12','13','14')
也就是说:把数组组合成这样的 '12','13','14'形式,程序不出错了,但是查询不出结果来,如果将sql 直接通过sql 界面工具 执行,比如
select * from tableName where colName1 in ('12','13','14') 就能查找出正确的结果集请各位大侠帮助一下!另外,在使用spring hibernate 过程中批量 更新和插入,大家有没有好的建议,在网上大家都不推荐使用hibernate 去做批量操作,从效率讲,不如直接使用jdbc ,可是如果抛开效率的话,(因为我本身的批量数据量也不会太大),大家有好的解决方案可以供我借鉴和参考的嘛?谢谢大家了!欢迎赐教!
数组组合成str = '12','13','14'; 然后setString(1,str)同样查不出值,因为数据库把'12','13','14'当作一个字符串来处理,比如oracle会把'12','13','14'在你调用的时候转为 ''12'',''13'',''14'',这就是为什么我们说PreparedStatement能防止sql注入的原因。
批量操作不知道hib会否提供executeBatch()这样的功能,如果没有,还是用jdbc的快这是肯定的。
你的批量数据不大的话,for循环拉倒吧呵呵
->
List<String> paramsList = new ArrayList(); paramsList.toArray()
->
paramsList.toArray(new String[]{})
throws Exception {
String s = "";
File f = new File(fileName);
if (!f.exists()) {
throw new Exception("file doesn't exist....");
}
BufferedReader br = null;
PreparedStatement pstmt = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(f)));
String sql = "insert into tbl_report(单价,工号,统计量,日期)"
+ "values(?,?,?,'2009-12-07')";
pstmt = con.prepareStatement(sql);
// 循环外部准备好prepareStatement;
while ((s = br.readLine()) != null) {
if (s.indexOf("合计") > 0) {
continue;
}
String[] c = s.split("\t");
printArray(c);
if (c.length == 3) {
// 加入批量参数
pstmt.setString(1, c[0]);
pstmt.setString(2, c[1]);
pstmt.setString(3, c[2]);
pstmt.addBatch();
}
}
// 一次执行。
pstmt.executeBatch();
System.out.println("file write back finished");
} catch (Exception e) {
throw e;
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
select A from com.po.TDocument A where A.docId in (:ids)
调用的时候:
List<Integer>lst=初始化一个集合。
query=getSession().getNamedQuery("HGetDocs");
query.setParameterList("ids", lst);
http://shop58425488.taobao.com/
这个我用过,应该是没有问题的。
8楼的方法你可一试。