Select 在 Northwind 数据库上使用 JPA 和 JDBC 模板
Select on Northwind database using JPA and JDBC Template
我想像下面这样在 Northwind
数据库上执行 select 语句。
select distinct b.*, a.CategoryName
from Categories a
inner join Products b on a.CategoryID = b.CategoryID
where b.Discontinued = 'N'
order by b.ProductName;
关于这个操作我有两个问题:
- 我已经为 table 的类别和产品创建了 POJO,如下所示
Table 产品
@Entity
public class Products {
@Id
private Long productid;
private String productname;
private Long supplierid;
@ManyToOne
@JoinColumn(name = "categories", referencedColumnName = "categoryid")
private Categories categoryid;
private String quantityperunit;
private Double unitprice;
private Long unitsinstock;
private Long unitsonorder;
private Long reorderlevel;
private String discontinued;
Table 类别
@Entity
public class Categories {
@Id
private Long categoryid;
private String categoryname;
private String description;
private String picture;
现在我不知道如何为这个 table 编写 rowmapper(请在下面找到????)
private static final RowMapper<Products> productsRowMapper = (rs, rowNum) ->{
Products products = new Products();
products.setProductid(rs.getLong("ProductID"));
products.setProductname(rs.getString("ProductName"));
products.setSupplierid(rs.getLong("SupplierID"));
products.setCategoryid(rs.?????
products.setQuantityperunit(rs.getString("QuantityPerUnit"));
products.setUnitprice(rs.getDouble("UnitPrice"));
products.setUnitsinstock(rs.getLong("UnitsInStock"));
products.setUnitsonorder(rs.getLong("UnitsOnOrder"));
products.setReorderlevel(rs.getLong("ReorderLevel"));
products.setDiscontinued(rs.getString("Discontinued"));
return products;
};
- 第二个问题是不知道table商品中categoryid列的注解是否正确?
修正后
@Repository
public class JdbcProductsDao implements ProductsDao{
private final JdbcTemplate jdbcTemplate;
@Autowired
public JdbcProductsDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private static final RowMapper<Products> productsRowMapper = (rs, rowNum) ->{
Products products = new Products();
products.setProductid(rs.getLong("ProductID"));
products.setProductname(rs.getString("ProductName"));
products.setSupplierid(rs.getLong("SupplierID"));
products.setCategoryid(new Categories(rs.getString("CategoryName")));
products.setQuantityperunit(rs.getString("QuantityPerUnit"));
products.setUnitprice(rs.getDouble("UnitPrice"));
products.setUnitsinstock(rs.getLong("UnitsInStock"));
products.setUnitsonorder(rs.getLong("UnitsOnOrder"));
products.setReorderlevel(rs.getLong("ReorderLevel"));
products.setDiscontinued(rs.getString("Discontinued"));
return products;
};
public Products findByProductName(String productname) {
String sql = "SELECT * FROM products WHERE ProductName = ?";
return jdbcTemplate.queryForObject(sql, productsRowMapper, productname);
}
public List<Products> sortByProductName(){
String sql = "SELECT * FROM products order by ProductName asc";
return jdbcTemplate.query(sql, productsRowMapper);
}
Table 类别
@Entity
public class Categories {
@Id
private Long categoryid;
@Column(name = "CategoryName")
private String categoryname;
private String description;
private String picture;
@OneToMany(mappedBy="categoryid")
private List<Products> products;
Table 产品
@Entity
public class Products {
@Id
private Long productid;
@Column(name = "ProductName")
private String productname;
private Long supplierid;
@ManyToOne
private Categories categoryid;
private String quantityperunit;
private Double unitprice;
private Long unitsinstock;
private Long unitsonorder;
private Long reorderlevel;
private String discontinued;
Now I have no idea how to write rowmapper for this tables (please find
below ????)
您必须像这样设置一个对象:
products.setCategoryid(new Categories(rs.getString("a.categoryname"));
我假设你有一个构造函数 Categories(String categoryname)
注意:如果您想获得更多信息,您还必须更改查询和构造函数。
the second problem is that I don't know if the annotations on the
column categoryid in the table products are correct?
我觉得没必要用@JoinColumn(name = "categories", referencedColumnName = "categoryid")
就用:
@ManyToOne
private Categories categoryid;
在您的类别实体中添加:
@OneToMany(mappedBy="categoryid")
List<Products> products;
我想像下面这样在 Northwind
数据库上执行 select 语句。
select distinct b.*, a.CategoryName
from Categories a
inner join Products b on a.CategoryID = b.CategoryID
where b.Discontinued = 'N'
order by b.ProductName;
关于这个操作我有两个问题:
- 我已经为 table 的类别和产品创建了 POJO,如下所示
Table 产品
@Entity
public class Products {
@Id
private Long productid;
private String productname;
private Long supplierid;
@ManyToOne
@JoinColumn(name = "categories", referencedColumnName = "categoryid")
private Categories categoryid;
private String quantityperunit;
private Double unitprice;
private Long unitsinstock;
private Long unitsonorder;
private Long reorderlevel;
private String discontinued;
Table 类别
@Entity
public class Categories {
@Id
private Long categoryid;
private String categoryname;
private String description;
private String picture;
现在我不知道如何为这个 table 编写 rowmapper(请在下面找到????)
private static final RowMapper<Products> productsRowMapper = (rs, rowNum) ->{
Products products = new Products();
products.setProductid(rs.getLong("ProductID"));
products.setProductname(rs.getString("ProductName"));
products.setSupplierid(rs.getLong("SupplierID"));
products.setCategoryid(rs.?????
products.setQuantityperunit(rs.getString("QuantityPerUnit"));
products.setUnitprice(rs.getDouble("UnitPrice"));
products.setUnitsinstock(rs.getLong("UnitsInStock"));
products.setUnitsonorder(rs.getLong("UnitsOnOrder"));
products.setReorderlevel(rs.getLong("ReorderLevel"));
products.setDiscontinued(rs.getString("Discontinued"));
return products;
};
- 第二个问题是不知道table商品中categoryid列的注解是否正确?
修正后
@Repository
public class JdbcProductsDao implements ProductsDao{
private final JdbcTemplate jdbcTemplate;
@Autowired
public JdbcProductsDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private static final RowMapper<Products> productsRowMapper = (rs, rowNum) ->{
Products products = new Products();
products.setProductid(rs.getLong("ProductID"));
products.setProductname(rs.getString("ProductName"));
products.setSupplierid(rs.getLong("SupplierID"));
products.setCategoryid(new Categories(rs.getString("CategoryName")));
products.setQuantityperunit(rs.getString("QuantityPerUnit"));
products.setUnitprice(rs.getDouble("UnitPrice"));
products.setUnitsinstock(rs.getLong("UnitsInStock"));
products.setUnitsonorder(rs.getLong("UnitsOnOrder"));
products.setReorderlevel(rs.getLong("ReorderLevel"));
products.setDiscontinued(rs.getString("Discontinued"));
return products;
};
public Products findByProductName(String productname) {
String sql = "SELECT * FROM products WHERE ProductName = ?";
return jdbcTemplate.queryForObject(sql, productsRowMapper, productname);
}
public List<Products> sortByProductName(){
String sql = "SELECT * FROM products order by ProductName asc";
return jdbcTemplate.query(sql, productsRowMapper);
}
Table 类别
@Entity
public class Categories {
@Id
private Long categoryid;
@Column(name = "CategoryName")
private String categoryname;
private String description;
private String picture;
@OneToMany(mappedBy="categoryid")
private List<Products> products;
Table 产品
@Entity
public class Products {
@Id
private Long productid;
@Column(name = "ProductName")
private String productname;
private Long supplierid;
@ManyToOne
private Categories categoryid;
private String quantityperunit;
private Double unitprice;
private Long unitsinstock;
private Long unitsonorder;
private Long reorderlevel;
private String discontinued;
Now I have no idea how to write rowmapper for this tables (please find below ????)
您必须像这样设置一个对象:
products.setCategoryid(new Categories(rs.getString("a.categoryname"));
我假设你有一个构造函数 Categories(String categoryname)
注意:如果您想获得更多信息,您还必须更改查询和构造函数。
the second problem is that I don't know if the annotations on the column categoryid in the table products are correct?
我觉得没必要用@JoinColumn(name = "categories", referencedColumnName = "categoryid")
就用:
@ManyToOne
private Categories categoryid;
在您的类别实体中添加:
@OneToMany(mappedBy="categoryid")
List<Products> products;