ssm实现角色管理以及用户登陆
package com. zhongruan. bean;
import java. util. List;
public class PageInfo < T > {
private List< T > list;
private int totalPage;
private int size;
private int totalCount;
private int currentPage;
public List< T > getList ( ) {
return list;
}
public void setList ( List< T > list) {
this . list = list;
}
public int getTotalPage ( ) {
return totalPage;
}
public void setTotalPage ( int totalPage) {
this . totalPage = totalPage;
}
public int getSize ( ) {
return size;
}
public void setSize ( int size) {
this . size = size;
}
public int getTotalCount ( ) {
return totalCount;
}
public void setTotalCount ( int totalCount) {
this . totalCount = totalCount;
}
public int getCurrentPage ( ) {
return currentPage;
}
public void setCurrentPage ( int currentPage) {
this . currentPage = currentPage;
}
@Override
public String toString ( ) {
return "PageInfo{" +
"list=" + list +
", totalPage=" + totalPage +
", size=" + size +
", totalCount=" + totalCount +
", currentPage=" + currentPage +
'}' ;
}
}
package com. zhongruan. bean;
public class Role {
private int id;
private String rolename;
private String roledesc;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getRolename ( ) {
return rolename;
}
public void setRolename ( String rolename) {
this . rolename = rolename;
}
public String getRoledesc ( ) {
return roledesc;
}
public void setRoledesc ( String roledesc) {
this . roledesc = roledesc;
}
@Override
public String toString ( ) {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", roledesc='" + roledesc + '\'' +
'}' ;
}
}
package com. zhongruan. bean;
public class User {
public User ( ) {
}
public User ( int id, String username, String password) {
this . id = id;
this . username = username;
this . password = password;
}
public User ( String username, String password) {
this . username = username;
this . password = password;
}
private int id;
private String username;
private String password;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}' ;
}
}
package com. zhongruan. bean;
public class UserRole {
public UserRole ( ) {
}
public UserRole ( int id, int userId, int roleId) {
this . id = id;
this . userId = userId;
this . roleId = roleId;
}
private int id;
private int userId;
private int roleId;
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getUserId ( ) {
return userId;
}
public void setUserId ( int userId) {
this . userId = userId;
}
public int getRoleId ( ) {
return roleId;
}
public void setRoleId ( int roleId) {
this . roleId = roleId;
}
@Override
public String toString ( ) {
return "Role{" +
"id=" + id +
", userId=" + userId +
", roleId=" + roleId +
'}' ;
}
}
package com. zhongruan. controller;
import com. zhongruan. bean. PageInfo;
import com. zhongruan. bean. Role;
import com. zhongruan. bean. UserRole;
import com. zhongruan. bean. User;
import com. zhongruan. service. IRoleService;
import com. zhongruan. service. IUserService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestParam;
import org. springframework. web. bind. annotation. ResponseBody;
import org. springframework. web. servlet. ModelAndView;
import javax. servlet. http. HttpSession;
import java. util. ArrayList;
import java. util. List;
@Controller
@RequestMapping ( "/user" )
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private IRoleService roleService;
@RequestMapping ( "/login.do" )
public ModelAndView login ( User user, HttpSession session) {
int id = userService. login ( user. getUsername ( ) , user. getPassword ( ) ) ;
ModelAndView modelAndView= new ModelAndView ( ) ;
if ( id!= - 1 ) {
List< Integer> roleIds = roleService. findRoleId ( id) ;
session. setAttribute ( "roleIds" , roleIds) ;
session. setAttribute ( "user" , user) ;
modelAndView. setViewName ( "main" ) ;
} else {
modelAndView. setViewName ( "../failer" ) ;
}
return modelAndView;
}
@RequestMapping ( "/findAll.do" )
public ModelAndView findAll ( @RequestParam ( defaultValue = "1" ) int currentPage, String username, @RequestParam ( defaultValue = "0" ) int type, HttpSession session) {
if ( type== 1 ) {
session. setAttribute ( "searchname" , username) ;
} else {
username= ( String) session. getAttribute ( "searchname" ) ;
}
PageInfo< User> pageInfo= userService. findAll ( currentPage, username) ;
ModelAndView modelAndView= new ModelAndView ( ) ;
modelAndView. addObject ( "pageInfo" , pageInfo) ;
modelAndView. setViewName ( "user-list" ) ;
return modelAndView;
}
@RequestMapping ( "/deleteById.do" )
public String delete ( int id) {
userService. deleteById ( id) ;
return "redirect:findAll.do" ;
}
@RequestMapping ( "/add.do" )
public String add ( User user) {
userService. add ( user) ;
return "redirect:findAll.do" ;
}
@RequestMapping ( "toUpdate.do" )
public ModelAndView toUpdate ( int id) {
User user= userService. selectUserById ( id) ;
ModelAndView modelAndView= new ModelAndView ( ) ;
modelAndView. setViewName ( "user-update" ) ;
modelAndView. addObject ( "user" , user) ;
return modelAndView;
}
@RequestMapping ( "/update.do" )
public String update ( User user) {
userService. update ( user) ;
return "redirect:findAll.do" ;
}
@RequestMapping ( "deleteAll.do" )
public String deleteAll ( String userList) {
String[ ] strs = userList. split ( "," ) ;
List< Integer> ids= new ArrayList < > ( ) ;
for ( String s: strs) {
ids. add ( Integer. parseInt ( s) ) ;
}
userService. deleteAll ( ids) ;
return "redirect:findAll.do" ;
}
@RequestMapping ( "toAddRole.do" )
public ModelAndView toAddRole ( int id) {
List< Role> roleList= roleService. findRoleByUserId ( id) ;
ModelAndView mv= new ModelAndView ( ) ;
mv. addObject ( "roles" , roleList) ;
mv. addObject ( "id" , id) ;
mv. setViewName ( "user-role-add" ) ;
return mv;
}
@RequestMapping ( "addRole.do" )
@ResponseBody
public String add ( String roleList, String userId) {
String[ ] strs = roleList. split ( "," ) ;
List< Integer> ids= new ArrayList < > ( ) ;
for ( String s: strs) {
ids. add ( Integer. parseInt ( s) ) ;
}
roleService. add ( ids, userId) ;
return "" ;
}
package com. zhongruan. dao;
import com. zhongruan. bean. Role;
import com. zhongruan. bean. UserRole;
import java. util. List;
public interface RoleDao {
List< Integer> findRoleIdByUserId ( int userId) ;
List< Role> findRoleByUserId ( int id) ;
void addRole ( UserRole userRole) ;
}
package com. zhongruan. dao;
import com. zhongruan. bean. User;
import org. apache. ibatis. annotations. Param;
import java. util. List;
public interface UserDao {
User findUserByUserName ( String username) ;
List< User> findAll ( @Param ( "start" ) int start, @Param ( "username" ) String username) ;
void deleteById ( int id) ;
void add ( User user) ;
User selectById ( int id) ;
void update ( User user) ;
int getTotalCount ( @Param ( "username" ) String username) ;
void deleteAll ( @Param ( "ids" ) List< Integer> ids) ;
}
package com. zhongruan. filter;
import com. zhongruan. bean. User;
import javax. servlet. * ;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import javax. servlet. http. HttpSession;
import java. io. IOException;
public class LoginFilter implements Filter {
@Override
public void init ( FilterConfig filterConfig) throws ServletException {
}
@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 ( ) ;
if ( user== null && uri. indexOf ( "login.do" ) == - 1 ) {
response. sendRedirect ( request. getContextPath ( ) + "../login.jsp" ) ;
} else {
filterChain. doFilter ( request, response) ;
}
}
@Override
public void destroy ( ) {
}
}
package com. zhongruan;
public class Test {
public static void main ( String[ ] args) {
String name = Thread. currentThread ( ) . getName ( ) ;
System. out. println ( name) ;
Thread1 thread1= new Thread1 ( ) ;
thread1. setName ( "小黄" ) ;
thread1. start ( ) ;
Thread1 thread2= new Thread1 ( ) ;
thread2. setName ( "xiaohua" ) ;
thread2. start ( ) ;
Thread2 t2= new Thread2 ( ) ;
Thread t3= new Thread ( t2) ;
t3. start ( ) ;
}
}
package com. zhongruan;
public class Thread1 extends Thread {
@Override
public void run ( ) {
for ( int i= 0 ; i< 100 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "----" + i) ;
}
}
}
package com. zhongruan;
public class Thread2 implements Runnable {
@Override
public void run ( ) {
for ( int i= 0 ; i< 100 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "----" + i) ;
}
}
}