ssm框架实现用户增删改查
1.Dao类
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectById(int id);
void update(User user);
}
2.Controller类
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");
}else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> userList = userService.findAll();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("userList",userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
private 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")
private String Update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
3.Service接口类
public interface IUserService {
boolean login(String username,String password);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
4.Service实现类
@Service
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUserName(username);
if (user!=null && user.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
5.xml配置
<mapper namespace="com.xkf.dao.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="user">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
</select>
<delete id="deleteById" parameterType="int" >
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user (username,password) values (#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=(#{id})
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>