PostgreSQL和Hibernate整合

NoSQL特性整合

要查询的数据格式:

{"1":{"signNo":"1","signDate":"2018-08-20","isSupply":"1","signStatus":"0"},"2":{"signNo":"2","signDate":"2018-08-21","isSupply":"0","signStatus":"0"},"3":{"signNo":"3","signDate":"2018-08-22","isSupply":"0","signStatus":"0"},"4":{"signNo":"4","signDate":"2018-08-23","isSupply":"0","signStatus":"0"},"5":{"signNo":"5","signDate":"2018-08-24","isSupply":"0","signStatus":"0"},"6":{"signNo":"6","signDate":"2018-08-25","isSupply":"0","signStatus":"0"},"7":{"signNo":"7","signDate":"2018-08-26","isSupply":"0","signStatus":"0"}}

在PostgreSQL中存储的是一个json类型的字段,查询需要使用PostgreSQL特定的语法,代码如下:

    public int getNowWeek(String userId) {
        List list = this.createSQLQuery("SELECT (sign_status->'" + tt.getNowWeekDay() + "')->'signStatus' FROM sign_score WHERE user_id = " + Integer.parseInt(userId)).list();
        int weekday = Integer.parseInt(list.get(0).replaceAll("\"", ""));
        if (weekday == 1) {
            return -1;
        }
        return tt.getNowWeekDay();
    }

用字段加 -> 指向json字段的key值,可以得到它的value值,Hibernate中返回的是一个List集合,用createSQLQuery方法来实现原生SQL的查询。

你可能感兴趣的:(PostgreSQL和Hibernate整合)