controller(控制器UserController)
package com.ninemax.user.controller; import java.io.IOException; import java.io.OutputStream; import java.net.URLDecoder; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.ninemax.user.model.User; import com.ninemax.user.service.UserService; /** * controller * * @author Darker * */ @Controller @RequestMapping("/user") public class UserController { @Resource private UserService userservice; /** * 更新用户操作 * * @param user * 用户对象 * @param req * @param rep */ @RequestMapping("/update") public void update(User user, HttpServletRequest req, HttpServletResponse rep) { try { rep.setContentType("text/html;charset=UTF-8"); // 获得输出流 OutputStream printWriter = null; printWriter = rep.getOutputStream(); // 对字符串进行转码操作 User newuser = null; // 转码字符串 ,防止乱码 newuser = new User(Integer.parseInt(URLDecoder.decode(user .getUserId().toString(), "utf-8")), URLDecoder.decode( user.getUserName(), "utf-8"), URLDecoder.decode( user.getAge(), "utf-8"), URLDecoder.decode(user.getFlag(), "utf-8")); userservice.update(newuser); printWriter.write("su".getBytes()); //关闭链接 printWriter.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 初始化界面 * * @return */ @RequestMapping("/index") public String toIndex(HttpServletRequest req, HttpServletResponse rep) { // 查找集合 List<User> users = userservice.getAllUsers(); // 获得总条数 int count = users.size(); // 分页 List<Map<String, Integer>> pageList = userservice.queryCount(count); // 放入作用域中 req.setAttribute("userlist", users); req.setAttribute("pageList", pageList); req.setAttribute("sumPage", count / 4 == 0 ? 1 : (count / 4) + 1); req.setAttribute("sumTotal", count); return "Index"; } /** * 进入添加界面 * * @return */ @RequestMapping("/toadd") public String toAdd() { return "add"; } /** * 初始化更新界面 * * @param req * @param id * @return */ @RequestMapping("/toUpdate") public String toUpdate(HttpServletRequest req, Integer id) { // 查找要更新的数据 User user = userservice.getUsrById(id); // 放入作用域 req.setAttribute("user", user); return "Userinfo"; } /** * 删除操作 * * @param req * 请求 * @param id * 删除的id * @param rep * 响应 * @return */ @RequestMapping("/del") public String del(HttpServletRequest req, Integer id, HttpServletResponse rep) { // 删除对应的数据库数据 userservice.delUser(id); // 获取数据集合 List<User> users = userservice.getAllUsers(); // 放入作用域 req.setAttribute("userlist", users); return "Index"; } /** * 假删除 * * @param req * 请求 * @param id * 删除的id * @param rep * 响应 * @return */ @RequestMapping("/falseDel") public void falseDel(Integer id, HttpServletResponse rep) { try { // 删除对应的数据库数据 userservice.falseDel(id); // 设置ajax返回类型 rep.setContentType("text/html;charset=UTF-8"); // 得到输出流 OutputStream printWriter = rep.getOutputStream(); // 输出提示信息 printWriter.write("success".getBytes()); //关闭链接 printWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 添加操作 * * @param name * 用户名 * @param age * 年龄 * @param rep * 响应 */ @RequestMapping("/add") public void add(String name, String age, String flag, HttpServletResponse rep) { try { // 设置ajax的返回类型 rep.setContentType("text/html;charset=UTF-8"); // 获得输出流 OutputStream printWriter = null; // ʵ实例化输出流 printWriter = rep.getOutputStream(); User user = null; user = new User(null, URLDecoder.decode(name, "utf-8"), URLDecoder.decode(age, "utf-8"), URLDecoder.decode(flag, "utf-8")); userservice.addUser(user); // 提示信息 printWriter.write("su".getBytes()); //关闭链接 printWriter.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
service(接口UserService)
package com.ninemax.user.service; import java.util.List; import java.util.Map; import com.ninemax.user.model.User; public interface UserService { /** * 添加用户操作 * * @param user * 用户 */ public void addUser(User user); /** * 用户集合 * * @return 集合类型 */ public List<User> getAllUsers(); /** * ͨ根据用户iD查询 * * @param id * @return user */ public User getUsrById(Integer UserId); /** * ɾ删除用户 * * @param userId */ public void delUser(Integer user); /** * 更新用户 * * @param user */ public void update(User user); /** * 分页 * @param count 总条数 * @return */ public List<Map<String,Integer>> queryCount(int count); /** * 非物理删除 * * @param user */ public void falseDel(int id); }
serviceImpl(实现类UserServiceImpl)
package com.ninemax.user.service.impl; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.ibatis.sqlmap.client.SqlMapClient; import com.ninemax.user.model.User; import com.ninemax.user.service.UserService; @Service @Transactional public class UserServiceImpl implements UserService{ @Resource private SqlMapClient sqlMapClient; @Override public void addUser(User user) { try { sqlMapClient.insert("adduser", user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unchecked") @Override public List<User> getAllUsers() { List<User> users=null; try { users=sqlMapClient.queryForList("selall",-1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return users; } @Override public User getUsrById(Integer UserId) { User user=null; try { user=(User) sqlMapClient.queryForObject("selById", UserId); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return user; } @Override public void delUser(Integer user) { try { sqlMapClient.delete("del", user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void update(User user) { try { sqlMapClient.update("update", user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 分页 @Override public List<Map<String,Integer>> queryCount(int count) { // TODO Auto-generated method stub // 存放分页信息 Map<String,Integer> map = new HashMap<String,Integer>(); // 存放分页集合 List<Map<String,Integer>> list= new ArrayList<Map<String,Integer>>(); // 存放信息 // 1.每页显示的条数 map.put("visiblePages", 10); // 2.当前页 map.put("currentPage", 1); // 3.总页数 map.put("totalPages", count/4==0?1:(count/4)+1); // 存放集合 list.add(map); return list; } //假删除 @Override public void falseDel(int id) { // TODO Auto-generated method stub try { Map<String,Integer> map = new HashMap<String,Integer> (); map.put("userId", id); map.put("flag", -1); sqlMapClient.update("falseDel",map); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
model(实体类User)
package com.ninemax.user.model; public class User { private Integer userId; private String userName; private String age; private String flag; public User() { } public User(Integer userId, String userName, String age, String flag) { this.userId = userId; this.userName = userName; this.age = age; this.flag = flag; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } }
model的映射文件(User-config.xml)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="User"> <typeAlias alias="User" type="com.ninemax.user.model.User" /> <resultMap id="UserResult" class="User"> <result property="userId" column="userId" /> <result property="userName" column="userName" /> <result property="age" column="age" /> </resultMap> <insert id="adduser" parameterClass="User"> insert user(userId,userName,age,flag) values(#userId#,#userName#,#age#,#flag#) </insert> <select id="selall" parameterClass="int" resultClass="User"> select * from user where flag!=#flag# </select> <select id="selById" parameterClass="int" resultClass="User"> select * from user where userId=#userId# </select> <!-- 查询数据条数 --> <select id="selCount" resultClass="int"> select count(1) from user </select> <delete id="del" parameterClass="int"> delete from user where userId=#userId# </delete> <update id="update" parameterClass="User"> update user set userName=#userName#, age=#age# where userId=#userId# </update> <!-- 假删除 --> <update id="falseDel" parameterClass="java.util.HashMap" > update user set flag=#flag# where userId=#userId# </update> </sqlMap>
博主的基础群:260052172(菜鸟的天堂,大神莫入)