1. 给子查询传递一个值
@select({“select * from user where username = #{username}”}) ①
@results({
@result(
property = “id”, column = “id” ②
),
@result(
property = “rolelist”,column=“id”, ③
many = @many(select = “com.x.dt.mapper.usermapper.selectbyusernameroleid”)
)})
list selectbyname(string username);
@select({"select id from role where id in(select role_id from user_role where user_id=#{userid})"})
list<role> selectbyusernameroleid(@param("userid") string userid);
三个表
用户表: user
角色表:role
用户角色关联表:user_role
- ①主查询,查询得到结果;
- 将主查询结果里面的id字段赋给③;
- ③里面的id为子查询的条件,③的id与①主查询里面的数据库字段相对应;
- selectbyusernameroleid通过③的coulmn为查询条件。
2. 给子查询传递多个值

@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
到此这篇关于mybatis many=@many的传值问题解决的文章就介绍到这了,更多相关mybatis many=@many传值内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论