@many一对多---------解决一向多(主查询传递给子查询参数为两个)
方法运用场景
主函数传递参数给子查询进行条件查找
方法简述
将变量参数转化为动态列,通过常用列传值方法传入
主查询
@Mapper---------------主查询和子查询都要打上注解Mapper,才可以映射过去
@Select("<script>"
+ "SELECT user_id "
+ "CASE "
+ "WHEN (#{userName} = '' or #{userName} is null) "
+ "THEN NULL "
+ "ELSE #{userName} END AS user_name"---------------------核心语句,注意这边是=不是==
+ "FROM sys_user "
+ "</script>")
@Results({
@Result(column = "{userName=user_name,userId=user_id}", property = "userList", javaType = List.class,
many = @Many(select = "com.dao.xxxDao.searchXxxByName"))--------------------------property是查询返回映射到DTO里面的变量,在column里面放置传到查多语句中的参数userName,userId
})
List<XxxDto> searchXxx(@Param("userName") String userName);---------传递过去的参数
子查询
@Mapper
@Select("<script>"
+ "SELECT * "
+ "FROM sys_detail "
+ "<if test=\"userId!=null \"> "
+ "AND user_id =#{userId} "
+ "</if> "
+ "<if test=\"userName!=null and userName!=\'\' \"> "
+ "AND user_name=#{userName} "
+ "</if> "
+ "</script>")
List<SysDetail> searchXxxByName(Map<String, String> param);------对应的参数一定要是Mapper