一、UserInfo.xml Mapper映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <? 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.liu.mapper.UserInfoMapper" > < resultMap type = "UserInfo" id = "usermap" > < result property = "userId" column = "user_id" /> < result property = "userName" column = "user_name" /> < result property = "userSex" column = "user_sex" /> </ resultMap > <!-- 查询多条记录 --> < select id = "getUserList" parameterType = "UserInfo" resultMap = "usermap" > select * from user_info < where > < if test = "userName!=null and userName!=''" > and user_name like #{userName} </ if > </ where > </ select > <!-- 添加 --> < insert id = "addUser" parameterType = "UserInfo" > insert into user_info (user_name,user_sex) values(#{userName},#{userSex}) </ insert > <!-- 根据编号查询用户信息 --> < select id = "getUser" parameterType = "Integer" resultMap = "usermap" > select * from user_info where user_id = #{userId} </ select > <!-- 修改 --> < update id = "updateUser" parameterType = "UserInfo" > update user_info < set > < if test = "userName!=null and userName!=''" > user_name=#{userName}, </ if > < if test = "userSex!=null and userSex!=''" > user_sex=#{userSex}, </ if > </ set > where user_id=#{userId} </ update > <!-- 删除 --> < delete id = "deleteUser" parameterType = "Integer" > delete from user_info where user_id = #{userId} </ delete > </ mapper > |
二、Mapper接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.liu.mapper; import java.util.List; import com.liu.po.UserInfo; public interface UserInfoMapper { /** * 添加用户信息 * @param user 用户信息 * @return 返回影响行数 */ public int addUser(UserInfo user); /** * 根据条件查询用户信息 * @param user 查询条件 * @return */ public List<UserInfo> getUserList(UserInfo user); /** * 修改用户信息 * @param user 用户信息 * @return 返回影响行数 */ public int updateUser(UserInfo user); /** * 根据编号查询用户信息 * @param userId 用户编号 * @return */ public UserInfo getUser(Integer userId); /** * 删除用户信息 * @return 返回影响行数 */ public int deleteUser(Integer userId); } |
三、业务逻辑处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.liu.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.liu.mapper.UserInfoMapper; import com.liu.po.UserInfo; import com.liu.service.UserInfoService; @Service public class UserInfoServiceImpl implements UserInfoService { @Resource (name = "userInfoMapper" ) private UserInfoMapper userInfoMapper; @Override public boolean addUser(UserInfo user) { int count = userInfoMapper.addUser(user); if (count > 0 ) { return true ; } return false ; } public void setUserInfoMapper(UserInfoMapper userInfoMapper) { this .userInfoMapper = userInfoMapper; } @Override public List<UserInfo> getUserList(UserInfo user) { if (user != null && user.getUserName() != null && !user.getUserName().equals( "" )) { user.setUserName( "%" + user.getUserName() + "%" ); } return userInfoMapper.getUserList(user); } @Override public boolean updateUser(UserInfo user) { int count = userInfoMapper.updateUser(user); if (count > 0 ) { return true ; } return false ; } @Override public UserInfo getUser(Integer userId) { if (userId != null ) { return userInfoMapper.getUser(userId); } return null ; } @Override public boolean deleteUser(Integer userId) { int count = userInfoMapper.deleteUser(userId); if (count > 0 ) { return true ; } return false ; } } |
四、控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | package com.liu.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.liu.po.UserInfo; import com.liu.service.UserInfoService; @Controller public class UserInfoController { @Resource (name = "userInfoServiceImpl" ) private UserInfoService userInfoService; @RequestMapping ( "/addUser.do" ) public String addUser(UserInfo user, Model model) { System.out.println( "user:" + user); boolean mark = false ; try { mark = userInfoService.addUser(user); } catch (Exception e) { e.printStackTrace(); } if (mark) { model.addAttribute( "info" , "用户信息添加成功" ); } else { model.addAttribute( "info" , "用户信息添加失败" ); } return "userinfo/user_info" ; } @RequestMapping ( "/userlist.do" ) public String userList(UserInfo user, Model model) { try { List<UserInfo> list = userInfoService.getUserList(user); model.addAttribute( "list" , list); } catch (Exception e) { e.printStackTrace(); } return "userinfo/user_list" ; } @RequestMapping ( "/loadUpdate.do" ) public String loadUpdate(Integer userId, Model model) { try { UserInfo user = userInfoService.getUser(userId); model.addAttribute( "user" , user); } catch (Exception e) { e.printStackTrace(); } return "userinfo/user_update" ; } @RequestMapping ( "/updateUser.do" ) public String updateUser(UserInfo user, Model model) { System.out.println( "user:" + user); boolean mark = false ; try { mark = userInfoService.updateUser(user); } catch (Exception e) { e.printStackTrace(); } if (mark) { model.addAttribute( "info" , "用户信息修改成功" ); } else { model.addAttribute( "info" , "用户信息修改失败" ); } return "userinfo/user_info" ; } @RequestMapping ( "/deleteUser.do" ) public String deleteUser(Integer userId, Model model) { boolean mark = false ; try { mark = userInfoService.deleteUser(userId); } catch (Exception e) { e.printStackTrace(); } if (mark) { model.addAttribute( "info" , "用户信息删除成功" ); } else { model.addAttribute( "info" , "用户信息删除失败" ); } return "userinfo/user_info" ; } } |