nutz的自定义sql

     Pager page = new Pager(pager.getCurrent(), pager.getPageSize());//分页
        NutDao dao = ioc.get(NutDao.class, "dao");

        //数据库查询数据
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT o1.id,o1.user_id,o1.name,o1.create_time,a3.name AS customer_name,a3.mobile,o4.user_contact FROM。。。")
if(StringUtils.isNotBlank(keyWords)){
            sb.append(" where o1.name like '%"+keyWords+"%' or a3.name like '%"+keyWords+"%' or a3.mobile like '%"+keyWords+"%' or o4.user_contact like '%"+keyWords+"%'");
        }

        Sql sql = Sqls.create(sb.toString());
        if(pager != null){
            sql.setPager(page);
        }
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List list = new LinkedList();
                while (null != rs && rs.next()) {
                    HashMap c = new HashMap();
                    c.put("id", rs.getInt("id"));
                    c.put("userId", rs.getString("user_id"));
                    c.put("name", rs.getString("name"));
                    c.put("createTime", rs.getLong("create_time"));
                    c.put("customerName", rs.getString("customer_name"));
                    c.put("mobile", rs.getString("mobile"));
                    c.put("userContact", rs.getString("user_contact"));
                    list.add(c);
                }
                return list;
            }
        });
        dao.execute(sql);
        List<HashMap> list = sql.getList(HashMap.class);

以上是自定义sql(因为有联查)的实现,其中要想获取数据是需要callback

当然还有普通的自定义sql查询:

List<SecurityServiceInstance> instanceList = operationSecurityServiceInstanceDAO.
                    query(SecurityServiceInstance.class, Cnd.where("customer_id", "=", userId)
                            .and("manage_status", "=", ManageStatusEnum.MANAGE.getCode())
                            .and("special_line_id", "=", id));

其中,operationSecurityServiceInstanceDAO继承了NutDao

猜你喜欢

转载自www.cnblogs.com/notchangeworld/p/11692311.html