BaseMemberController.java
@RestController
@RequestMapping("base/basemember")
注解为controller
注解路径为"base/basemember"
@Autowired
private BaseMemberService baseMemberService;
注入service
@RequestMapping("/list")
@RequiresPermissions("base:basemember:list")
注解路径为"/list",
注入权限为"base:basemember:list"
LOGGER.info("【病患管理】获取列表数据,参数:{}", new Gson().toJson(params));
日志记录用于在控制台打印数据
new Gson().toJson(params)将参数转为Json
PageUtils page = baseMemberService.listPage(params);
调用service方法
Service.java
BaseMemberService.java编写接口
BaseMemberServiceImpl实现接口方法,包含以下几种方法:
queryPage()
listPage()
selectByPatNo()
public class BaseMemberServiceImpl extends ServiceImpl<BaseMemberDao, BaseMemberEntity> implements BaseMemberService {
从泛型中可以看到引用了BaseMemberDao和BaseMemberEntity,
BaseMemberEntity实体类可以看到对应的表名,及对应表的属性
@TableName("base_member")
BaseMemberDao.java编写接口抽象方法
BaseMemberDao.xml编写实现抽象方法的SQL语句
总结:
Controller调用Service,转向ServiceImpl,
ServiceImpl调用Dao.java,
Dao.java从Dao.xml抽取数据。
BaseMemberController.java
package com.aoyang.modules.base.controller;
import com.aoyang.modules.base.entity.BaseMemberEntity;
import com.aoyang.modules.base.service.BaseMemberService;
import com.google.gson.Gson;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Map;
/**
* @author went
* @email went@aoyang.com
* @date 2019-10-08 09:58:35
*/
@RestController
@RequestMapping("base/basemember")
public class BaseMemberController {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseMemberController.class);
@Autowired
private BaseMemberService baseMemberService;
public BaseMemberController() {
super();
}
/**
* 列表
*/
@RequestMapping("/list")
@RequiresPermissions("base:basemember:list")
public R list(@RequestParam Map<String, Object> params) {
LOGGER.info("【病患管理】获取列表数据,参数:{}", new Gson().toJson(params));
PageUtils page = baseMemberService.listPage(params);
LOGGER.info("【病患管理】获取列表数据:{}", new Gson().toJson(page));
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
@RequiresPermissions("base:basemember:info")
public R info(@PathVariable("id") Long id) {
LOGGER.info("【病患管理】获取病患信息,参数:{}", id);
BaseMemberEntity baseMember = baseMemberService.selectById(id);
LOGGER.info("【病患管理】获取病患信息:{}", new Gson().toJson(baseMember));
return R.ok().put("baseMember", baseMember);
}
/**
* 保存
*/
@RequestMapping("/save")
@RequiresPermissions("base:basemember:save")
public R save(@RequestBody BaseMemberEntity baseMember) {
LOGGER.info("【病患管理】保存,参数:{}", new Gson().toJson(baseMember));
Long userId = ShiroUtils.getUserId();
baseMember.setCreateBy(userId);
baseMember.setLastModifyBy(userId);
baseMemberService.insert(baseMember);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
@RequiresPermissions("base:basemember:update")
public R update(@RequestBody BaseMemberEntity baseMember) {
LOGGER.info("【病患管理】更新,参数:{}", new Gson().toJson(baseMember));
ValidatorUtils.validateEntity(baseMember);
baseMember.setLastModifyBy(ShiroUtils.getUserId());
baseMemberService.updateById(baseMember);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
@RequiresPermissions("base:basemember:delete")
public R delete(@RequestBody Long[] ids) {
LOGGER.info("【病患管理】删除,参数:{}", new Gson().toJson(ids));
baseMemberService.deleteBatchIds(Arrays.asList(ids));
return R.ok();
}
}
BaseMemberService.java
package com.aoyang.modules.base.service;
import com.aoyang.modules.base.entity.BaseMemberEntity;
import com.baomidou.mybatisplus.service.IService;
import io.renren.common.utils.PageUtils;
import java.util.List;
import java.util.Map;
/**
* @author went
* @email went@aoyang.com
* @date 2019-10-08 09:58:35
*/
public interface BaseMemberService extends IService<BaseMemberEntity> {
PageUtils queryPage(Map<String, Object> params);
PageUtils listPage(Map<String, Object> params);
List<BaseMemberEntity> selectByPatNo(String patNo);
}
BaseMemberServiceImpl.java
package com.aoyang.modules.base.service.impl;
import com.aoyang.modules.base.dao.BaseMemberDao;
import com.aoyang.modules.base.entity.BaseMemberEntity;
import com.aoyang.modules.base.service.BaseMemberService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service("baseMemberService")
public class BaseMemberServiceImpl extends ServiceImpl<BaseMemberDao, BaseMemberEntity> implements BaseMemberService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
Page<BaseMemberEntity> page = this.selectPage(
new Query<BaseMemberEntity>(params).getPage(),
new EntityWrapper<BaseMemberEntity>()
);
return new PageUtils(page);
}
@Override
public PageUtils listPage(Map<String, Object> params) {
Page<BaseMemberEntity> page = new Query<BaseMemberEntity>(params).getPage();
List<BaseMemberEntity> baseMemberEntities = baseMapper.listPage(page, (String) params.get("keyword"));
return new PageUtils(page.setRecords(baseMemberEntities));
}
@Override
public List<BaseMemberEntity> selectByPatNo(String patNo) {
Map<String, Object> param = Maps.newHashMap();
param.put("patNo", patNo);
return baseMapper.selectByPatNo(param);
}
}
BaseMemberDao.java
package com.aoyang.modules.base.dao;
import com.aoyang.modules.base.entity.BaseMemberEntity;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import java.util.List;
import java.util.Map;
/**
* @author went
* @email went@aoyang.com
* @date 2019-10-08 09:58:35
*/
public interface BaseMemberDao extends BaseMapper<BaseMemberEntity> {
List<BaseMemberEntity> listPage(Page<BaseMemberEntity> page, String keyword);
List<BaseMemberEntity> selectByPatNo(Map<String, Object> param);
}
BaseMemberDao.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.aoyang.modules.base.dao.BaseMemberDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.aoyang.modules.base.entity.BaseMemberEntity" id="baseMemberMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="patNo" column="pat_no"/>
<result property="gender" column="gender"/>
<result property="genderDesc" column="gender_desc"/>
<result property="birthday" column="birthday"/>
<result property="age" column="age"/>
<result property="remark" column="remark"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="lastModifyBy" column="last_modify_by"/>
<result property="lastModifyTime" column="last_modify_time"/>
<result property="deleted" column="deleted"/>
</resultMap>
<select id="listPage" resultMap="baseMemberMap">
select
a.*
,b.value as gender_desc
from base_member a
left join sys_dict b on a.gender = b.id
where deleted = 0
<if test="_parameter != null">
<bind name="pattern" value="'%' + _parameter + '%'"/>
and a.name like #{pattern}
</if>
</select>
<select id="selectByPatNo" resultMap="baseMemberMap">
select
a.*
,b.value as gender_desc
,year( from_days( datediff( now(), a.birthday))) as age
from base_member a
left join sys_dict b on a.gender = b.id
where deleted = 0
<if test="patNo!= null and patNo != ''">
and
a.pat_no =#{patNo}
</if>
</select>
</mapper>
basemember.html
<!DOCTYPE html>
<html>
<head>
<title>成员管理</title>
<#include "/header.html">
</head>
<body>
<div id="rrapp" v-cloak>
<div v-show="showList">
<div class="grid-btn">
<div class="form-group col-sm-2">
<input type="text" class="form-control" v-model="q.name" @keyup.enter="query" placeholder="姓名">
</div>
<a class="btn btn-default" @click="query">查询</a>
<#if shiro.hasPermission("base:basemember:save")>
<a class="btn btn-primary" @click="add"><i class="fa fa-plus"></i> 新增</a>
</#if>
<#if shiro.hasPermission("base:basemember:update")>
<a class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i> 修改</a>
</#if>
<#if shiro.hasPermission("base:basemember:delete")>
<a class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i> 删除</a>
</#if>
</div>
<table id="jqGrid"></table>
<div id="jqGridPager"></div>
</div>
<div v-show="!showList" class="panel panel-default">
<div class="panel-heading">{{title}}</div>
<form class="form-horizontal">
<div class="form-group">
<div class="col-sm-2 control-label">姓名</div>
<div class="col-sm-10">
<input type="text" class="form-control" v-model="baseMember.name" placeholder="姓名"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">性别</div>
<div class="col-sm-10">
<select class="form-control" v-model="baseMember.gender">
<option v-for="g in genders" :value="g.id">{{g.value}}</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">登记号</div>
<div class="col-sm-10">
<input type="text" class="form-control" v-model="baseMember.patNo" placeholder="病人登记号"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">出生日期</div>
<div class="col-sm-10">
<input type="date" class="form-control layui-input" id="birthday-input"
v-model="baseMember.birthday" placeholder="出生日期"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">手机号码</div>
<div class="col-sm-10">
<input type="text" class="form-control" v-model="baseMember.phone" placeholder="病人手机号码"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">备注</div>
<div class="col-sm-10">
<input type="text" class="form-control" v-model="baseMember.remark" placeholder="备注"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2 control-label"></div>
<input type="button" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
<input type="button" class="btn btn-warning" @click="reload" value="返回"/>
</div>
</form>
</div>
</div>
<script src="${request.contextPath}/statics/plugins/laydate/laydate.js"></script>
<script src="${request.contextPath}/statics/js/modules/base/basemember.js?_${.now?long}"></script>
</body>
</html>
basemember.js
$(function () {
$("#jqGrid").jqGrid({
url: baseURL + 'base/basemember/list',
datatype: "json",
colModel: [
{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
{ label: '姓名', name: 'name', index: 'name', width: 80 },
{ label: '病人登记号', name: 'patNo', index: 'pat_no', width: 80 },
{ label: '性别', name: 'genderDesc', index: 'gender', width: 80 },
{ label: '出生日期', name: 'birthday', index: 'birthday', width: 80 },
{ label: '手机号码', name: 'phone', index: 'phone', width: 80 },
{ label: '备注', name: 'remark', index: 'remark', width: 80 },
{ label: '创建人', name: 'createBy', index: 'create_by', width: 80 },
{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
{ label: '最后修改人', name: 'lastModifyBy', index: 'last_modify_by', width: 80 },
{ label: '最后修改时间', name: 'lastModifyTime', index: 'last_modify_time', width: 80 }
],
viewrecords: true,
height: 385,
rowNum: 10,
rowList : [10,30,50],
rownumbers: true,
rownumWidth: 25,
autowidth:true,
multiselect: true,
pager: "#jqGridPager",
jsonReader : {
root: "page.list",
page: "page.currPage",
total: "page.totalPage",
records: "page.totalCount"
},
prmNames : {
page:"page",
rows:"limit",
order: "order"
},
gridComplete:function(){
//隐藏grid底部滚动条
$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
}
});
laydate.render({
elem: "#birthday-input",
min: "1900-01-01",
max: 0,
done: function(value){
vm.baseMember.birthday = value;
}
})
});
var vm = new Vue({
el:'#rrapp',
data:{
q:{
name: null
},
showList: true,
title: null,
baseMember: {},
genders:null
},
methods: {
query: function () {
vm.reload();
},
add: function(){
vm.showList = false;
vm.title = "新增";
vm.baseMember = {};
vm.getGenders();
},
update: function (event) {
var id = getSelectedRow();
if(id == null){
return ;
}
vm.showList = false;
vm.title = "修改";
vm.getGenders();
vm.getInfo(id);
},
saveOrUpdate: function (event) {
var url = vm.baseMember.id == null ? "base/basemember/save" : "base/basemember/update";
$.ajax({
type: "POST",
url: baseURL + url,
contentType: "application/json",
data: JSON.stringify(vm.baseMember),
success: function(r){
if(r.code === 0){
alert('操作成功', function(index){
vm.reload();
});
}else{
alert(r.msg);
}
}
});
},
del: function (event) {
var ids = getSelectedRows();a
if(ids == null){
return ;
}
confirm('确定要删除选中的记录?', function(){
$.ajax({
type: "POST",
url: baseURL + "base/basemember/delete",
contentType: "application/json",
data: JSON.stringify(ids),
success: function(r){
if(r.code == 0){
alert('操作成功', function(index){
$("#jqGrid").trigger("reloadGrid");
});
}else{
alert(r.msg);
}
}
});
});
},
getInfo: function(id){
$.get(baseURL + "base/basemember/info/"+id, function(r){
vm.baseMember = r.baseMember;
});
},
reload: function (event) {
vm.showList = true;
var page = $("#jqGrid").jqGrid('getGridParam','page');
$("#jqGrid").jqGrid('setGridParam',{
postData:{'keyword': vm.q.name},
page:page
}).trigger("reloadGrid");
},
getGenders: function() {
$.get(baseURL + "sys/dict/sex", function (r) {
vm.genders = r.sex;
})
}
}
});