使用自定义的JdbcTemplate将查询结果映射到对象中。

huangapple go评论74阅读模式
英文:

Map Custom JdbcTemplate query result in an Object

问题

我是你的中文翻译,以下是翻译好的部分:

如果我从我的Employee表中选择员工数据,我可以将它映射到一个POJO模型User,并在该模型中定义表的结构,就像这样:

这是从jdbcTemplate的映射示例:

这是从一张表中选择数据的明确示例。

我的问题是,如果我的数据是自定义查询,如何映射查询的数据?比如使用连接并选择那些表的自定义字段,我需要为每个查询都创建POJO吗?

有时我只需要选择employee.id_employeeemployee.name字段,而在另一个控制器中,我需要从我的employee表中选择employee.id_employee

在另一种情况下,我只需要选择employee.nameemployee_product.product_name。是否有不需要为每种情况都创建POJO的替代方法?

英文:

I new in java and try to use spring framework. I have a question.
By example, I have table :

  1. employee (id_employee, name)
  2. employee_product (id_employee_product, id_employee, product_name)

if I select an employee data from my Employee table, I can map it in a POJO model User and define the tables structure in that model, like this:

public class Employee {
    private final int id_employee;
    private final String nama;

    public Employee(int id_employee, String nama){
        this.id_employee = id_employee;
        this.nama = nama;
    }

    public int getId() {
        return id_employee;
    }

    public String getNama() {
        return nama;
    }
}

And this is the map from jdbcTemplate:

final String sql = "SELECT id_employee, nama FROM employee";
return jdbcTemplate.query(sql, (resultSet, i) -> {
    return new Employee(
            resultSet.getInt("id_employee"),
            resultSet.getString("nama")
    );
});

That is clear example for select data from 1 table.

My question is, how to map data from query if my data is custom query? Such us using join and select custom field from that tables, Am I need to create POJO every query?

Sometimes I need to select only employee.id_employee, and employee.name field from my employee table.

And in another controller I need to select employee.id_employee from my employee table.

In another case, I need only select employee.name, and employee_product.product_name

Is there an alternative to map the data without creating POJO for every case?

答案1

得分: 3

创建一个将两个表合并为一个POJO的类,如下所示:

public class Employee {
    private int id_employee;
    private String name;
    private int id_employee_product;
    private String product_name;

    //getter和setter方法  
    //不要创建构造函数,这是一个实体类
}

然后,使用BeanPropertyRowMapper 文档链接 编写您的仓库方法,如下所示:

public List<Employee> fetchEmployeeProduct() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate("Your_DataSource");
    StringBuilder query = new StringBuilder();
    query.append("Your Query");
    List<Employee> employeeProductList =
        jdbcTemplate.query(query.toString(), new BeanPropertyRowMapper<Employee>(Employee.class));
    return employeeProductList;
}

请确保查询中的SELECT子句和Employee POJO的字段名相同。

执行查询后,它将自动映射到POJO,您不需要编写自定义映射器,BeanPropertyRowMapper会负责映射。

英文:

Create a one POJO combining two tables like this

public class Employee {
    private int id_employee;
    private String name;
    private int id_employee_product.
    private String product_name
    
    //getter and setters  
    //Don&#39;t create a constructor its Entiry
}

Now by using a BeanPropertyRowMapper Doc Link write your repository like

public List&lt;Employee&gt; fetchEmployeeProduct(){
	JdbcTemplate jdbcTemplate = new JdbcTemplate(&quot;Your_DataSource&quot;);
	StringBuilder query = new StringBuilder();
	query.append(&quot;Your Query&quot;);
	List&lt;Employee&gt; employeeProductList = 
		jdbcTemplate.query(query.toString(), new BeanPropertyRowMapper&lt;Employee&gt;(Employee.class));
}

Make sure SELECT clause in the query and Employee POJO's filed name is same.

Once if you execute your query it will automatically map to POJO. You no need to write a custom mapper BeanPropertyRowMapperwill take care of mapping.

huangapple
  • 本文由 发表于 2020年5月3日 16:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61571357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定