private int getTotal(final int page, final int pageSize,final int productTypeId, final int progress,final String orderCode,final Date startDate, final Date stopDate,final String clientUserName) {
Session session = (Session) this.hibernateTemplate.getSessionFactory().openSession(); StringBuffer buff = new StringBuffer();
buff.append(" from Orders o ");
boolean where = false;
if(productTypeId !=0 && !"".equals(productTypeId)){
buff.append(" where o.workBillInfo.productType.productTypeId=:productTypeId ");
where = true;
}
if (progress!=0 && !"".equals(progress)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode=:progress ");
}
if (progress==0) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode!=:progressC ");
}
if (orderCode != null && !"".equals(orderCode)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.orderCode=:orderCode ");
}
if (startDate != null && !"".equals(startDate)
&& stopDate!= null && !"".equals(stopDate)) {
if (where) {
buff.append(" and ");
} else {
buff.append(" where ");
where = true;
}
buff.append(" o.orderTime between :startDate and :stopDate ");
}
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.clientLoginInfo.clientUserName=:clientUserName ");
Query query=session.createQuery(buff.toString());
if (progress!=0 && !"".equals(progress)) {
query.setInteger("progress", progress);
}
if (productTypeId != 0 && !"".equals(productTypeId)) {
query.setInteger("productTypeId", productTypeId);
}
if (orderCode != null && !"".equals(orderCode)) {
query.setString("orderCode", orderCode);
}
if (progress==0) {
query.setInteger("progressC", 17);
}
if (startDate != null && stopDate != null) {
query.setDate("startDate", startDate);
query.setDate("stopDate", stopDate);
}
query.setString("clientUserName", clientUserName);
return query.list().size();
}
我打了断点,只要一执行到return那句 控制台就打印出20多次相同的查询 求解
Session session = (Session) this.hibernateTemplate.getSessionFactory().openSession(); StringBuffer buff = new StringBuffer();
buff.append(" from Orders o ");
boolean where = false;
if(productTypeId !=0 && !"".equals(productTypeId)){
buff.append(" where o.workBillInfo.productType.productTypeId=:productTypeId ");
where = true;
}
if (progress!=0 && !"".equals(progress)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode=:progress ");
}
if (progress==0) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode!=:progressC ");
}
if (orderCode != null && !"".equals(orderCode)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.orderCode=:orderCode ");
}
if (startDate != null && !"".equals(startDate)
&& stopDate!= null && !"".equals(stopDate)) {
if (where) {
buff.append(" and ");
} else {
buff.append(" where ");
where = true;
}
buff.append(" o.orderTime between :startDate and :stopDate ");
}
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.clientLoginInfo.clientUserName=:clientUserName ");
Query query=session.createQuery(buff.toString());
if (progress!=0 && !"".equals(progress)) {
query.setInteger("progress", progress);
}
if (productTypeId != 0 && !"".equals(productTypeId)) {
query.setInteger("productTypeId", productTypeId);
}
if (orderCode != null && !"".equals(orderCode)) {
query.setString("orderCode", orderCode);
}
if (progress==0) {
query.setInteger("progressC", 17);
}
if (startDate != null && stopDate != null) {
query.setDate("startDate", startDate);
query.setDate("stopDate", stopDate);
}
query.setString("clientUserName", clientUserName);
return query.list().size();
}
我打了断点,只要一执行到return那句 控制台就打印出20多次相同的查询 求解
解决方案 »
- weblogic10.3.3.0部署的java程序,登录应用程序之后,报错如下
- 每年双月底之前提交一次文档。怎么设计数据库?请过来人赐教!
- dwr Illegal access to default constructor错误
- java 的中断执行
- ${ }如何嵌入JSP表达式中?
- 如何将java文件生成web service? 更高要求
- 关于 hibernate的问题?
- axis 写SOAP头,做鉴权处理时WebService端取不到数据。
- 哪位有装了Android ADT插件的Eclipse ,麻烦发我一个。
- 如何在WEBLOGIC 8下部署单独的SERVLET文件
- struts1 和 struts2的区别
- 请问:用java语言写一个方法,返回值是3的n次方,long类型;输入参数是n,整型。如何实现?
private int getTotal(final int page, final int pageSize,final int productTypeId, final int progress,final String orderCode,final Date startDate, final Date stopDate,final String clientUserName) {
Session session = (Session) this.hibernateTemplate.getSessionFactory().openSession(); StringBuffer buff = new StringBuffer();
buff.append(" from Orders o ");
boolean where = false;
if(productTypeId !=0 && !"".equals(productTypeId)){
buff.append(" where o.workBillInfo.productType.productTypeId=:productTypeId ");
where = true;
}
if (progress!=0 && !"".equals(progress)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode=:progress ");
}
if (progress==0) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where = true;
}
buff.append(" o.progress.progressCode!=:progressC ");
}
if (orderCode != null && !"".equals(orderCode)) {
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.orderCode=:orderCode ");
}
if (startDate != null && !"".equals(startDate)
&& stopDate!= null && !"".equals(stopDate)) {
if (where) {
buff.append(" and ");
} else {
buff.append(" where ");
where = true;
}
buff.append(" o.orderTime between :startDate and :stopDate ");
}
if(where){
buff.append(" and ");
}else{
buff.append(" where ");
where =true;
}
buff.append(" o.clientLoginInfo.clientUserName=:clientUserName ");
Query query=session.createQuery(buff.toString());
if (progress!=0 && !"".equals(progress)) {
query.setInteger("progress", progress);
}
if (productTypeId != 0 && !"".equals(productTypeId)) {
query.setInteger("productTypeId", productTypeId);
}
if (orderCode != null && !"".equals(orderCode)) {
query.setString("orderCode", orderCode);
}
if (progress==0) {
query.setInteger("progressC", 17);
}
if (startDate != null && stopDate != null) {
query.setDate("startDate", startDate);
query.setDate("stopDate", stopDate);
}
query.setString("clientUserName", clientUserName);
return query.list().size();
}
2. 写这么多就为了判断第一个是and还是where 直接在buff.append(" from Orders o where 1=1 ");后面都是and就好了