需求:查询出指定性别和用户角色列表下的用户列表信息
实际上:mybatis在入参的时候,都是将参数封装成为map集合进行入参的,不管你是单参数入参,还是多参数入参,都是可以封装成map集合的,这是无可非议的。
/**
* 需求:查询出指定性别和用户角色列表下的用户列表信息
* @param roleids
* @return
*/
public List<User> getUserListByGender_UserRoleids(Map<String,Object> conditionMap);
<select id="getUserListByGender_UserRoleids" resultMap="userListArray" >
select * from smbms_user where 1=1 and gender=#{gender} and userRole in
<foreach collection="roleIDS" item="aaa" open="(" separator="," close=")">
#{aaa}
</foreach>
</select>
<resultMap type="User" id="userListArray">
<id property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
</resultMap>
//多参数的时候,传入map集合
@Test
public void testGetUserByForeach_Gender_Roleids(){
SqlSession sqlSession = null;
Map conditionMap=new HashMap<String, Object>();
List<Integer> userList = new ArrayList<Integer>();
userList.add(2);
userList.add(3);
conditionMap.put("roleIDS", userList);
conditionMap.put("gender", 1);
List<User> userListShow=new ArrayList<User>();
try {
sqlSession = MyBatisUtil.createSqlSession();
userListShow = sqlSession.getMapper(UserMapper.class).getUserListByGender_UserRoleids(conditionMap); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user: userListShow){
logger.debug("testGetUserByForeach_Gender_Roleids UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
} }
运行结果:
[DEBUG] 2019-12-22 15:49:46,403 cn.smbms.dao.user.UserMapper.getUserListByGender_UserRoleids - ==> Preparing: select * from smbms_user where 1=1 and gender=? and userRole in ( ? , ? )
[DEBUG] 2019-12-22 15:49:46,442 cn.smbms.dao.user.UserMapper.getUserListByGender_UserRoleids - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
[DEBUG] 2019-12-22 15:49:46,462 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@40f892a4]
[DEBUG] 2019-12-22 15:49:46,463 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@40f892a4]
[DEBUG] 2019-12-22 15:49:46,463 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1090032292 to pool.
[DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhanghua and UserName: 张华and userRole:3
[DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhaoyan and UserName: 赵燕and userRole:3
[DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhangchen and UserName: 张晨and userRole:3
[DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhaomin and UserName: 赵敏and userRole:2