SSM框架的增删改查
package com.zhong.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.zhong.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.zhong.bean;
public class Role {
private int Id;
private String rolename;
private String roledesc;
public int getId() {
return Id;
}
public void setId(int id) {
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.zhong.controller;
import com.zhong.Service.RoleService;
import com.zhong.bean.PageInfo;
import com.zhong.bean.Role;
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.servlet.ModelAndView;
import com.zhong.Service.UserService;
import com.zhong.bean.User;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController
{
@Autowired
private UserService userService;
@Autowired
private RoleService 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.jsp");
}
return modelAndView;
}
@RequestMapping(value="/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 delte(int id)
{
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/addUser.do")
public String addUser(User user)
{
userService.addUser(user);
return "redirect:findAll.do";
}
@RequestMapping("/findOne.do")
public ModelAndView findOne(int id)
{
User user=userService.findOne(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("user-update");
return modelAndView;
}
@RequestMapping("/update.do")
public String updateById(User user)
{
userService.updateUser(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.findRole(id);
ModelAndView mv=new ModelAndView();
mv.addObject("roleList",roleList);
mv.addObject("userId",id);
mv.setViewName("user-role-add");
return mv;
}
@RequestMapping("/addRole.do")
public ModelAndView addRole(int userId,String roleId)
{
String[] strs=roleId.split(",");
List<Integer> ids=new ArrayList<>();
for (String s:strs)
{
ids.add(Integer.parseInt(s));
}
for (int d:ids)
{
roleService.addRole(userId,d);
System.out.println(d);
System.out.println(userId);
}
ModelAndView mv=new ModelAndView();
mv.setViewName("user-role-add");
return mv;
}
}
package com.zhong.Dao;
import com.zhong.bean.PageInfo;
import com.zhong.bean.User;
import java.util.List;
public interface UserDao
{
User findUser(String username);
public List<User> findAll(int start);
void deleteById(int id);
void addUser(User user);
void updateUser(User user);
User findOne(int id);
public int getCount();
List<User> findLike(String username);
}
package com.zhong.Dao;
import com.zhong.bean.Role;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface RoleDao
{
List<Integer> findRoleIdByUserId(int userId);
List<Role> findRole(int id);
void addRole(@Param("userId")int userId,@Param("roleId")int roleId);
}
package com.zhong.filter;
import com.zhong.bean.User;
import javax.servlet.*;
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.zhong.Service.Imp;
import com.zhong.Dao.RoleDao;
import com.zhong.Service.RoleService;
import com.zhong.bean.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceImp implements RoleService
{
@Autowired
private RoleDao roleDao;
@Override
public List<Integer> findRoleId(int userId) {
return roleDao.findRoleIdByUserId(userId);
}
@Override
public List<Role> findRole(int id)
{
return roleDao.findRole(id);
}
public void addRole(int userId,int roleId)
{
roleDao.addRole(userId,roleId);
}
}
package com.zhong.Service.Imp;
import com.zhong.bean.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhong.Dao.UserDao;
import com.zhong.Service.UserService;
import com.zhong.bean.User;
import java.util.List;
@Service
public class UserServiceImp implements UserService
{
@Autowired
private UserDao userDao;
@Override
public int login(String username,String password) {
User user=userDao.findUser(username);
if (user!=null && user.getPassword().equals(password))
{
return user.getId();
}
else
{
return -1;
}
}
@Override
public PageInfo<User> findAll(int currentPage,String username)
{
PageInfo<User> pageInfo=new PageInfo<>();
pageInfo.setSize(5);
int ts=userDao.getCount(username);
pageInfo.setTotalCount(ts);
int tp=(int)Math.ceil(ts/5.0);
System.out.println(currentPage);
pageInfo.setTotalPage(tp);
if (currentPage<1)
{
pageInfo.setCurrentPage(1);
}
else if (currentPage>tp)
{
pageInfo.setCurrentPage(tp);
}
else
{
pageInfo.setCurrentPage(currentPage);
}
int start=(pageInfo.getCurrentPage()-1)*5;
List<User> users=userDao.findAll(start,username);
pageInfo.setList(users);
return pageInfo;
}
@Override
public void deleteById(int id)
{
userDao.deleteById(id);
}
@Override
public void addUser(User user)
{
userDao.addUser(user);
}
public void updateUser(User user)
{
userDao.updateUser(user);
}
public User findOne(int id)
{
return userDao.findOne(id);
}
public List<User> findLike(String username)
{
List<User> user=userDao.findLike(username);
return user;
}
@Override
public void deleteAll(List<Integer> ids)
{
userDao.deleteAll(ids);
}
}
```java
package com.zhong.Service.Imp;
import com.zhong.bean.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhong.Dao.UserDao;
import com.zhong.Service.UserService;
import com.zhong.bean.User;
import java.util.List;
@Service
public class UserServiceImp implements UserService
{
@Autowired
private UserDao userDao;
@Override
public boolean login(String username,String password) {
User user=userDao.findUser(username);
if (user!=null && user.getPassword().equals(password))
{
return true;
}
else
{
return false;
}
}
@Override
public PageInfo<User> findAll(int currentPage)
{
PageInfo<User> pageInfo=new PageInfo<>();
pageInfo.setSize(5);
int ts=userDao.getCount();
pageInfo.setTotalCount(ts);
int tp=(int)Math.ceil(ts/5.0);
pageInfo.setTotalPage(tp);
if (currentPage<2)
{
pageInfo.setCurrentPage(2);
}
else if (currentPage>tp-1)
{
System.out.println(tp);
System.out.println(currentPage);
pageInfo.setCurrentPage(tp-1);
}
else
{
pageInfo.setCurrentPage(currentPage);
}
int start=(currentPage-1)*5;
List<User> users=userDao.findAll(start);
pageInfo.setList(users);
return pageInfo;
}
@Override
public void deleteById(int id)
{
userDao.deleteById(id);
}
@Override
public void addUser(User user)
{
userDao.addUser(user);
}
public void updateUser(User user)
{
userDao.updateUser(user);
}
public User findOne(int id)
{
return userDao.findOne(id);
}
public List<User> findLike(String username)
{
List<User> user=userDao.findLike(username);
return user;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhong.Dao.UserDao" >
<select id="findUser" parameterType="String" resultType="user">
select * from user where username=#{username}
</select>
<select id="findAll" resultType="user" parameterType="int">
select * from user limit #{start},5
</select>
<delete id="deleteById" parameterType="int" >
delete from user where id=#{id}
</delete>
<insert id="addUser" parameterType="user">
insert into user(username,password) values(#{username},#{password})
</insert>
<select id="findOne" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
<update id="updateUser" parameterType="user" >
update user set username=#{username},password=#{password} where id=#{id}
</update>
<select id="getCount" resultType="int">
select count(*) from user
</select>
<select id="findLike" resultType="user" parameterType="String">
select * from user where username LIKE "%"#{username}"%"
</select>
</mapper>
```java
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhong.Dao.RoleDao" >
<select id="findRoleIdByUserId" parameterType="int" resultType="int">
select roleId from user_role where userId=#{userId};
</select>
<select id="findRole" parameterType="int" resultType="Role">
select * from tb_role WHERE id NOT IN (SELECT roleId FROM user_role WHERE userId=#{id})
</select>
<insert id="addRole" parameterType="int">
insert into user_role(userId,roleid) values(#{userId},#{roleId})
</insert>
</mapper>