hibernate使用原生SQL使用count统计表记录的数量

本例基于SSH框架。

public int getCountByUserCodeAndTel(String userCode, String tel) {
        int count = 0;
        String sql = 
                        "select count(*) from sm_user t1 , sm_user_information t2 \n" +
                        "where  t1.user_information_id = t2.id \n" + 
                        " and   t1. user_code = :userCode \n";
        if(StringUtils.isBlank(tel)){
                 sql += " and   t2.mobile_telephone is null ";
        }else{
                 sql += " and   t2.mobile_telephone = :tel";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userCode", userCode);
        map.put("tel", tel);
        
        List list = super.queryListBySql(sql, map);
       
        if(list!=null && list.size()>0){
            count = Integer.parseInt(String.valueOf(list.get(0)));
        }
        return count;
}

猜你喜欢

转载自blog.csdn.net/a310934797/article/details/86074398