Mybatis中传参数的方法
1.使用注解绑定,mapper.xml 对应方法 不需要指定 parameterType,(推荐使用注解绑定方式)
方法接口:
List selectByExample(@Param("example") CalculateIdeacommissionsum example,@Param("roleNameList") List roleNameList);
方法对应的Mapper.xml
select
id, calculateYear, calculateMonth, userId, userCode, userName, userStatus, companyName,
companyId, curDeptId, curDeptName, roleName from Calculate_IdeaCommissionSum
where calculateYear=#{example.calculateYear,jdbcType=SMALLINT}
and calculateMonth=#{example.calculateMonth,jdbcType=SMALLINT}
and deleteFlag=#{example.deleteFlag,jdbcType=TINYINT}
#{rolename,jdbcType=VARCHAR}
order by userStatus
2.如果接口参数没有使用注解绑定,mapper.xml 对应方法 需要指定对应的参数类型。
List selectByExample(CalculateIdeacommissionsum example);
方法对应的Mapper.xml
select
id, calculateYear, calculateMonth, userId, userCode, userName, userStatus, companyName,
companyId, curDeptId, curDeptName, roleName from Calculate_IdeaCommissionSum
where calculateYear=#{calculateYear,jdbcType=SMALLINT}
and calculateMonth=#{calculateMonth,jdbcType=SMALLINT}
order by userStatus
3. parameterType 也可以使用Map存放参数进行查询
接口方法:
List selectByParam(Map map);
接口方法对应的Mapper.xml 文件方法:
select * from Base_EmpInfo
where deleteFlag=0
and userName=#{userName,jdbcType=VARCHAR}
4.mybatis 遍历循环
collection标识我们程序传值过来的集合
open表示我们遍历的集合以什么字符开始
close表示我们遍历的集合以什么字符结尾
item是给我们集合遍历取一个变量
separator 表示的是分隔符,将我们集合中遍历出来的数据用","分隔开。
#{rolename,jdbcType=VARCHAR}
sql如下:
select * from Calculate_IdeaCommissionSum where calculateYear=2019 and calculateMonth=2 and roleName in ('副总经理','总监','经纪人');
将roleName的多个条件用关系转化为 roleName in (roleName1,roleName2,roleName3...) in中用foreach循环