1.接着上一篇,写设备管理的部分
主要界面如下图
2.这里面有增删改查的基本功能,还有分页的查询,关键词查询,还有定时刷新的功能
实体类如下
package com.timmy.entity;
public class Device {
private Integer id;
private String serialNum;
private String deviceName;
private String area;
private Integer status;
private Integer province;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSerialNum() {
return serialNum;
}
public void setSerialNum(String serialNum) {
this.serialNum = serialNum == null ? null : serialNum.trim();
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName == null ? null : deviceName.trim();
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area == null ? null : area.trim();
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getProvince() {
return province;
}
public void setProvince(Integer province) {
this.province = province;
}
@Override
public String toString() {
return "Device [id=" + id + ", serialNum=" + serialNum
+ ", deviceName=" + deviceName + ", area=" + area + ", status="
+ status + "]";
}
}
mapper
id, serial_num, device_name, area, status,province
delete from device
where id = #{id,jdbcType=INTEGER}
delete from device where serial_num = #{serialNum,jdbcType=VARCHAR}
insert into device (id, serial_num, device_name,
area, status,province)
values (#{id,jdbcType=INTEGER}, #{serialNum,jdbcType=VARCHAR}, #{deviceName,jdbcType=VARCHAR},
#{area,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},#{province,jdbcType=INTEGER})
insert into device
id,
serial_num,
device_name,
area,
status,
province,
#{id,jdbcType=INTEGER},
#{serialNum,jdbcType=VARCHAR},
#{deviceName,jdbcType=VARCHAR},
#{area,jdbcType=VARCHAR},
#{status,jdbcType=INTEGER},
#{province,jdbcType=INTEGER},
update device
serial_num = #{serialNum,jdbcType=VARCHAR},
device_name = #{deviceName,jdbcType=VARCHAR},
area = #{area,jdbcType=VARCHAR},
status = #{status,jdbcType=INTEGER},
province = #{province,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update device
set serial_num = #{serialNum,jdbcType=VARCHAR},
device_name = #{deviceName,jdbcType=VARCHAR},
area = #{area,jdbcType=VARCHAR},
status = #{status,jdbcType=INTEGER},
province = #{province,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
update device set status = #{status,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
需要注意的只有下面的这一部分,这回事关键词查询的语句
实体类和mapper映射都可以使用mybaties-generator逆向生成工具进行生成
mybatis-generator-core-1.3.2.zip解压放在本地文件夹中
下载地址:https://download.csdn.net/download/moshubai/11941711
然后修改
这个文件里面数据库的配置,如下
修改成自己的,然后在命令行进入
这个文件所在目录。输入java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite,回车,就会在 src文件中生成mapper和实体类了
3.接下来是controller部分,这部分是接收前端请求,并且调用后端的接口的。
package com.timmy.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.timmy.entity.AttandenceRecords;
import com.timmy.entity.Device;
import com.timmy.entity.DeviceTemp;
import com.timmy.entity.Msg;
import com.timmy.entity.Person;
import com.timmy.entity.Province;
import com.timmy.service.DeviceService;
import com.timmy.service.ProvinceService;
@Controller
@RequestMapping("device")
public class DeviceController {
@Autowired
DeviceService deviceService;
@Autowired
ProvinceService provinceService;
@RequestMapping("/queryDeviceList")
public String queryDevice(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
PageHelper.startPage(pn, 15);
Listlist=deviceService.selectAllDevice();
PageInfopageInfo=new PageInfo(list,10);
model.addAttribute("pageInfo", pageInfo);
return "deviceList";
}
@RequestMapping("/queryDevices")
public String queryDevice() {
return "deviceList2";
}
/*刷新设备列表*/
@RequestMapping(value="/deviceList")
@ResponseBody
public Msg refreshDevice(@RequestParam(value="pn",defaultValue="1") Integer pn) {
PageHelper.startPage(pn, 15);
Listlist=deviceService.selectAllDevice();
PageInfo page= new PageInfo(list,5);
return Msg.success().add("pageInfo", page);
}
@RequestMapping(value="/selectDeviceById")
@ResponseBody
public Msg selectDeviceById(@RequestParam("id") int id) {
Device device=deviceService.selectByPrimaryKey(id);
return Msg.success().add("device", device);
}
@ResponseBody
@RequestMapping(value = "/deviceSn", method = RequestMethod.GET)
public Msg getDevice() {
List devices = deviceService.selectDeviceOnLine();
System.out.println("设别信息" + devices);
return Msg.success().add("device", devices);
}
@ResponseBody
@RequestMapping(value = "/provinceList", method = RequestMethod.GET)
public Msg getProvince() {
//List devices = deviceService.selectDeviceOnLine();
Listprovince=provinceService.selectAllProvince();
System.out.println("设别信息" + province);
return Msg.success().add("device", province);
}
@RequestMapping(value = "/addDevice", method = RequestMethod.POST)
public Msg saveDevice(@ModelAttribute DeviceTemp deviceTemp) {
System.out.println("设备数据"+deviceTemp);
Device device=new Device();
device.setSerialNum(deviceTemp.getSerialNum());
device.setDeviceName(deviceTemp.getDeviceName());
device.setProvince(deviceTemp.getProvince());
device.setArea(deviceTemp.getArea());
device.setStatus(0);
if(deviceService.selectDeviceBySerialNum(device.getSerialNum())==null){
deviceService.insert(device);
}else{
return Msg.fail();
}
//deviceService.insert(device);
return Msg.success();
}
@RequestMapping(value = "/updateDevice", method = RequestMethod.POST)
public Msg updateDevice(@ModelAttribute DeviceTemp deviceTemp) {
System.out.println("设备数据"+deviceTemp);
Device device=deviceService.selectByPrimaryKey(deviceTemp.getId());
device.setSerialNum(deviceTemp.getSerialNum());
device.setDeviceName(deviceTemp.getDeviceName());
device.setProvince(deviceTemp.getProvince());
device.setArea(deviceTemp.getArea());
device.setStatus(0);
try {
deviceService.updateByPrimaryKey(device);
} catch (Exception e) {
// TODO Auto-generated catch block
return Msg.fail();
}
return Msg.success();
}
@RequestMapping(value = "/searchKeyword", method = RequestMethod.GET)
public String searchByKeyword(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam(value="province",defaultValue="0")int province,@Param("keyword")String keyword,Model model) {
System.out.println("shen"+province);
PageHelper.startPage(pn, 15);
Listlist=new ArrayList();
if(province!=0){
list=deviceService.selectByProvinceId(province);
System.out.println("通过编号查询"+list);
}else{
list=deviceService.selectByKeyword(keyword);
}
PageInfopageInfo=new PageInfo(list,10);
model.addAttribute("pageInfo", pageInfo);
return "deviceList";
}
@RequestMapping(value = "/searchByProvince", method = RequestMethod.GET)
public String searchByProvince(@RequestParam(value="pn",defaultValue="1")Integer pn,@Param("province")int province) {
PageHelper.startPage(pn, 8);
Listlist=deviceService.selectByProvinceId(province);
PageInfo page= new PageInfo(list,5);
return "hello1";
}
@RequestMapping("/deleteDevice")
public String deleteDevice(@RequestParam("id")int id) {
deviceService.deleteByPrimaryKey(id);
return "deviceList2";
}
}
4.前端代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
Dynamic Face Device Auto Data Manage System
Operation
ID
DeviceSn
Name
Parameter
Province
Status
Operation
后续,我会把项目上传,介绍一些刚入门的不太熟悉的功能怎么用
附上项目源码连接https://download.csdn.net/download/moshubai/11945282