Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA-从联接SQL查询向JSON添加对象_Java_Mysql_Json - Fatal编程技术网

JAVA-从联接SQL查询向JSON添加对象

JAVA-从联接SQL查询向JSON添加对象,java,mysql,json,Java,Mysql,Json,我希望标题设置得很好 我有一个现有的Java应用程序(我无法更改所有的应用程序),它只是使用gson库将db表公开为JSON 我的问题是如何创建Json,不仅使用DB的简单字段,而且使用链接对象 我有一张这样的桌子 | objectid | plantid | other_fields | 现在,现有的gson只将plantid作为一个整数提供给我,但我有一个plants表 | plantid | plantname | other_fields | 我希望gson与Plant对象一起使用,

我希望标题设置得很好

我有一个现有的Java应用程序(我无法更改所有的应用程序),它只是使用gson库将db表公开为JSON

我的问题是如何创建Json,不仅使用DB的简单字段,而且使用链接对象

我有一张这样的桌子

| objectid | plantid | other_fields |
现在,现有的gson只将
plantid
作为一个整数提供给我,但我有一个
plants

| plantid | plantname | other_fields |
我希望gson与Plant对象一起使用,而不仅仅是plantid

这就是应用程序将Sql查询映射到模型的方式

public ArrayList<ObjectsEntity> getObject(String objectId){
        ArrayList<ObjectsEntity> list = new ArrayList<>();
        ResultSet rs = null;
        try {
            Connection connLocal = connFactLocal.getConnection();
            String query = "select * from OBJECTS o left join PLANTS p on o.PLANTID = p.PLANTID "
                    + "left join OBJECTTYPES ot on o.OBJECTTYPEID = ot.OBJECTTYPEID";
            Statement stmt = connLocal.createStatement();
            rs = stmt.executeQuery(query);
            while (rs.next()) {
                ObjectsEntity oe = new ObjectsEntity();
                oe.setOBJECTID(rs.getInt("OBJECTID"));
                oe.setPLANTID(rs.getInt("PLANTID"));
                oe.setOBJECTNAME(rs.getString("OBJECTNAME"));
                oe.setOBJECTTYPEID(rs.getInt("OBJECTTYPEID"));
                oe.setDESCRIPTION(rs.getString("DESCRIPTION"));
                oe.setISMONITORED(rs.getBoolean("ISMONITORED"));
                oe.setGROUP(rs.getString("GROUP"));
                list.add(oe);
            }
        } catch (SQLException e){
            logger.error("SQL Error:",e);
        }
        return list;
    }


但是我不知道如何从查询的结果集设置植物。。。有什么想法吗?

您需要使用resultset中的数据填充Plants对象,并将其设置为ObjectSenty。使用ObjectMapper,您可以将ObjectsEntity转换为JSON字符串

    while (rs.next()) {
                    ObjectsEntity oe = new ObjectsEntity();
                    oe.setOBJECTID(rs.getInt("OBJECTID"));
                    Plants plants = new Plants();
                    plants.setId(rs.getInt("PLANTID"));
                      //Set other fields.
                    oe.setPLANTS(plants);
                    oe.setOBJECTNAME(rs.getString("OBJECTNAME"));
                    oe.setOBJECTTYPEID(rs.getInt("OBJECTTYPEID"));
                    oe.setDESCRIPTION(rs.getString("DESCRIPTION"));
                    oe.setISMONITORED(rs.getBoolean("ISMONITORED"));
                    oe.setGROUP(rs.getString("GROUP"));
                    list.add(oe);
                }

因为您已经有了一个对象来存储数据,所以只需更改
oe.setPLANTID(rs.getInt(“PLANTID”)用于
植物=新植物();植物。setId(rs.getInt(“PLANTID”);植物。其他地(……)。。。。;oe.植物(植物)哪个ObjectMapper?杰克逊?你想让OP知道你指的是什么类吗?Jackson ObjectMapper。这将帮助您将java对象转换为JSON字符串。你需要在ObjectsEntity上使用@JsonProperty注释,我非常了解Jackon,但这不是我评论的重点(你也不需要注释)。你能详细说明你的问题吗?问题是如何使用resultsethanks@MayurB设置植物,它也适用于Gson库,不需要使用Jackson
Plants plant
    while (rs.next()) {
                    ObjectsEntity oe = new ObjectsEntity();
                    oe.setOBJECTID(rs.getInt("OBJECTID"));
                    Plants plants = new Plants();
                    plants.setId(rs.getInt("PLANTID"));
                      //Set other fields.
                    oe.setPLANTS(plants);
                    oe.setOBJECTNAME(rs.getString("OBJECTNAME"));
                    oe.setOBJECTTYPEID(rs.getInt("OBJECTTYPEID"));
                    oe.setDESCRIPTION(rs.getString("DESCRIPTION"));
                    oe.setISMONITORED(rs.getBoolean("ISMONITORED"));
                    oe.setGROUP(rs.getString("GROUP"));
                    list.add(oe);
                }