Mybatis 示例 foreach 以Map 传递参数 (SpringMvc+Spring+Mybatis)

Contrller

 
  

@RequestMapping( "/userList") @ResponseBody
public List getUserList(String name,String role,HttpServletRequest request){
List roleList= new ArrayList();
if(role.length()> 0){
String r[]=role.split( ",");
for (String ro:r ) {
roleList.add(ro);
}
}
Map paramMap= new HashMap();
paramMap.put( "name",name);
if(roleList.isEmpty()){
paramMap.put( "roles", null);
} else{
paramMap.put( "roles",roleList);
}
List list = userService.getAll3(paramMap);
System. out.println(list.toArray().toString());
return list;
}
Service
@Service("userService")
public class UserServiceImpl implements UserServiceI {

   private UserMapper userMapper;

   public UserMapper getUserMapper() {
      return userMapper;
   }

   @Autowired
   public void setUserMapper(UserMapper userMapper) {
      this.userMapper = userMapper;
   }

   public User getUserById(String id) {
      return userMapper.selectByPrimaryKey(id);
   }
   
   public boolean userLogin(User user) {
      User  u =userMapper.userLogin(user);
      if (u !=null){
         return true;
      }else {
         return false;
      }
   }

   public Role getUserRole(User user) {
      List list =userMapper.getUserRole(user);
      if(!list.isEmpty()){
         return (Role) list.get(0);
      }else{
         return null;
      }
   }

   public List  getRoleMenu(Role role) {
      return userMapper.getRoleMenu(role);
   }
   public List getAll() {
      return userMapper.getAll();
   }

   public List getAll2() {
      return userMapper.getAll2();
   }

   public List getAll3(Map paramMap) {
      return userMapper.getAll3(paramMap);
   }


}

Dao (UserMapper.java)
List getAll3(Map paramMap);
mapper.xml

 
  
< select id ="getAll3" resultMap ="userResultMap3" parameterType ="java.util.Map" > SELECT tuser.ID, tuser.CREATDATETIME, tuser.MODIFYDATETIME, tuser.`NAME`, tuser.PWD, tuser.CREATE_TIME, tuser.UPDATE_TIME, trole.ID role_id, trole.TEXT role_text FROM tuser JOIN tuser_trole ON tuser.ID = tuser_trole.USER_ID JOIN trole ON tuser_trole.ROLE_ID = trole.ID where 1=1 < if test ="name != null and name != ''" > and tuser.name like "%"#{name,jdbcType=VARCHAR}"%" if > < if test ="roles != null and roles !='' " > and trole.ID in < foreach collection ="roles" index ="index" item ="roleList" open ="(" separator ="," close =")" > #{roleList} foreach > if > select >
其中 roles 为 参数map集合中的"KEY" ,item ="roleList" 中roleList 为参数map 集合中roles(key) 对应的Value
#{roleList} 为每条显示的数据

你可能感兴趣的:(笔记)