1.创建fliter.LoginFiliter类,实现filiter过滤器,使其在未登录时界面不能随便跳转,只能锁定在登录界面
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
String uri = request.getRequestURI();
System.out.println("uri-------"+uri);
System.out.println("uri.indexof="+uri.indexOf("login.do"));
if (user==null && uri.indexOf("login.do")==-1){
response.sendRedirect(request.getContextPath()+"../login.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() { }
2.实现批量删除用户功能
<delete id="deleteAll" parameterType="List">
delete from tb_user where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
@Override
public void deleteAll(List<Integer> ids) {
System.out.println("service in"+ids);
userDao.deleteAll(ids);
System.out.println("service end");
}
3.创建Role实体类,定义相关属性
4.建立寻找角色和寻找角色ID的接口并实现
@Override
public List<Integer> findRoleId(int userId) {
return roleDao.findRoleIdByUserId(userId);
}
@Override
public List<Role> findRoleByUserId(int id) {
return roleDao.findRoleByUserId(id);
}
<select id="findRoleIdByUserId" parameterType="int" resultType="int">
select roleId from user_role where userId=#{userId}
</select>
<select id="findRoleByUserId" parameterType="int" resultType="role">
select * from tb_role where id not in (select roleId from user_role where userId=#{id})
</select>
5.实现添加角色功能
<insert id="addRole" parameterType="userRole">
insert into user_role (userId,roleId) values (#{userId},#{roleId})
</insert>
@Override
public void add(List<Integer> ids, String userId) {
for (int roleId:ids){
UserRole userRole = new UserRole();
userRole.setUserId(Integer.parseInt(userId));
userRole.setRoleId(roleId);
roleDao.addRole(userRole);
}
}