使用JPA和JDBC Template在Northwind数据库上选择

时间:2018-01-16 02:44:00

标签: spring-boot spring-jdbc jdbctemplate

我想在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;  

我对此操作有两个问题:

  • 我为表类别和bellow
  • 等产品创建了POJO

表格产品

@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;

表类别

@Entity
public class Categories {
  @Id
  private Long categoryid;
  private String categoryname;
  private String description;
  private String picture;

现在我不知道如何为这些表编写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;
    };
  • 第二个问题是我不知道表格产品中列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);
        }

表类别

@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;

表格产品

   @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;

CategoryName

1 个答案:

答案 0 :(得分:0)

  

现在我不知道如何为这个表编写rowmapper(请找到   下面????)

您必须像这样设置一个Object:

products.setCategoryid(new Categories(rs.getString("a.categoryname"));

我假设你有一个构造函数Categories(String categoryname)

注意:如果您想获取更多信息,还必须更改查询和构造函数。

  

第二个问题是,我不知道是否有注释   表产品中的列categoryid是否正确?

我认为没有必要使用@JoinColumn(name = "categories", referencedColumnName = "categoryid")只使用:

@ManyToOne
private Categories categoryid;

在您的类别实体中添加以下内容:

@OneToMany(mappedBy="categoryid")
List<Products> products;