Bean类

UserInfo

package njitt.software.cms.bean;
import java.io.Serializable;
 import java.util.Date;import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;@AllArgsConstructor
 @NoArgsConstructor
 @Data
 public class UserInfo implements Serializable{
     /**
      * 
      */
     private static final long serialVersionUID = 3317389902771634439L;
     private Integer id;
     private String name;
     private String password;
     private String phone;
     @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
     @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
     private Date registerDate
 }

 Controller类

UserInfoController

package njitt.software.cms.controller;
import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;import com.github.pagehelper.PageInfo;
import njitt.software.cms.bean.UserInfo;
 import njitt.software.cms.service.UserInfoService;@Controller
 public class UserInfoController {
     @Autowired
     private UserInfoService userInfoService;
     
     @RequestMapping("/login")
     public String login(String name,String password,HttpSession session) {
         String msg = userInfoService.login(name, password);
         if(msg.contains("success")) {
             session.setAttribute("name",name);
             return "redirect:manager/index.jsp";
         }else {
             return "login";
         }
     }
     
     @RequestMapping("/findAllUsers")
     @ResponseBody
     public Map<String,Object> findAllUsers(int page,int limit) {
         Map<String,Object> map = new HashMap<String,Object>();
         PageInfo<UserInfo> pageInfo = userInfoService.findAllUsers(page,limit);
         map.put("code", 0);
         map.put("msg", "success");
         map.put("count", pageInfo.getTotal());
         map.put("data", pageInfo.getList());
         return map;
     }
     
     @RequestMapping("/deleteUserInfo")
     public String deleteUserInfo(HttpServletRequest request, Model model) {
         Integer id = Integer.valueOf(request.getParameter("action"));
         userInfoService.deleteUserInfo(id);
         return listUserInfo(model);
     }
     
     public String listUserInfo(Model model) {
         List<UserInfo> userInfos = userInfoService.findAllUsers();
         model.addAttribute("UserInfo", userInfos);
         return "ListUserInfo";
     }    @RequestMapping("/deleteSelectedUsers")
     public String deleteSelectedUsers(Integer[] ids) {
         List<Integer> list = Arrays.asList(ids);
         userInfoService.deleteSelectedUsers(list);
         return "redirect:manager/list_user.jsp";
     }
     
     /**
      * JSON:传入参数为JSON格式,使用@RequestBody
       *  返回值为JSON格式,使用@ResponseBody
      * @param model
      * @return 受影响的行数:1
      */
     @RequestMapping("/updateUserInfo")
     @ResponseBody
     public Integer updateUserInfo(@RequestBody UserInfo userInfo) {
         int result = userInfoService.updateUserInfo(userInfo);
         return result;
     }
     
     @RequestMapping("/register")
     public String register(UserInfo userInfo, Model model) {
         String msg = userInfoService.register(userInfo);
         if(msg.contains("成功")) {
             return "redirect:manager/list_user.jsp";
         }else {
             model.addAttribute("msg", msg);
             return "redirect:manager/register.jsp";
         }
     }
     
     @RequestMapping("/editUserInfo")
     @ResponseBody
     public UserInfo editUserInfo(int id) {
         UserInfo userInfo = userInfoService.findUserInfoById(id);
         return userInfo;
     }
     
     //用于退出页面
     @RequestMapping("/tologinout")
     public String tologinout(HttpServletRequest request) {
         request.getSession().removeAttribute("message");
         request.getSession().invalidate();
         return"login";
     }
     
 }

Mapper类

UserInfoMapper.java

package njitt.software.cms.mapper;
import java.util.List;
 import njitt.software.cms.bean.UserInfo;
public interface UserInfoMapper {
     public int insertUserInfo(UserInfo userInfo);
     public UserInfo findUserInfoByName(String name); 
     public List<UserInfo> findAllUsers();
     public int deleteUserInfo(int id);
     public void deleteSelectedUsers(List<Integer> ids);
     public Integer updateUserInfo(UserInfo userInfo);
     public UserInfo findUserInfoById(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="njitt.software.cms.mapper.UserInfoMapper">
     <resultMap type="UserInfo" id="userMap">
         <id column="user_id" property="id"/>
         <result column="user_name" property="name"/>
         <result column="user_password" property="password"/>
         <result column="user_phone" property="phone"/>
         <!-- <result column="registerDate" property="registerDate"/>     -->
     </resultMap>
     
     <insert id="insertUserInfo" parameterType="UserInfo">
         insert into user_list(user_name,user_password,user_phone)
         values(#{name},#{password},#{phone})
     </insert>
     
     <select id="findUserInfoByName" parameterType="String"
     resultMap="userMap">
         select * from user_list where user_name=#{name}
     </select>
     
     <select id="findUserInfoById" parameterType="int"
     resultMap="userMap">
         select * from user_list where user_id=#{id}
     </select>
     
     <select id="findAllUsers" resultMap="userMap">
         select * from user_list
     </select>
     
     <delete id="deleteUserInfo" parameterType="UserInfo">
         delete from user_list where user_id=#{id}
     </delete>
     
     <delete id="deleteSelectedUsers">
         delete from user_list where user_id in
         <foreach collection="list" item="id" separator="," open="("
             close=")">
             #{id}
         </foreach>
     </delete>
     
     <update id="updateUserInfo" parameterType="UserInfo">
         update user_list
         <set>
             <if test="name!=null and name!=''">
                 user_name=#{name},
             </if>
             <if test="password!=null and password!=''">
                 user_password=#{password},
             </if>
             <if test="phone!=null and phone!=''">
                 user_phone=#{phone},
             </if>
             <if test="registerDate!=null and registerDate!=''">
                 registerDate=#{registerDate},
             </if>
         </set>
         where user_id=#{id}
     </update>
 </mapper>

service

UserInfoService

package njitt.software.cms.service;
 import java.util.List;
import com.github.pagehelper.PageInfo;
import njitt.software.cms.bean.UserInfo;
public interface UserInfoService {
     public int insertUserInfo(UserInfo userInfo);
     public String login(String name,String password); 
     public PageInfo<UserInfo> findAllUsers(int pageIndex,int pageSize);
     public int deleteUserInfo(int id);
     public void deleteSelectedUsers(List<Integer>ids);
     public int updateUserInfo(UserInfo userInfo);
     public String register(UserInfo userInfo);
     public List<UserInfo> findAllUsers();
     public UserInfo findUserInfoById(int id);
     }

UserInfoServiceImpl
    

package njitt.software.cms.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo; import njitt.software.cms.bean.UserInfo;
 import njitt.software.cms.mapper.UserInfoMapper;
 import njitt.software.cms.service.UserInfoService;@Transactional
 @Service
 public class UserInfoServiceImpl implements UserInfoService{
     @Autowired
     private UserInfoMapper userInfoMapper;    @Override
     public int insertUserInfo(UserInfo userInfo) {
         return userInfoMapper.insertUserInfo(userInfo);
     }    @Override
     public List<UserInfo> findAllUsers() {
         return userInfoMapper.findAllUsers();
     }
     
     @Override
     public String login(String name,String password) {
         UserInfo userInfo = userInfoMapper.findUserInfoByName(name);
         if(userInfo !=null &&
                 userInfo.getPassword().equals(password)) {
             return "login success";
         }else {
             return "login error";
         }
     }    /**
      * 分页实现的关键代码
      * pageIndex:当前的页码,pageSize:每一页最多的记录数
      */
     @Override
     public PageInfo<UserInfo> findAllUsers(int pageIndex,int pageSize) {
         PageHelper.startPage(pageIndex,pageSize);//拦截功能,返回了当前页的pageSize条数据
         List<UserInfo> list = userInfoMapper.findAllUsers();
         PageInfo<UserInfo> pageInfo = new PageInfo<>(list);
         return pageInfo;
     }
     
     @Override
     public void deleteSelectedUsers(List<Integer> ids) {
         userInfoMapper.deleteSelectedUsers(ids);
     }
     
     @Override
     public int updateUserInfo(UserInfo userInfo) {
         return userInfoMapper.updateUserInfo(userInfo);
     }
     
     /**
      * 注册:登录名不存在,注册,否则返回提示信息
      */
     @Override
     public String register(UserInfo userInfo) {
         UserInfo existUser = userInfoMapper.findUserInfoByName(userInfo.getName());
         if(existUser==null) {
             userInfoMapper.insertUserInfo(userInfo);
             return "新增成功";
         }else {
             return "新增错误:登录名已存在";
         }
     }    @Override
     public int deleteUserInfo(int id) {
         return userInfoMapper.deleteUserInfo(id);
     }    @Override
     public UserInfo findUserInfoById(int id) {
         return userInfoMapper.findUserInfoById(id);
     }    }

Test

UserInfoTest

package njitt.software.cms.test;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import njitt.software.cms.bean.UserInfo;
 import njitt.software.cms.service.UserInfoService; /**
  * 单元测试:白盒测试,使用断言进行测试
  * @author liyu
  *
  */@RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations="classpath:applicationContext.xml")
 public class UserInfoServiceTest {    @Autowired
     private UserInfoService userInfoService;
     
     @Test
     public void testInsertUserInfo() {
         UserInfo userInfo = new UserInfo(1,"admin12","admin12","12238759669", null);
         int actual = userInfoService.insertUserInfo(userInfo);
         int expected = 1;
         Assert.assertEquals("新增错误!", expected,actual);
     }
     
     @Test
     public void testLogin() {
         String msg = userInfoService.login("admin2", "admin2");
         System.out.println(msg);    
     }
     //    @Test
 //    public void testFindAllUsers() {
 //        List<UserInfo> list = userInfoService.findAllUsers();
 //        for (UserInfo userInfo : list) {
 //            System.out.println(userInfo);
 //        }
 //    }
 }