概要
随着时代变化,有的需求也会越来越多,例如:有的文件上千条数据需要导入数据库不可能手动一条条导入吧?太浪费时间了!所以需要编写程序让程序来导入
整体架构流程
我这里使用的是springboot+mybatis+MVC的项目架构使用的poi实现的导入功能
技术细节
前端代码:
后端代码:
这是接收创建临时表然后先将前端的数据存入到临时表内然后比对临时表中有无相同的数据,有的话将一个字段设为标识字段然后对此条数据进行标识,点击保存的时候比对完的临时表再去和实体的表再次比对将共同数据排除将新数据存入实际的表内
这是创建临时表之前先删除临时表,两条sql可以写成一条的但是MariaDB的写法比较严谨我尝试了但是不行就分开写了,创建临时表时要多写两个字段用于进行数据的标识,标识完成就可以在临时表里导入了
以下代码是导入临时表,这里是在实体类的pom文件内导入了poi的依赖引用poi的方法进行获取文件判断类型获取数据对数据进行各种操作
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
<scope>compile</scope>
</dependency>
这是保存的前端,保存完之后需要查询 我这里没有这个业务所以后面代码注释掉了
//
$('#save').on('click', function(){
// let saveHouseLoading = layer.msg('正在保存房屋信息,请不要关闭浏览器!!!', {icon: 16, shade: 0.3, time:0});
$.ajax({
url:'companyManage/addSave',
type:'post',
data: {'regionlId':comId ? comId : regionlId},
success:function (res) {
alert(res)
// if (res=="导入成功!"){
// alert(res)
// admin.closeDialog('#improtHousePage');
// $.ajax({
// url: '/heatUser/importInformation/findProblemInformation',
// type: 'post',
// success: function (res) {
// console.log(res)
// console.log(res.length)
// // if (res.length>0){
// // var data = res;
// // data = excel.filterExportData(res, [
// // 'villageName',
// // 'buildingName',
// // 'unitName',
// // 'house',
// // 'alipayConsNo',
// // 'location',
// // 'floor',
// // 'holder',
// // 'idCard',
// // 'holdPhone',
// // 'houseType',
// // 'isElevator',
// // 'heatMode',
// // 'householdType',
// // 'buildingArea',
// // 'publicArea',
// // 'insideArea',
// // 'areaAdjust',
// // 'adjustReason',
// // 'zhdwnhenergy',
// // 'zhcrxsHeat',
// // 'zhgswdtemperature',
// // 'zhhswdtemperature',
// // 'cases',
// // ]);
// // data.unshift({
// // villageName: '小区名称'
// // , buildingName: '楼号'
// // , unitName: '单元'
// // , house: '室号'
// // , alipayConsNo: '住户卡号'
// // , location: '位置'
// // , floor: '所在楼层'
// // , holder: '户主姓名'
// // , idCard: '身份证号'
// // , holdPhone: '联系电话'
// // , houseType: '房屋类别'
// // , isElevator: '是否电梯楼'
// // , heatMode: '采暖方式'
// // , householdType: '房屋属性'
// // , buildingArea: '建筑面积'
// // , publicArea: '公摊面积'
// // , insideArea: '套内面积'
// // , areaAdjust: '面积修正'
// // , adjustReason: '修正原因'
// // , zhdwnhenergy: '单位能耗w/㎡'
// // , zhcrxsHeat: '传热系数w/(㎡·k)'
// // , zhgswdtemperature: '供水温度℃'
// // , zhhswdtemperature: '回水温度℃'
// // , cases: '失败原因'
// //
// // });
// // excel.exportExcel(data, '问题房屋信息' + '.xlsx', 'xlsx');
// // }else {
// // return;
// // }
// },
// error: function () {
// alert("报错!");
// }
// });
// }else {
// alert("出现问题,请联系管理员!")
// }
}
});
});
//将导入的信息进行保存
@RequestMapping("/addSave")
public String addSave(HttpServletRequest request,Integer regionlId) throws NoLoginException {
// HttpSession session = MyHttpServletRequest.getSession(request);
if (regionlId == null) {
throw new NoLoginException("未登录");
}
Integer companyId = regionlId;
String com= "house"+companyId;
String ins= "inspect"+companyId;
String returns=testservice.isOverImport(com,ins,companyId);
return returns;
}
String isOverImport(String com, String ins, Integer companyId);
@Override
public String isOverImport(String com, String ins, Integer companyId) {
int countyID = importHouseAndUnitInformationMapper.selectCitylevel();
importHouseAndUnitInformationMapper.updateOnlyOne(com);//查看当前表格是否存在重复的房屋信息,并进行标识
importHouseAndUnitInformationMapper.updateHaving(com);//查看houseDeatil表中是否存在数据,如果没有就把临时表中的isHaving改为0
importHouseAndUnitInformationMapper.insertTestHavingIs0(com);//将确认好的数据导入到houseDetail中
return "导入成功!";
}
/**
* 当信息表中只有这一条数据,没有重复的相同数据则进行标识
*/
@Update(value = "UPDATE temporary.`${name}` a\n" +
"LEFT JOIN (\n" +
" SELECT cylinderLabelNum, COUNT(*) AS count\n" +
" FROM temporary.`${name}`\n" +
" WHERE cylinderLabelNum IS NOT NULL\n" +
" GROUP BY cylinderLabelNum\n" +
") b ON a.cylinderLabelNum = b.cylinderLabelNum\n" +
"SET a.count = b.count;")
void updateOnlyOne (@Param("name") String name);
/**
* 将没有信息的标识为0,代表房屋信息里边没有,接下来导入的就是这些
*/
@Update(value = " UPDATE temporary.`${name}` AS a\n" +
"LEFT JOIN dkgovtgassystem.`lpgcylinder` b ON a.cylinderLabelNum = b.cylinderLabelNum\n" +
"SET a.isHaving = CASE WHEN b.cylinderLabelNum IS NULL THEN 0 ELSE 1 END; ")
void updateHaving (@Param("name") String com);
//查询ishaving=0的信息,这些信息是可以导入到房屋信息表中的
@Insert(value = " INSERT INTO dkgovtgassystem.`lpgcylinder` (productionName, productionUnit, productionDate, cylinderLabelNum, volume, maxVolume, lastInspectionTime, nextInspectionTime, pressureNum, remark, companyID,countyID,cityID ) SELECT\n" +
"productionName,\n" +
"productionUnit,\n" +
"productionDate,\n" +
"cylinderLabelNum,\n" +
"volume,\n" +
"maxVolume,\n" +
"lastInspectionTime,\n" +
"nextInspectionTime,\n" +
"pressureNum,\n" +
"remark, \n" +
"companyId, \n" +
"countyID, \n" +
"cityID \n" +
"FROM\n" +
"\t temporary.`${name}` B \n" +
"WHERE\n" +
"\tB.isHaving = 0 \n" +
"\tAND B.cylinderLabelNum IS NOT NULL \n" +
"\tAND B.count = 1; ")
void insertTestHavingIs0 (@Param("name") String name);
这三条sql分别就是第一次标识临时表相同的数据,第二次临时表和实际表再次进行比对和标识有无相同数据,第三条是将临时表都处理好的数据查询出来并新增到实际表中
小结
文章比较粗糙,这是MariaDB里面的sql的写法,我仿照别人的代码写的,仅供参考但是业务大致是这样只是sql语法不一样,参考时注意即可