ssm增删改查
LoginController
package com.web.controller;
import com.web.pojo.User;
import com.web.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class LoginController {
@Autowired
private UserService userService;
/**登录
* @param model
* @param uPwd
* @param uName
* @param session
* @return {@link String}
*/
@RequestMapping("/toLogin")
public String toLogin(Model model, String uPwd, String uName, HttpSession session){
User user1 = userService.login(uPwd, uName);
if (user1!=null){
List<User> list = userService.findAll();
//增删改查需要的参数
session.setAttribute("user1",user1);
//列表参数
model.addAttribute("list",list);
//登录成功跳转到首页
return "index";
}else {
//登录失败跳转回登录页面
return "cwlogin";
}
}
/**返回列表
* @param model
* @return {@link String}
*/
@RequestMapping("/flush")
public String flush(Model model){
List<User> list = userService.findAll();
model.addAttribute("list",list);
return "index";
}
/**删除功能
* @param uCode
* @return {@link String}
*/
@RequestMapping("/toDel")
public String toDel(int uCode){
int id = userService.delete(uCode);
if (id>0){
System.out.println("删除成功");
}
return "redirect:flush";
}
/**查看详情
* @param uCode
* @param model
* @return {@link String}
*/
@RequestMapping("/toLook")
public String toLook(int uCode,Model model){
User user = userService.findById(uCode);
model.addAttribute("user",user);
return "userOne";
}
/**添加页面
* @return {@link String}
*/
@RequestMapping("/toAdd")
public String toAdd(){
return "addUser";
}
/**实现添加功能
* @param user
* @return {@link String}
*/
@RequestMapping("/doAdd")
public String doAdd(User user){
int i = userService.add(user);
if (i>0){
System.out.println("添加成功");
}
return "redirect:flush";
}
/**修改页面
* @param uCode
* @param model
* @return {@link String}
*/
@RequestMapping("/toUpdate")
public String toUpdate(int uCode,Model model){
User user = userService.findById(uCode);
model.addAttribute("user",user);
return "update";
}
/**实现修改功能
* @param user
* @return {@link String}
*/
@RequestMapping("/doUpdate")
public String doUpdate(User user){
int i = userService.updateUser(user);
if (i>0){
System.out.println("修改成功");
}
return "redirect:flush";
}
}
LoginFilter
package com.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.pojo.User;
public class LoginFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
//通过过滤器进行登录过滤,不是进行合法用户名密码登录的,不可以跳转到下一页面,防止通过路径直接访问网页
HttpServletRequest requ=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
User userSession=(User)requ.getSession().getAttribute("userSession");
if(userSession==null){
res.sendRedirect("/tset1_war_exploded/");
}else{
arg2.doFilter(request, response);
}
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
UserMapper
package com.web.mapper;
import com.web.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
List<User> findAll();
User login(@Param("uPwd") String uPwd,@Param("uName") String uName);
User findById(int uCode);
int delete(int uCode);
int addUser(User user);
int updateUser(User user);
}
UserMapper.xml
<?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.web.mapper.UserMapper">
<select id="findAll" resultType="com.web.pojo.User">
select * from user
</select>
<select id="login" resultType="com.web.pojo.User">
select * from user where uPwd=#{uPwd} and uName=#{uName}
</select>
<select id="findById" resultType="com.web.pojo.User">
select * from user where uCode=#{uCode}
</select>
<delete id="delete" parameterType="int">
delete from user where uCode=#{uName}
</delete>
<insert id="addUser" parameterType="com.web.pojo.User">
insert into user(uName,uPwd,uType,uMemo) values(#{uName},#{uPwd},#{uType},#{uMemo});
</insert>
<!-- <update id="updateUser" parameterType="com.web.pojo.User">-->
<!-- update user set username=#{username},password=#{password},role=#{role},remark=#{remark} where id=#{id};-->
<!-- </update>-->
<update id="updateUser" parameterType="com.web.pojo.User">
update user set uName=#{uName},uPwd=#{uPwd},uType=#{uType},uMemo=#{uMemo} where uCode=#{uCode};
</update>
</mapper>
User
package com.web.pojo;
public class User {
private int uCode;
private String uName;
private String uPwd;
private int uType;
private String uMemo;
public User() {
}
public int getuCode() {
return uCode;
}
public void setuCode(int uCode) {
this.uCode = uCode;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuPwd() {
return uPwd;
}
public void setuPwd(String uPwd) {
this.uPwd = uPwd;
}
public int getuType() {
return uType;
}
public void setuType(int uType) {
this.uType = uType;
}
public String getuMemo() {
return uMemo;
}
public void setuMemo(String uMemo) {
this.uMemo = uMemo;
}
@Override
public String toString() {
return "User{" +
"uCode=" + uCode +
", uName='" + uName + '\'' +
", uPwd='" + uPwd + '\'' +
", uType=" + uType +
", uMemo='" + uMemo + '\'' +
'}';
}
}
UserService
package com.web.service;
import com.web.pojo.User;
import java.util.List;
public interface UserService {
public List<User> findAll();
User login(String uPwd,String uName);
User findById(int uCode);
int delete(int uCode);
int add(User user);
int updateUser(User user);
}
UserImpl
package com.web.service.Impl;
import com.web.mapper.UserMapper;
import com.web.pojo.User;
import com.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User login(String uPwd, String uName) {
return userMapper.login(uPwd,uName);
}
@Override
public User findById(int uCode) {
return userMapper.findById(uCode);
}
@Override
public int delete(int uCode) {
int i = userMapper.delete(uCode);
return i;
}
@Override
public int add(User user) {
int i = userMapper.addUser(user);
return i;
}
@Override
public int updateUser(User user) {
int i = userMapper.updateUser(user);
return i;
}
}