java - Spring数据与关系表的序列化

标签 java mysql json spring serialization

问题:经过长时间的研究,我如何序列化具有两个表的数据库中的数据 I have found that tutorial 。但我需要从数据库中获取数据,但我不知道如何将数据连接到数据库。 我只找到了一张表的非关系样本。

问题:有人有 DAO 类的示例来从数据库中获取特征数据吗?

需要JSON结构:

[
   {
      "id":1,
      "name":"CNC",
      "beschreibung":"Metallverarbeitung",
      "characteristics":[
         "id_characteristic":1,
         "id_maschine":2,
         "name":"size",
         "description":"length of maschine",
         "type":1
      ]
   }
]

当前JSON结构:

[
   {
      "id":1,
      "name":"CNC",
      "beschreibung":"Metallverarbeitung",
      "characteristics":[

      ]
},

...

]

DAO方法(到目前为止,没有填充特征数组):

@Override
    public List<Maschine> list() {
        String selectMaschines = "SELECT * FROM maschine";


        List<Maschine> listMaschine = jdbcTemplate.query(selectMaschines, new RowMapper<Maschine>() {

            @Override
            public Maschine mapRow(ResultSet rs, int rowNum) throws SQLException {
                Maschine aMaschine = new Maschine();

                aMaschine.setId(rs.getInt("Maschine_id"));
                aMaschine.setName(rs.getString("name"));
                aMaschine.setBeschreibung(rs.getString("beschreibung"));


                return aMaschine;
            }

        });

        return listMaschine;
    }

表结构:

机器表:

maschine_id || name || description

特征表:

id_characteristic || id_maschine || name || description || type

最佳答案

如果你想使用 Spring 数据,你必须将 Spring 数据依赖项添加到你的 pom.xml

然后为您的实体添加注释:

@Entity
public class Maschine implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int maschine_id  ;
  private String name ;
  private String description ;
  @OneToMany(mappedBy="maschine")
  private Collection<Characteristic> characteristics;
public Maschine() {
    super();
    // TODO Auto-generated constructor stub
}
public int getMaschine_id() {
    return maschine_id;
}
public void setMaschine_id(int maschine_id) {
    this.maschine_id = maschine_id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

//特征实体

 @Entity
    public class Characteristic implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id_characteristic ;
        private String name;
        private String description;
        private String type ;
        @ManyToOne
        @JoinColumn(name="id_machine")
        private Maschine maschine;
        public int getId_characteristic() {
            return id_characteristic;
        }
        public void setId_characteristic(int id_characteristic) {
            this.id_characteristic = id_characteristic;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public Maschine getMaschine() {
            return maschine;
        }
        public void setMaschine(Maschine maschine) {
            this.maschine = maschine;
        }
        public Characteristic() {
            super();
            // TODO Auto-generated constructor stub
        }

    }

在您的 DAO 包中,您必须创建一个扩展 JPARepository 的接口(interface),例如:

public interface MaschineRepository extends JpaRepository<Maschine,Integer> {

} 

然后,当您调用 MaschineRepository.findAll() 时,您将获得所有具有其特征的机器

Spring数据依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

如果您使用 Spring Boot,请不要忘记在 application.properties 中添加数据库配置

关于java - Spring数据与关系表的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807478/

相关文章:

java - 如何以类型友好的方式包装可调用对象?

java - 如何根据 pojo 类值删除列表中的项目

Mysql:日期比较和连接

c# - HttpResponseMessage.Content.ReadAsStringAsync 不反序列化来自 CreateErrorResponse 的 JSON

javascript - 将 JSON 发布到 WCF REST 端点

json - Apache NiFi 拆分 JSON 根数组

java - JTree 事件似乎顺序错误

java - onSaveInstanceState() 事件中的savedInstanceState 和outState 有什么区别?

mysql - 如何从多个表中获取7天的数据

mysql - 如果有子查询,则未知连接列