NutzCN Logo
问答 自定义sql,在数据库中查询是有数据的,在代码中查询不到
发布于 1765天前 作者 码农 2725 次浏览 复制 上一个帖子 下一个帖子
标签:

日志里没有报错信息,试了几次,都不行。
在navicat执行拷贝日志打印的sql,可以获取到数据。

StringBuffer bf = new StringBuffer();
        bf.append(" (select edit_time,address,diagnose_total,diagnose_add,suspect_total,suspect_add, cure_total,cure_add,death_total,death_add  from hp_epidemic ");
        bf.append(" where type ='1' and edit_time >= DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s') ORDER BY edit_time desc LIMIT 0,1)   UNION ");
        bf.append(" (select edit_time,address,diagnose_total,diagnose_add,suspect_total,suspect_add, cure_total,cure_add,death_total,death_add  from hp_epidemic  ");
        bf.append(" where type ='2' and edit_time >= DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s') ORDER BY edit_time desc LIMIT 0,1) ");
        String sqls = bf.toString();
        Sql sql = Sqls.create(sqls);
        sql.setEntity(dao().getEntity(Record.class));
        sql.setCallback(Sqls.callback.entities());
        dao().execute(sql);
        List<Record> list = sql.getList(Record.class);
7 回复

edit_time 你如何传入的参数

可以参考 https://github.com/TomYule/NutzSite/blob/eb6ec4ffb03a17949647df4b79e42ec428ed2e5f/src/main/java/io/nutz/nutzsite/module/sys/services/MenuService.java

    /**
     * 查询用户菜单
     *
     * @param userId 用户id
     * @return 菜单
     */
    public List<Menu> getMenuList(String userId) {
        String sqlstr = "select distinct m.id, m.parent_id, m.menu_name, m.url, m.perms , m.menu_type, m.icon, m.order_num, m.create_time " +
                "from sys_menu m " +
                "left join sys_role_menu rm on m.id = rm.menu_id " +
                "left join sys_user_role ur on rm.role_id = ur.role_id " +
                "left join sys_role ro on ur.role_id = ro.id " +
                "where ur.user_id = @userId and m.menu_type in ('M', 'C') and m.visible = '0' " +
                "order by m.order_num";
        Sql sql = Sqls.create(sqlstr);
        sql.params().set("userId", userId);
        sql.setCallback(Sqls.callback.entities());
        Entity<Menu> entity = dao().getEntity(Menu.class);
        sql.setEntity(entity);
        dao().execute(sql);
        return sql.getList(Menu.class);
    }

@tonyyule edit_time不是入参,

(select edit_time,address,diagnose_total,diagnose_add,suspect_total,suspect_add, cure_total,cure_add,death_total,death_add  from hp_epidemic where type ='1' and edit_time >=DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s') ORDER BY edit_time desc LIMIT 0,1)
UNION
(select edit_time,address,diagnose_total,diagnose_add,suspect_total,suspect_add, cure_total,cure_add,death_total,death_add  from hp_epidemic where type ='2' and edit_time >=DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s') ORDER BY edit_time desc LIMIT 0,1)

你用MySQL 命令行 输入 SQL 看看是不是关键字 冲突

@tonyyule 不是,mysql命令行也能执行成功,获取到数据,放到代码中进行自定义执行成功,但是获取不到数据

日期用Date对象,别用命令行下才搞的format

添加回复
请先登陆
回到顶部