代码
UserInfo.java
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public UserInfo() {
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = 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 "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
IUserInfoDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserInfoDao {
public List<UserInfo> findAll();
public void add(UserInfo userInfo);
public void delete(int id);
public void update(UserInfo userInfo);
public UserInfo findUserById(int id);
}
UserInfoMapper.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.zhongruan.dao.IUserInfoDao">
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo;
</select>
<insert id="add" parameterType="com.zhongruan.bean.UserInfo">
insert into userinfo(id,username,password)values (#{id},#{username},#{password});
</insert>
<update id="update" parameterType="com.zhongruan.bean.UserInfo">
update userinfo set username=#{username},password=#{password} where id=#{id};
</update>
<delete id="delete" parameterType="int" >
delete from userinfo where id=#{id};
</delete>
<select id="findUserById" parameterType="int" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where id=#{id};
</select>
</mapper>
IUserInfoService.java
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserInfoService {
public List<UserInfo> findAll();
public void add(UserInfo userInfo);
public void delete(int id);
public void update(UserInfo userInfo);
public UserInfo findUserById(int id);
}
UserInfoServiceImpl.java
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserInfoDao;
import com.zhongruan.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
@Autowired
IUserInfoDao userInfoDao;
@Override
public List<UserInfo> findAll() {
return userInfoDao.findAll();
}
@Override
public void add(UserInfo userInfo) {
userInfoDao.add(userInfo);
}
@Override
public void delete(int id) {
userInfoDao.delete(id);
}
@Override
public void update(UserInfo userInfo) {
userInfoDao.update(userInfo);
}
@Override
public UserInfo findUserById(int id) {
return userInfoDao.findUserById(id);
}
}
UserInfoController.java
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserInfoService;
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.List;
@Controller
@RequestMapping("user")
public class UserInfoController {
@Autowired
IUserInfoService userInfoService;
@RequestMapping("findAll.do")
public ModelAndView findAll() {
List<UserInfo> users = userInfoService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users", users);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("toAddUser.do")
public ModelAndView toAdd() {
ModelAndView mv = new ModelAndView();
mv.setViewName("addUser");
return mv;
}
@RequestMapping("save.do")
public ModelAndView save(int id, String username, String password) {
UserInfo userInfo = new UserInfo(id, username, password);
userInfoService.add(userInfo);
return findAll();
}
@RequestMapping("delete.do")
public ModelAndView delete(int id) {
userInfoService.delete(id);
List<UserInfo> users = userInfoService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users", users);
mv.setViewName("allUser");
return mv;
}
@RequestMapping("toUpdate.do")
public ModelAndView toUpdate(int id) {
UserInfo userInfo = userInfoService.findUserById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("userInfo", userInfo);
mv.setViewName("updateUser");
return mv;
}
@RequestMapping("update.do")
public ModelAndView updateUser(int id, String username, String password) {
UserInfo userInfo = new UserInfo(id, username, password);
userInfoService.update(userInfo);
return findAll();
}
}