current position:Home>Association query of Dao layer database (one class is used as the attribute query of another class)

Association query of Dao layer database (one class is used as the attribute query of another class)

2022-01-27 02:56:26 llxxqq5

Example : There is a shopping cart here , There are goods in the shopping cart id And commodity object properties

public List<Cart> selectCarts(long uid) {
    // In the query , Use the association table to query , Product objects can be directly injected into the shopping cart 
    String sql = "select * from cart c inner join product p on c.p_id=p.p_id where c.u_id=?";
    try {
        List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), uid);
        if(list != null){
            List<Cart> carts = new ArrayList<>();
            for(Map<String, Object> map : list){
                // take map Value injection into Cart object , And product In the object ,cart Object contains product object 
                Cart cart = new Cart();
                Product product = new Product();
                BeanUtils.populate(cart,map);
                BeanUtils.populate(product,map);
                cart.setProduct(product);
                carts.add(cart);
            }
            return carts;
        }
    } catch (SQLException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

The above codes are based on the of the shopping cart id Find shopping cart and product information , And injection Cart Object and the product In the object , Here, , Use MapListHandler, The object that processes the associated query results .( Use BeanUtils.populate Inject

copyright notice
author[llxxqq5],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270256242901.html

Random recommended