zoukankan      html  css  js  c++  java
  • 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

    持续更新中、、、

  • 相关阅读:
    ping命令的几个简单使用
    CentOS下编译安装hping3
    CentOS下安装gns3
    sendip简单使用
    Ubuntu/CentOS使用BIND配置DNS服务器
    远程重启linux主机的几种方法
    使用U盘安装win7系统,遇到“无法定位现有系统分区”问题
    导出csv文件
    Mvc 分页栏扩展方法
    初学HTML5系列三:事件
  • 原文地址:https://www.cnblogs.com/notchangeworld/p/11692311.html
Copyright © 2011-2022 走看看