question:
Assuming that a method with a query parameter x is passed in, and this parameter is also required in the subquery,
how to bring this parameter into the subquery b
General one-to-many query method:
/**
* 按User表中platform查询User
*/
@Select("select * from user where pid = #{id}")
List<User> findUsers(int id);
/**
* 一对多查询
*/
@Select("select * from platform where 1 = 1")
@Results({
/*@Result(property = "id" , column = "id"),*/
@Result(property = "users",
/*javaType = List.class,*/
//对platform表中id属性进行一对多查询
column = "id",
many = @Many(select = "mybatis.mapper.HelloMapper.findUsers")
)
})
List<Platform> getPlatforms();
- findUsers needs to pass in a parameter -> that is, the id in the platform table can be automatically brought in for one-to-many query
- @Result中column = "id"
- Indicates passing each value of the id column in the parent query to the subquery for one-to-many query
solution
- Table shoppingcart: The shopping cart mainly includes the merchant id field, product id and information, customer id, etc.
- In order for a customer to display the merchant in the shopping cart first, and then display the merchandise in the merchant, a customer id needs to be passed in
- Querying merchants requires customer id as the query condition, and querying products requires two conditions: merchant id and customer id
- If the above method is used, (there is no customer id in the merchant table), the customer parameter cannot be passed
- (Assuming that only one merchant id parameter is passed, both parameters of the subquery will be set to the merchant id value for query)
Idea: Put the customer id into the query and save it, and give him an alias
- After this, the customer id can be passed to the subquery
/**
* 按顾客id查询其购物车(商家->商品 一对多查询)
* @param consumerId 顾客id
* @return 购物车商品列表
*/
@Select("select distinct saler.id,saler.shopname,#{consumerId} as consumerId from shoppingcart \n" +
"join saler on saler.id = shoppingcart.salerId \n" +
"where consumerId = #{consumerId}")
@Results(
@Result(
property = "goods",
column = "{salerId = id,consumerId = consumerId}",
many = @Many(select = "cn.datacharm.springbootvuecli.dao.CartMapper.findGoodsBySalerId")
)
)
List<Shop> findCartById(Integer consumerId);
@Select("select \n" +
"sid,consumerId,productName,price,photo,\n" +
"shoppingcart.salerId,\n" +
"shoppingcart.productId,\n" +
"shoppingcart.amount\n" +
"from shoppingcart\n" +
"join saler_inventory on shoppingcart.salerId = saler_inventory.salerId\n" +
"and shoppingcart.productId = saler_inventory.productId\n" +
"where shoppingcart.salerId = #{salerId}\n"+
"and consumerId = #{consumerId}" )
List<Goods> findGoodsBySalerId(Integer salerId,Integer consumerId);
- @Result中column = "{salerId = id,consumerId = consumerId}"
- Indicates that the id column and consumerId column are taken out
- The id column value uses saleserId, and the consumerId column uses consumerId to represent (similar to aliases, corresponding to subquery parameters)
- Then make a subquery with these two parameters