英文:
Map Custom JdbcTemplate query result in an Object
问题
我是你的中文翻译,以下是翻译好的部分:
如果我从我的Employee表中选择员工数据,我可以将它映射到一个POJO模型User,并在该模型中定义表的结构,就像这样:
这是从jdbcTemplate的映射示例:
这是从一张表中选择数据的明确示例。
我的问题是,如果我的数据是自定义查询,如何映射查询的数据?比如使用连接并选择那些表的自定义字段,我需要为每个查询都创建POJO吗?
有时我只需要选择employee.id_employee
和employee.name
字段,而在另一个控制器中,我需要从我的employee
表中选择employee.id_employee
。
在另一种情况下,我只需要选择employee.name
和employee_product.product_name
。是否有不需要为每种情况都创建POJO的替代方法?
英文:
I new in java and try to use spring framework. I have a question.
By example, I have table :
- employee (id_employee, name)
- 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't create a constructor its Entiry
}
Now by using a BeanPropertyRowMapper
Doc Link write your repository like
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));
}
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 BeanPropertyRowMapper
will take care of mapping.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论