一后端
1:entity
package com.woniu.community.controller;
import com.woniu.community.entity.Building;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.entity.vo.BuildingVO;
import com.woniu.community.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/building")
public class BuildingController {
@Autowired
private IBuildingService iBuildingService;
@RequestMapping("/list")
HttpResult selectAll(String numbers,int pageIndex, int pageSize){
return iBuildingService.selectAll(numbers,pageIndex,pageSize);
}
@RequestMapping ("add")
HttpResult insertBuilding( BuildingVO vo){
Building building = new Building();
building.setNumbers(vo.getNumbers());
building.setUints(vo.getUints());
return iBuildingService.insertBuilding(building);
}
@RequestMapping("update")
HttpResult updateBuilding( Building building){
return iBuildingService.updateBuilding(building);
}
@RequestMapping("delete")
HttpResult deleteBuilding(int id){
return iBuildingService.deleteBuilding(id);
}
@RequestMapping("info")
HttpResult selectById(int id){
return iBuildingService.selectById(id);
}
}
2:BuildingMapper
package com.woniu.community.mapper;
import com.woniu.community.entity.Building;
import com.woniu.community.entity.User;
import java.util.List;
public interface BuildingMapper {
List<Building> selectAll(String numbers,int start,int size);
int buildCount(String numbers);
int insertBuilding(Building building);
int updateBuilding(Building building);
int deleteBuilding(int id);
Building selectById(int id);
}
3:BuildingService
package com.woniu.community.service;
import com.woniu.community.entity.Building;
import com.woniu.community.entity.HttpResult;
import java.util.List;
public interface IBuildingService {
HttpResult selectAll( String numbers,int pageIndex, int pageSize);
HttpResult insertBuilding(Building building);
HttpResult updateBuilding(Building building);
HttpResult deleteBuilding(int id);
HttpResult selectById(int id);
}
4:BuildingServiceImpl
package com.woniu.community.service.impl;
import com.woniu.community.entity.Building;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.mapper.BuildingMapper;
import com.woniu.community.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BuildingServiceImpl implements IBuildingService {
@Autowired(required = false)
private BuildingMapper buildingMapper;
@Override
public HttpResult selectAll( String numbers,int pageIndex, int pageSize) {
HttpResult result=null;
List<Building> buildings = buildingMapper.selectAll(numbers,(pageIndex - 1)*pageSize, pageSize);
int count = buildingMapper.buildCount( numbers);
if (buildings!=null&&buildings.size()>0){
result=new HttpResult(buildings,count,200,null);
}else{
result=new HttpResult(null,0,200,"没有更多数据");
}
return result;
}
@Override
public HttpResult insertBuilding(Building building) {
HttpResult result=null;
int count = buildingMapper.insertBuilding(building);
if (count>0){
result=new HttpResult(null,count,200,"添加成功");
}else{
result=new HttpResult(null,count,500,"添加失败");
}
return result;
}
@Override
public HttpResult updateBuilding(Building building) {
HttpResult result=null;
int count = buildingMapper.updateBuilding(building);
if (count>0){
result=new HttpResult(null,count,200,"修改成功");
}else{
result=new HttpResult(null,count,500,"修改失败");
}
return result;
}
@Override
public HttpResult deleteBuilding(int id) {
HttpResult result=null;
int count = buildingMapper.deleteBuilding(id);
if (count>0){
result=new HttpResult(null,count,200,"删除成功");
}else{
result=new HttpResult(null,count,500,"删除失败");
}
return result;
}
@Override
public HttpResult selectById(int id) {
HttpResult result=null;
Building building = buildingMapper.selectById(id);
if (building!=null){
result=new HttpResult(building,0,200,null);
}else{
result=new HttpResult(null,0,500,"没有更多数据");
}
return result;
}
}
5:BuildingController
package com.woniu.community.controller;
import com.woniu.community.entity.Building;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.entity.vo.BuildingVO;
import com.woniu.community.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/building")
public class BuildingController {
@Autowired
private IBuildingService iBuildingService;
@RequestMapping("/list")
HttpResult selectAll(String numbers,int pageIndex, int pageSize){
return iBuildingService.selectAll(numbers,pageIndex,pageSize);
}
@RequestMapping ("add")
HttpResult insertBuilding( BuildingVO vo){
Building building = new Building();
building.setNumbers(vo.getNumbers());
building.setUints(vo.getUints());
return iBuildingService.insertBuilding(building);
}
@RequestMapping("update")
HttpResult updateBuilding( Building building){
return iBuildingService.updateBuilding(building);
}
@RequestMapping("delete")
HttpResult deleteBuilding(int id){
return iBuildingService.deleteBuilding(id);
}
@RequestMapping("info")
HttpResult selectById(int id){
return iBuildingService.selectById(id);
}
}
6:BulidingMapper.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.woniu.community.mapper.BuildingMapper">
<resultMap id="buildMap" type="Building">
<result column="id" property="id"/>
<result column="numbers" property="numbers"/>
<result column="uints" property="uints"/>
<result column="remarks" property="remarks"/>
</resultMap>
<select id="selectAll" resultMap="buildMap">
select * from building
<where>
<if test="numbers!=null and numbers!='null'">
and numbers like '%${numbers}%'
</if>
</where>
limit #{start},#{size}
</select>
<select id="buildCount" resultType="int">
select count(id) from building
<where>
<if test="numbers!=null and numbers!='null'">
and numbers like '%${numbers}%'
</if>
</where>
</select>
<insert id="insertBuilding">
insert into building(numbers,uints)values (#{numbers},#{uints})
</insert>
<update id="updateBuilding">
update building set numbers=#{numbers},uints=#{uints} where id=#{id}
</update>
<delete id="deleteBuilding">
delete from building where id=#{id}
</delete>
<select id="selectById" resultMap="buildMap">
select * from building where id=#{id}
</select>
</mapper>
二前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">'
<title>Title</title>
<link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/right.css" rel="stylesheet">
<script src="assets/jquery-3.5.1.min.js"></script>
<script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="assets/vue.min-v2.5.16.js"></script>
<script src="assets/vue-router.min-2.7.0.js"></script>
<script src="assets/axios.min.js"></script>
</head>
<body>
<div id="app" class="container">
<div class="row">
<div class="col-md-12" style="height: 30px; line-height: 30px;">
<input type="text" v-model="numbers">
<button class="btn btn-danger" @click="doLikeQuery()">查询</button>
<button class="btn btn-info" @click="doAdd">新增</button>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<caption>楼栋管理</caption>
<thead>
<tr>
<th>ID</th>
<th>楼栋号</th>
<th>单元号</th>
<th>备注</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="b in buildings">
<td>{{b.id}}</td>
<td>{{b.numbers}}</td>
<td>{{b.uints}}</td>
<td>{{b.remarks}}</td>
<td>
<button class="btn btn-link" @click="update(b.id)">修改</button>
<button class="btn btn-link" @click="doDelete(b.id)">删除</button>
</td>
</tr>
</tbody>
</table>
<ul class="pagination" v-for="p in pageNum">
<li v-if="p==pageIndex" class="active"><a @click="doGO(p)">{{p}}</a></li>
<li v-else="p==pageIndex"><a @click="doGO(p)">{{p}}</a></li>
</ul>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
buildings:null,
pageIndex:1,//当前页码
pageSize:5,//每显示的条数
pageTotal:0,//总条数
pageNum:0,//分页
numbers:null,
},
methods: {
doAdd(){
window.parent.main_right.location.href = "building_add_update.html";
},
update(id){
window.parent.main_right.location.href = "building_add_update.html?id="+id;
},
doDelete(id){
var url="http://localhost:8080/building/delete?id="+id;
axios.get(url).then(response=>{
console.log(response.data)
if (response.data.code==200){
var url="http://localhost:8080/building/list?pageIndex=1&pageSize="+this.pageSize;
this.requestListBuilding(url);
}else{
alert(response.data.msg)
}
})
},
doLikeQuery(){
this.doGO(1);
},
requestListBuilding(url){
axios.get(url).then(response=>{
console.log(response.data);
this.buildings=response.data.data;//用户列表
this.pageTotal=response.data.pageTotal;//总条数
this.pageNum=Math.ceil(this.pageTotal / this.pageSize);//计算页数
})
},
doGO(p){
this.pageIndex=p;
var url="http://localhost:8080/building/list?pageIndex="+p+"&pageSize="+this.pageSize+"&numbers="+this.numbers;
this.requestListBuilding(url);
},
},
created: function () {
var url="http://localhost:8080/building/list?pageIndex="+this.pageIndex+"&pageSize="+this.pageSize;
this.requestListBuilding(url);
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/right.css" rel="stylesheet">
<script src="assets/jquery-3.5.1.min.js"></script>
<script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="assets/vue.min-v2.5.16.js"></script>
<script src="assets/vue-router.min-2.7.0.js"></script>
<script src="assets/axios.min.js"></script>
<script src="assets/date_picker.js"></script>
</head>
<body>
<div id="app" class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="row">
<div class="col-md-12" style="text-align: center; font-weight: bold; font-size: 18px; height: 80px; line-height: 80px;">
{{title}}
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3" style="height: 150px;">
<label>楼栋号</label>
<input type="text" class="form-control" v-model="numbers">
<label>单元号</label>
<input type="text" class="form-control" v-model="uints">
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3" style="height: 80px;">
<button class="btn btn-primary" @click="doSave">保存</button>
<button class="btn btn-default" @click="doCancel">取消</button>
</div>
</div>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
title: null,
buildingId:null,
numbers:null,
uints:null,
},
methods: {
doSave(){
if (this.buildingId==null){
this.title="添加用户";
var url="http://localhost:8080/building/add?numbers="+this.numbers+"&uints="+this.uints;
console.log(url)
axios.get(url).then(response=>{
if (response.data.code==200){
window.parent.main_right.location.href = "building_list.html";
}else{
alert(response.data.msg)
}
})
}else{
this.title="修改用户";
var url="http://localhost:8080/building/update?numbers="+this.numbers+"&uints="+this.uints+"&id="+this.buildingId;
axios.get(url).then(response=>{
if (response.data.code==200){
window.parent.main_right.location.href = "building_list.html";
}else{
alert(response.data.msg)
}
})
}
},
doCancel(){
//直接返回上级页面
history.go(-1);
},
},
created: function () {
var url=window.location.href
if (url.indexOf("id")!=-1){
this.buildingId=url.substring(url.indexOf("=")+1)
}
if (this.buildingId==null){
this.title="添加用户";
}else{
this.title="修改用户";
var url="http://localhost:8080/building/info?id="+this.buildingId;
axios.get(url).then(response=>{
this.numbers=response.data.data.numbers;
this.uints=response.data.data.uints;
})
}
}
});
</script>
</body>
</html>