2020暑假项目-车辆派遣管理系统开发记录#3

2020暑假项目-车辆派遣管理系统开发记录#3

内容展示

  • 1、今日完成内容,
    - 用户信息列表完成,及信息更新维护;使用layui的表格编辑功能,可直接在表格中修改信息
    - 车辆信息列表完成,及信息更新维护;使用layui的表格编辑功能,可直接在表格中修改信息
  • 2、核心源码
    userList.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<jsp:include page="common/menu.jsp" />

<html>
<head>
    <title>用户信息管理</title>
</head>

<script src="<%=basePath%>js/tools.js"></script>
<body>
<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this">用户管理</li>
    </ul>
    <div class="layui-tab-content">
        <table class="layui-hide" id="backUser" lay-filter="backUser"></table>
    </div>
</div>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addUser">新增数据</button>
    </div>
</script>

<script type="text/html" id="bduCreateDate">
    {{ dateFormat(d.createTime) }}
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启用|停用" lay-filter="bduStatusFilter" {{ d.status == 1 ? 'checked' : '' }}>
</script>
</body>
</html>
<jsp:include page="common/js.jsp" />
<script type="text/javascript">

    $('document').ready(function() {
        var layer = null;
        var laypage = null;
        var table = null;
        var form = null;
        layui.use([ "laypage", "layer", 'table' ], function() {
            layer = layui.layer;
            laypage = layui.laypage;
            table = layui.table;
            form = layui.form;

            table.render({
                elem : '#backUser',
                url : '<%=basePath%>user/usersList',
                id : 'backUser',
                height:650,
                toolbar : '#toolbar',
                cellMinWidth : 100,
                cols : [ [
                    {
                        field : 'id',
                        title : '编号',
                        width : '5%',
                        unresize : true,
                        sort : true
                    }
                    , {
                        field : 'loginName',
                        title : '登录名',
                        width : '10%',
                        sort : true
                    }
                    , {
                        field : 'pwd',
                        title : '密码',
                        edit: 'text',
                        width : '10%',
                        unresize : true
                    }
                    , {
                        field : 'nickName',
                        title : '姓名',
                        width : '10%',
                        edit: 'text',
                        sort : true
                    }
                    , {
                        field : 'tel',
                        title : '电话',
                        width : '10%',
                        edit: 'text',
                        sort : true
                    }
                    , {
                        field : 'remark',
                        title : '备注',
                        edit: 'text',
                        width : '12%',
                        unresize : true
                    }
                    , {
                        field : 'createTime',
                        title : '创建日期',
                        width : '10%',
                        templet : '#bduCreateDate',
                        unresize : true
                    }
                    , {
                        fixed : 'right',
                        width : '10%',
                        align : 'center',
                        toolbar : '#status',
                        title : '状态'
                    }
                ] ],
                page : true
            });

            //监听单元格编辑 TODO
            table.on('edit(backUser)', function(obj){
                var value = obj.value //得到修改后的值
                    ,data = obj.data //得到所在行所有键值
                    ,field = obj.field; //得到字段
                layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为:'+ value);

                var postData ={};
                postData["id"] = data.id;
                postData[field] = obj.value;

                $.ajax({
                    url:'<%=basePath %>user/updateUserByPk',
                    data:postData,
                    type:'POST',
                    success:function(data){
                        if(data.responseHead.code == '200'){
                            layer.alert('操作成功!', {icon: 1,title: "结果"});
                        }else{
                            layer.alert(data.responseHead.msg, {icon: 5,title: "结果"});
                        }

                        table.reload('backUser', {
                            url: '<%=basePath%>user/usersList'
                        });
                    }
                });
            });

            //头工具栏事件
            table.on('toolbar(backUser)', function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch(obj.event){
                    case 'addUser':
                        layer.msg('添加');
                        window.location.href="<%=basePath %>user/toAddUser";
                        break;
                };

            });

            form.on('switch(bduStatusFilter)', function(obj) {

                var ids =[];
                ids[0] = this.id;
                var type = this.value=="1"?0:1;
                $.ajax({
                    url:'<%=basePath %>user/updateUserStatus',
                    data:{
                        "type":type,
                        "list":ids
                    },
                    type:'POST',
                    success:function(data){
                        if(data.responseHead.code == '200'){
                            layer.alert('操作成功!', {icon: 1,title: "结果"});
                        }else{
                            layer.alert(data.responseHead.msg, {icon: 5,title: "结果"});
                        }

                        table.reload('backUser', {
                            url: '<%=basePath%>user/usersList'
                        });
                    }
                });
            });

        });
    });
</script>

UserController.java

    @RequestMapping(value = "/toUserList")
    public String toUserList(){return "userList";}

    @ResponseBody
    @RequestMapping(value = "/usersList",method = RequestMethod.GET)
    public Map<String,Object> userList(){
        List<UserInfo> userList = userInfoService.userList();
        int count = userInfoService.getUserCount();
        Map<String,Object> retMap = new HashMap<String,Object>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updateUserStatus",method=RequestMethod.POST)
    public Map<String,Object> updateUserStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(userInfoService.updateUserStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updateUserByPk",method=RequestMethod.POST)
    public Map<String,Object> updateUserByPk(UserInfo user){
        if(userInfoService.updateByPk(user)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddUser")
    public String toAddUser(){return "addUser";}

    @RequestMapping(value = "/addUser" , method = RequestMethod.POST)
    public String addUser(UserInfo user){
        userInfoService.insertSelective(user);
        return "redirect:toUserList";
    }

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.vdm.dao.UserInfoMapper" >
  <resultMap id="BaseResultMap" type="com.vdm.model.UserInfo" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="login_name" property="loginName" jdbcType="VARCHAR" />
    <result column="pwd" property="pwd" jdbcType="VARCHAR" />
    <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
    <result column="tel" property="tel" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="SMALLINT" />
    <result column="remark" property="remark" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, login_name, pwd, nick_name, tel, status, remark, create_time
  </sql>

  <select id="listUser" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_info
  </select>

  <select id="getUserCount" resultType="java.lang.Integer">
		select count(*) from user_info
	</select>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user_info
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="selectByLoginName" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from user_info
    where login_name = #{loginName,jdbcType=VARCHAR}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user_info
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.vdm.model.UserInfo" >
    insert into user_info (id, login_name, pwd, 
      nick_name, tel, status, 
      remark, create_time)
    values (#{id,jdbcType=INTEGER}, #{loginName,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}, 
      #{nickName,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{status,jdbcType=SMALLINT}, 
      #{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.vdm.model.UserInfo" >
    insert into user_info
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="loginName != null" >
        login_name,
      </if>
      <if test="pwd != null" >
        pwd,
      </if>
      <if test="nickName != null" >
        nick_name,
      </if>
      <if test="tel != null" >
        tel,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="remark != null" >
        remark,
      </if>

        create_time,

    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="loginName != null" >
        #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="pwd != null" >
        #{pwd,jdbcType=VARCHAR},
      </if>
      <if test="nickName != null" >
        #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="tel != null" >
        #{tel,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=SMALLINT},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createTime == null" >
        now()
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.vdm.model.UserInfo" >
    update user_info
    <set >
      <if test="loginName != null" >
        login_name = #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="pwd != null" >
        pwd = #{pwd,jdbcType=VARCHAR},
      </if>
      <if test="nickName != null" >
        nick_name = #{nickName,jdbcType=VARCHAR},
      </if>
      <if test="tel != null" >
        tel = #{tel,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=SMALLINT},
      </if>
      <if test="remark != null" >
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.vdm.model.UserInfo" >
    update user_info
    set login_name = #{loginName,jdbcType=VARCHAR},
      pwd = #{pwd,jdbcType=VARCHAR},
      nick_name = #{nickName,jdbcType=VARCHAR},
      tel = #{tel,jdbcType=VARCHAR},
      status = #{status,jdbcType=SMALLINT},
      remark = #{remark,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateUserStatus">
    update user_info
    set
    status = #{type,jdbcType=INTEGER}
    where 1=1
    <if test="ids !=null and ids.size()>0">
      and id IN
      <foreach collection="ids" open="(" close=")" separator=","
               index="index" item="item">
        #{item, jdbcType=INTEGER}
      </foreach>
    </if>
  </update>
</mapper>
  • 3、遇到的问题

  • 4、解决问题

posted @ 2020-07-20 16:44  LemonTG  阅读(146)  评论(0编辑  收藏  举报