包结构
controller层
package com.ww.ssm.controller;
import com.ww.ssm.po.MyUser;
import com.ww.ssm.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
@Controller
public class MyUserController {
@Autowired
MyUserService myUserService;
@RequestMapping("/users.do")
public ModelAndView userlist() throws Exception {
List<MyUser> list=myUserService.selectUsersSer();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("list",list);
modelAndView.setViewName("myuserlist");
return modelAndView;
}
@RequestMapping("/del.do")
public ModelAndView deluser(int id) throws Exception {
System.out.println(id+"-----------------");
myUserService.delUserSer(id);
return userlist();
}
@RequestMapping("/add.do")
public String add() throws Exception {
return "redirect:add.jsp";
}
@RequestMapping("/addMyuser.do")
public ModelAndView adduser(MyUser myUser) throws Exception {
myUserService.addUserSer(myUser);
return userlist();
}
@RequestMapping("/up.do")
public ModelAndView up(String id) throws Exception {
int uid=Integer.valueOf(id);
MyUser myUser = myUserService.selectByIdSer(uid);
System.out.println(myUser);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("myuser",myUser);
modelAndView.setViewName("edit");
return modelAndView;
}
@RequestMapping("/update.do")
public ModelAndView up(MyUser myUser) throws Exception {
myUserService.upUserSer(myUser);
return userlist();
}
//批量查询操作
@RequestMapping("/queryMyusers.do")
public ModelAndView queryMyuserpacths(Integer[] check_id) throws Exception {
List<MyUser> list=new ArrayList<MyUser>();
for(int i:check_id)
{
System.out.println(i);
list.add(myUserService.selectByIdSer(i));
}
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("list",list);
modelAndView.setViewName("myuserlist");
return modelAndView;
}
//批量删除操作
@RequestMapping("/deleteMyusers.do")
public ModelAndView delMyUsers(Integer[] check_id) throws Exception {
for(int i:check_id)
{
System.out.println(i);
myUserService.delUserSer(i);
}
return userlist();
}
//批量修改操作
@RequestMapping("/upMyusers.do")
public ModelAndView upMyUsers(Integer[] check_id) throws Exception {
List<MyUser> list=new ArrayList<MyUser>();
for(int i:check_id)
{
System.out.println(i);
list.add(myUserService.selectByIdSer(i));
}
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("list",list);
modelAndView.setViewName("uplist");
return modelAndView;
}
//批量修改操作
@RequestMapping("/updateUsers.do")
public void updateMyUsers(Integer[] check_id) throws Exception {
}
}
mapper
package com.ww.ssm.mapper;
import com.ww.ssm.po.MyUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface MyUserMapper {
@Select("select * from myuser")
List<MyUser> selectUsers() throws Exception;
@Delete("delete from myuser where id=#{id}")
void delUser(int id)throws Exception;
@Insert("insert into myuser(username,sex,address) values(#{username},#{sex},#{address})")
void addUser(MyUser myUser) throws Exception;
@Select("select * from myuser where id=#{id}")
MyUser selectById(int id) throws Exception;
@Update("update myuser set username=#{username},sex=#{sex},address=#{address} where id=#{id}")
void upUser(MyUser myUser) throws Exception;
}
po
package com.ww.ssm.po;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyUser {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public MyUser(String username, String sex, String address) {
this.username = username;
this.sex = sex;
this.address = address;
}
}
service
package com.ww.ssm.service;
import com.ww.ssm.po.MyUser;
import org.springframework.stereotype.Service;
import java.util.List;
public interface MyUserService {
List<MyUser> selectUsersSer() throws Exception;
void delUserSer(int id)throws Exception;
void addUserSer(MyUser myUser) throws Exception;
MyUser selectByIdSer(int id) throws Exception;
void upUserSer(MyUser myUser) throws Exception;
}
serviceimpl
package com.ww.ssm.service.impl;
import com.ww.ssm.mapper.MyUserMapper;
import com.ww.ssm.po.MyUser;
import com.ww.ssm.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MyUserServiceImpl implements MyUserService {
@Autowired
MyUserMapper mapper;
@Override
public List<MyUser> selectUsersSer() throws Exception {
return mapper.selectUsers();
}
@Override
public void delUserSer(int id) throws Exception {
mapper.delUser(id);
}
@Override
public void addUserSer(MyUser myUser) throws Exception {
mapper.addUser(myUser);
}
@Override
public MyUser selectByIdSer(int id) throws Exception {
return mapper.selectById(id);
}
@Override
public void upUserSer(MyUser myUser) throws Exception {
mapper.upUser(myUser);
}
}
数据库