简体   繁体   English

使用jdbcTemplate从两个表中显示多个记录

[英]Show multiple records from two tables using jdbcTemplate

So I have two tables: 所以我有两个表:

Samochod (id, FK id of silnik, model)
Silnik (id, capacity)

Now I want to list all my cars (all information from table "Samochod"). 现在,我要列出我的所有汽车(表“ Samochod”中的所有信息)。 I tried something like this: 我尝试过这样的事情:

public List<Samochod> listuj() {
    List<Samochod> samochodList = new ArrayList<Samochod>();
    String query = "select * from samochod";
    samochodList = jdbcTemplate.query(query, new BeanPropertyRowMapper(Samochod.class));
    return samochodList;
}

But I got en error: 但是我得到了错误:

Failed to convert property value of type 'java.lang.Integer' to required type 'pl.edu.lab1.Silnik'...

Somebody have idea how can I take capacity from table "Silnik"? 有人知道如何从表“ Silnik”中获取容量? Because I want to print model and capacity. 因为我要打印型号和容量。 For example I have in data base: 例如我在数据库中:

Samochod: id = 1, id of silnik = 3, model = "Hyundai"
Silnik: id = 3, capacity = 1.4

So I want to print "Hyundai 1.4". 所以我想打印“现代1.4”。

Anybody have some idea how can I do this? 有人知道我该怎么做吗?

@edit @编辑

Photos of my tables structure: Samochod: 我的桌子结构照片:Samochod: http://i.imgur.com/CFtnIQv.png

Silnik ("pojemnosc" = "capacity"): Silnik(“ pojemnosc” =“容量”): http://i.imgur.com/7NsJ5B9.png

Relations: 关系:

http://i.imgur.com/946ENz7.png

silnik field on class Samochod is of type Silnik. 类Samochod上的silnik字段为Silnik类型。 But on database is Integer. 但是在数据库上是Integer。 You need change type of silnik to integer or write you own BeanPropertyRowMapper . 您需要将silnik的类型更改为integer或编写自己的BeanPropertyRowMapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM