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) { // 查找集合 Listusers = userservice.getAllUsers(); // 获得总条数 int count = users.size(); // 分页 List
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 ListgetAllUsers(); /** * ͨ根据用户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
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@Transactionalpublic 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 ListgetAllUsers() { List 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
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)
insert user(userId,userName,age,flag) values(#userId#,#userName#,#age#,#flag#)
博主的基础群:260052172(菜鸟的天堂,大神莫入)