SQL绑定变量的那些事

1.String bankAccountName = null == cb.getBankAccountName()?"":cb.getBankAccountName().trim();
String bankAccount = null == cb.getBankAccount()?"":cb.getBankAccount().trim();
String accountName = null == cb.getAccountName()?"":cb.getAccountName().trim();

CustomerBank customerBank = getHibernateTemplate().load(CustomerBank.class, cb.getCustId());
if(null != customerBank){
customerBank.setBankAccount(bankAccount);
customerBank.setBankName(cb.getBankName());
customerBank.setBankCode(cb.getBankCode());
customerBank.setBankAccountName(cb.getBankAccountName());
customerBank.setBankFlag(cb.getBankFlag());
customerBank.setAccountName(accountName);
customerBank.setUpdateTime(new Date());
customerBank.setUpdateUser(empCode);
getHibernateTemplate().saveOrUpdate(customerBank);
}
//用 拼接 形式 保存
/* String sql = "update cdh_customer_bank t set "
+"t.bank_account = '"+bankAccount+"',"
+"t.bank_name = '"+cb.getBankName()+"',"
+"t.bank_code = '"+cb.getBankCode()+"',"
+"t.bank_account_name = '"+bankAccountName+"',"
+"t.bank_flag = "+cb.getBankFlag()+","
+"t.account_name = '"+accountName+"',"
+"t.update_time = sysdate,"
+"t.update_user = '"+empCode+"' "
+"where t.cust_id = "+cb.getCustId();
                                
SQLQuery query = session.createSQLQuery(sql);
query.executeUpdate();*/

       return null;


2. return (Boolean) this.getHibernateTemplate().execute(new HibernateCallback (){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
//modify by 591797 绑定SQL变量
String sql = "select cp.sfgou_flag from cdh_protocol p, cdh_cod_protocol cp "
+ " where p.protocol_id = cp.protocol_id  and p.protocol_type =2 "
+ "and p.customer_code = ? ";//'"+customerCode+"'";

           SQLQuery query = session.createSQLQuery(sql);
           query.setParameter(0, customerCode);
           List lis = query.list();
           if(null != lis && lis.size() > 0 ){
               String str = lis.get(0)+"";
               if(str.equals("1")){
               return true;
               }else{
                   return false;
               }
           }
           return false;
}
});

3. return (List) super.getHibernateTemplate().execute(new HibernateCallback(){
@Override
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
boolean flag = false; //是否有条件
// String sql = "select t.cust_id,t.bank_account,t.bank_name,"+
//                     "  t.bank_code,t.bank_account_name,t.bank_flag,"+
//                     "  t.account_name,c.current_department, t.customer_code,"+
//                     "  a.billing_code,t.customer_id,c.fullname "+
//                     " from cdh_customer_bank t,cdh_customer c ,cdh_account a where c.customer_code(+) = t.customer_code "+
//                     "   and a.account_id(+) = c.account_id ";
String sql = "select t.cust_id,t.bank_account,t.bank_name,t.bank_code,t.bank_account_name,"+
                             "t.bank_flag,t.account_name,'' current_department,t.customer_code,'' billing_code,t.customer_id," +
                             "'' fullname   from cdh_customer_bank t Where 1=1 ";
//客户卡号
if(null != custCode && !"".equals(custCode)){
// sql += " AND T.CUSTOMER_CODE ='"+custCode.trim()+"'";
sql += " AND T.CUSTOMER_CODE = :code ";//'"+custCode.trim()+"'";
flag = true;
}
//银行名称
if(null != bankName && !"".equals(bankName)){
// sql += " AND T.BANK_NAME ='"+bankName.trim()+"'";
sql +=" AND T.BANK_NAME = :bankname ";//'"+bankName.trim()+"'";
flag = true;
}
//户名
if(null != accountName && !"".equals(accountName)){
// sql += " AND T.ACCOUNT_NAME ='"+accountName.trim()+"'";
sql += " AND T.ACCOUNT_NAME = :accountname ";//'"+accountName.trim()+"'";
flag = true;
}
//银行账号
if(null != bankAccount && !"".equals(bankAccount)){
// sql += " AND T.BANK_ACCOUNT ='"+bankAccount.trim()+"'";
sql += " AND T.BANK_ACCOUNT = :bankaccount ";//'"+bankAccount.trim()+"'";
flag = true;
}

//如果没有 任何的 查询条件 直接返回空, 目的是 防止 查询返回的数据量过大 造成内存溢出
if(!flag){
return  null;
}

SQLQuery query = session.createSQLQuery(sql);
if(null != custCode && !"".equals(custCode)){
query.setParameter("code", custCode.trim());
}
//银行名称
if(null != bankName && !"".equals(bankName)){
query.setParameter("bankname", bankName.trim());
}
//户名
if(null != accountName && !"".equals(accountName)){
query.setParameter("accountname", accountName.trim());
}
//银行账号
if(null != bankAccount && !"".equals(bankAccount)){
query.setParameter("bankaccount", bankAccount.trim());
}

       
       query = to_Type(query);//查询 返回类型 绑定
       List lis = query.list();
       List list = new ArrayList();
     
       for(Iterator it =lis.iterator();it.hasNext();){
        Object [] obj = (Object[]) it.next();
        CustomerBank cb = getCustomerBank(obj);//赋值
        list.add(cb);
       }
       return list;
}
});
}


测试找个spring定时器,修改定时器时间,在定时器调用的函数里增加如下的入口测试:



if(true){

          WebApplicationContext spring = ContextLoader.getCurrentWebApplicationContext();

           //WebApplicationContext spring =WebApplicationContextUtils..getRequiredWebApplicationContext(ServletActionContext.getServletContext());

           CcsCustomerReceiverDaoccsCustomerReceiverDao=(CcsCustomerReceiverDao)spring.getBean("ccsCustomerReceiverDao");

           Contact contact = newContact();

           contact.setName("汪先生");

           contact.setCustid("7559957925");

           contact.setId("7559957925002");

           contact.setOp("U");

          ccsCustomerReceiverDao.saveCcsCustContact(contact);

           return;

       }

 

进行修改测试。




你可能感兴趣的:(JAVA)