1.domian
public class Employee {
private Long id;
private String username;
private String realnam;
private String password;
private String tel;
private Department department;
//必须要有这个字段的domain
@JsonFormat(pattern = "yyyy-MM-dd ",timezone ="GMT+8" )
private Date inputtime;
private String state;
private Long role;
private String unionid;
private Tenant tenant;
//get.set省略
2.mapper.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="cn.itsource.crm.mapper.EmployeeMapper" >
<resultMap id="BaseResultMap" type="cn.itsource.crm.domain.Employee" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="realnam" property="realnam" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="tel" property="tel" jdbcType="VARCHAR" />
<result column="inputtime" property="inputtime" jdbcType="TIMESTAMP" />
<result column="state" property="state" jdbcType="VARCHAR" />
<result column="role" property="role" jdbcType="BIGINT" />
<result column="unionid" property="unionid" jdbcType="VARCHAR" />
<collection property="department" ofType="cn.itsource.crm.domain.Department">
<id column="did" property="id" jdbcType="BIGINT"/>
<result column="dname" property="name"></result>
</collection>
<collection property="tenant" ofType="cn.itsource.crm.domain.Tenant">
<id column="tid" property="id" jdbcType="BIGINT"/>
<result column="tcompanyname" property="companyname"></result>
</collection>
</resultMap>
<!--微信登录-->
<select id="selectByUnionId" parameterType="string" resultMap="BaseResultMap">
select id, username, realnam, password, tel, department_id, inputtime, state, role,
unionid, tenant
from t_employee
where unionid=#{unionId}
</select>
<update id="updateUnionidById">
update t_employee
set unionid = #{unionid}
where id=#{id}
</update>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from t_employee
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="cn.itsource.crm.domain.Employee" >
<selectKey resultType="java.lang.Long" keyProperty="id" order="BEFORE" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_employee (username, realnam,
password, tel, department_id,
inputtime, state, role,
unionid, tenant)
values (#{username,jdbcType=VARCHAR}, #{realnam,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{department.id,jdbcType=VARCHAR},
#{inputtime,jdbcType=TIMESTAMP}, #{state,jdbcType=VARCHAR}, #{role,jdbcType=BIGINT},
#{unionid,jdbcType=VARCHAR}, #{tenant,jdbcType=BIGINT})
</insert>
<update id="updateByPrimaryKey" parameterType="cn.itsource.crm.domain.Employee" >
update t_employee
<set>
<if test="username!=null and username!=''">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="realnam!=null and realnam!=''">
realnam = #{realnam,jdbcType=VARCHAR},
</if>
<if test="password!=null and password!=''">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="tel!=null and tel!=''">
tel = #{tel,jdbcType=VARCHAR},
</if>
<if test="department!=null and department!=''">
department_id = #{department.id,jdbcType=VARCHAR},
</if>
<if test="inputtime!=null and inputtime!=''">
inputtime = #{inputtime,jdbcType=TIMESTAMP},
</if>
<if test="state!=null and state!=''">
state = #{state,jdbcType=VARCHAR},
</if>
<if test="role!=null and role!=''">
role = #{role,jdbcType=BIGINT},
</if>
<if test="unionid!=null and unionid!=''">
unionid = #{unionid,jdbcType=VARCHAR},
</if>
<if test="tenant!=null and tenant!=''">
tenant = #{tenant,jdbcType=BIGINT}
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select id, username, realnam, password, tel, department_id, inputtime, state, role,
unionid, tenant
from t_employee
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, username, realnam, password, tel, department_id, inputtime, state, role,
unionid, tenant
from t_employee
</select>
<!-- <!–准备公共sql–>
<sql id="whereSql">
<where>
<if test="username!=null and username!=''">
and e.username like concat("%",#{username},"%")
</if>
<if test="departmentId!=null">
and e.department_id=#{department.id}
</if>
</where>
</sql>-->
<!--分页查询-->
<select id="querySelectAll" parameterType="EmployeeQuery" resultMap="BaseResultMap">
select e.id, e.username, e.realnam, e.password, e.tel, e.inputtime, e.state, e.role,
e.unionid, d.id did,d.name dname,t.id tid,t.companyname tcompanyname
from t_employee e left join t_department d on e.department_id = d.id
left join t_tenant t on e.tenant=t.id
<where>
<if test="username!=null and username!=''">
and e.username like concat("%",#{username},"%")
</if>
<if test="state!=null and state!=''">
and e.state like concat("%",#{state},"%")
</if>
<if test="oldSellerId!=null">
and e.id != #{oldSellerId}
</if>
</where>
</select>
3.前台
1.JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<script src="/static/layui/layui.js"></script>
<script src="/static/js/jquery/jquery.min.js"></script>
<script src="/static/js/jquery/jquery.jdirk.js"></script>
<script src="/static/js/jquery/jquery.form.js"></script>
<%--居中--%>
<style>
.layui-table-cell .layui-form-checkbox[lay-skin="primary"] {
top: 50%;
transform: translateY(-50%);
}
.layui-table-cell {
text-align: center;
}
</style>
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
<div class="layui-inline">
<form id="searchForm">
用户名:
<div class="layui-input-inline">
<input type="text" value="" id="usernameSearch" name="username"
placeholder="请输入关键字" class="layui-input search_input">
</div>
<%-- 部门:
<div class="layui-input-inline" style="margin-right: 20px;">
<select id="DepartmentDearch" name="departmentId" lay-search>
<option value="">--请选择--</option>
</select>
</div>--%>
状态:
<div class="layui-input-inline">
<select name="state" lay-filter="aihao" >
<option value="">--请选择--</option>
<option value="0">离职</option>
<option value="1">在职</option>
</select>
</div>
<button class="layui-btn" type="button" lay-event="search">查询</button>
<button class="layui-btn layui-btn-normal" type="button" lay-event="add">新增</button>
<button class="layui-btn layui-btn-danger " type="button" lay-event="batchDel">批量删除</button>
</form>
</div>
</script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div id="add" style="display: none">
<form class="layui-form" id="addForm" lay-filter="editForm">
<%--id隐藏域--%>
<input type="hidden" name="id" placeholder="" autocomplete="off"
class="layui-input">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<input type="text" name="username" lay-verify="required" placeholder="用户名" autocomplete="off"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">姓名</label>
<div class="layui-input-inline">
<input type="test" name="realnam" lay-verify="pass" placeholder="真实姓名"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="text" name="password" lay-verify="password" placeholder="密码"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">电话</label>
<div class="layui-input-inline">
<input type="text" name="tel" lay-verify="required|number" placeholder="电话"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">部门</label>
<div class="layui-input-inline">
<select name="department.id" lay-filter="departmentId" lay-verify="部门">
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">租户</label>
<div class="layui-input-inline">
<select name="tenant.id" lay-filter="tenant" lay-verify="租户">
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<select name="state" lay-filter="aihao" lay-verify="状态" >
<option value=""></option>
<option value="0">离职</option>
<option value="1">在职</option>
</select>
</div>
</div>
</div>
<%-- <div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">角色</label>
<div class="layui-input-inline">
<input type="text" name="rool" lay-filter="rool" placeholder="角色"
class="layui-input">
</div>
</div>
</div>--%>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">第三方ID</label>
<div class="layui-input-inline">
<input type="text" name="unionid" lay-filter="unionid" placeholder="第三方ID"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit="" lay-filter="*">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
<%--引入自己的js--%>
<script src="/static/js/employee/employee.js"></script>
</body>
</html>
2.js
layui.use(['form','layer','jquery','laypage','table'], function(){
var table = layui.table;
$ = layui.jquery;
var form=layui.form;
form.verify({
//用户名认证规则
length: function(value, item) {
if(value.trim()&&value.length<6){
return "输入的用户名不能少于6个字符";
}
},
checkName:function(value, item) {
var html=$.ajax({
url:"/employee/checkName",
data:{"username":value,"id":$("input[username='id']").val()},
async:false
}).responseText;
if(html=='false'){
return "用户名重复";
}
}
});
table.render({
elem: '#test'
,url:'/employee/list'
,toolbar: '#toolbarDemo'
,title: '用户数据表'
,cols: [[
{type: 'checkbox', fixed: 'left'}
,{field:'id', title:'ID', fixed: 'left', unresize: true, sort: true}
,{field:'username', title:'员工账号', edit: 'text'}
,{field:'realnam', title:'真实姓名', edit: 'text'}
,{field:'tel', title:'电话', edit: 'text'}
,{field:'inputtime', title:'录入时间',edit: 'text', sort: true}
,{field:'department', title:'部门', sort: true, templet: function(d){
console.debug(d)
//在js中认为false的值: 0 "" undefined false null NaN
return d.department ? d.department.name:"<span style='color: red'>无部门</span>";
}}
,{field:'state', title:'状态', edit: 'text',
templet : function(data) {// 替换数据
if(data.state==0){
return "<span style='color: red'>离职</span>";;
}
else if(data.state==1){
return "在职";
}
}
}
/* ,{field:'rool', title:'角色', edit: 'text'}*/
,{field:'unionid', title:'第三方ID', edit: 'text'}
,{field:'tenant', title:'租户', sort: true, templet: function(d){
console.debug(d)
//在js中认为false的值: 0 "" undefined false null NaN
return d.tenant ? d.tenant.companyname:"<span style='color: red'>无租户</span>";
}}
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]]
,page: true
});
//租户下拉框选择
$.get("/tenant/list",function (result) {
var select = $("select[name='tenant.id']");
select.empty();
select.append("<option value=''>请选择</option>");
$.each(result.data,function (index,obj) {
select.append("<option value='"+obj.id+"'>"+obj.companyname+"</option>");
});
form.render('select');
})
//部门下拉框
$.get("/department/list",function (result) {
var select = $("select[name='department.id']");
select.empty();
select.append("<option value=''>请选择</option>");
$.each(result.data,function (index,obj) {
select.append("<option value='"+obj.id+"'>"+obj.name+"</option>");
});
form.render('select');
})
//头工具栏事件
table.on('toolbar(test)', function(obj){
var checkStatus = table.checkStatus(obj.config.id);
switch(obj.event){
//新增
case 'add':
//清空form表单
$("#addForm").clearForm();
var data = checkStatus.data;
layer.open({
title : "新增用户",
type:1,
skin:'layui-layer-rim',
area: ['350px', '400px'],
content: $("#add"),
success : function(layero, index){
setTimeout(function(){
layui.layer.tips('点击此处可返回', '.layui-layer-setwin .layui-layer-close', {
tips: 3
});
},500)
}
})
break;
case 'search':
table.reload('test', {
where: $("#searchForm").serializeObject()//用jquery.jdirk.js实现的,将form表单中的input值转成json格式
,page: {
curr: 1 //重新从第 1 页开始
}
});
break;
case 'batchDel':
var data = checkStatus.data;
if(data.length==0){
layer.alert("请至少选中一行再进行操作",{
icon:2,
title:'错误'
});
break;
}
//准备一个id数组
var ids=[];
$.each(data,function (index, obj) {
ids.push(obj.id)
})
layer.confirm('确定删除这些数据?', function(index) {
$.post("/employee/batchDel", {"ids": ids.toString()}, function (result) {
if (result.succe) {
//刷新页面
table.reload('test',{page:{curr:1}});
layer.close(index);
} else {
layer.alert(result.backMessage, {
icon: 2,
title: '错误'
});
}
})
})
break;
};
});
//监听行工具事件
table.on('tool(test)', function(obj){
var data = obj.data;
//console.log(obj)
if(obj.event === 'del'){
layer.confirm('真的删除行么', function(index){
//删除请求
$.get("/employee/delete",{id: data.id},function (result) {
if(result.succe){
//刷新页面
table.reload('test',{page:{curr:1}});
layer.close(index);
}else {
layer.alert(result.backMessage,{
icon:2,
title:'错误'
});
}
})
});
} else if(obj.event === 'edit'){
layer.open({
title : "修改用户",
type:1,
skin:'layui-layer-rim',
area: ['350px', '400px'],
content: $("#add"),
success : function(layero, index){
setTimeout(function(){
layui.layer.tips('点击此处可返回', '.layui-layer-setwin .layui-layer-close', {
tips: 3
});
},200)
}
})
//动态赋值解决下拉框字段不一样
if(data.tenant){
data['tenant.id']=data.tenant.id;
}
if(data.department){
data['department.id']=data.department.id;
}
form.val("editForm", data)
}
});
//提交保存
form.on('submit(*)', function(data){
var url = "/employee/save";
//获取id
var id = data.field.id;
if (id) {
url = "/employee/update?param=update";
}
$.post(url,data.field,function (result) {
if(result.succe){
//弹出结果信息
layer.msg(result.backMessage);
//关闭窗口
layer.closeAll();
//刷新页面
table.reload("test");
}else {
layer.alert(result.backMessage,{
icon:2,
title:'错误'
});
}
})
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
//监听单元格编辑
table.on('edit(test)', function(obj){
var value = obj.value //修改后的单元格的值
,data = obj.data //修改单元格所属的该行所有对象
,field = obj.field; //单元格后台对应的字段
//重新准备json对象
var editJson={
"id":data.id,
"username":data.username,
"password":data.password,
"realnam":data.realnam,
"email":data.email,
"age":data.age,
"tel":data.tel,
"state":data.state,
"department.id":data.department.id
};
$.get("/employee/update",editJson,function (result) {
if(result.succe){
//弹出结果信息
layer.msg(result.backMessage);
}else {
layer.alert(result.backMessage,{
icon:2,
title:'错误'
});
//刷新页面
table.reload("test");
}
})
});
});