第一行代码android网课,使用Mongodb实现打卡签到系统的实例代码
使用excel文件導(dǎo)入數(shù)據(jù),整合mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)
環(huán)境參數(shù)
開(kāi)發(fā)工具:IDEA
基礎(chǔ)環(huán)境:Maven+JDK8
主要技術(shù):SpringBoot、Mongodb
SpringBoot版本:2.2.6
實(shí)現(xiàn)步驟如下:
1.添加依賴
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-mongodb
org.projectlombok
lombok
true
org.apache.poi
poi-ooxml
4.0.1
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
junit
junit
test
2.實(shí)體層
3.業(yè)務(wù)service層
4. service實(shí)現(xiàn)層
package com.ckf.mongodb_punch.service.impl; import com.ckf.mongodb_punch.mapper.AttendRepository; import com.ckf.mongodb_punch.entity.Attend; import com.ckf.mongodb_punch.service.AttendService; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Service public class AttendServiceImpl implements AttendService { @Autowired private AttendRepository attendRepository; @Autowired private MongoTemplate mongoTemplate; /** * 上傳文件 * @param classes * @param nameListExcel * @return */ @Override public String upload(String classes, MultipartFile nameListExcel) { String result = "no"; if (nameListExcel == null) { return result; } //實(shí)例化對(duì)象列表,用于存儲(chǔ)Excel中的數(shù)據(jù)
List attendList = new ArrayList(); //讀取文件對(duì)象nameListExcel 中的數(shù)據(jù)(讀取Excel中每一行數(shù)據(jù),存到對(duì)象,存到對(duì)象列表中)
try { //根據(jù)路徑獲取這個(gè)操作excel的實(shí)例
HSSFWorkbook wb = new HSSFWorkbook(nameListExcel.getInputStream()); //根據(jù)頁(yè)面index 獲取sheet頁(yè)
HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = null; //循環(huán)sesheet頁(yè)中數(shù)據(jù)從第二行開(kāi)始,第一行是標(biāo)題
for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) { //獲取每一行數(shù)據(jù)
row = sheet.getRow(i); Attend attend = new Attend(); //下面cellnum對(duì)應(yīng)著下標(biāo),id是第一位對(duì)應(yīng)著下標(biāo)為0,name是第二位對(duì)應(yīng)的下標(biāo)為1,等等..
attend.setId(Integer.valueOf((int) row.getCell(0).getNumericCellValue())); attend.setName(row.getCell(1).getStringCellValue()); attend.setSign(Integer.valueOf((int) row.getCell(2).getNumericCellValue())); attendList.add(attend); } } catch (IOException e) { e.printStackTrace(); } System.out.println("解析Excel中的數(shù)據(jù):" + attendList); /** * 如果成功就,寫(xiě)入mongodb中 */ attendRepository.saveAll(attendList); result = "ok"; return result; } /** * 簽到 * @param name * @return */ @Override public String sign(String name) { Query query = Query.query(Criteria.where("name").is(name)); //局部修改的內(nèi)容
Update update = new Update(); update.set("sign", 1); //attend 集合名 對(duì)應(yīng)實(shí)體的集合名
mongoTemplate.updateFirst(query, update, "attend"); return "ok"; } /** * 全查詢學(xué)生信息 * @param sign * @return */ @Override public List findAllBySign(Integer sign) { return attendRepository.findAllBySign(sign); } }
5.controller層
package com.ckf.mongodb_punch.controller;
import com.ckf.mongodb_punch.entity.Attend;
import com.ckf.mongodb_punch.service.AttendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap; import java.util.List;
import java.util.Map;
@RestController public class AttendController
{
@Autowired private AttendService attendService;
@GetMapping("/sign")
public String sign(String name)
{ /** * 將名字傳給服務(wù)層,mongodb修改登錄狀態(tài)
*/ attendService.sign(name); return "ok";
}
/** * 上傳文件 * @param classes * @param nameListExcel * @return
*/ @PostMapping("/upload")
public String upload(String classes, MultipartFile nameListExcel)
{
/** * 接收到前臺(tái)傳過(guò)來(lái)的文件對(duì)象,交給service層或者Excel工具類來(lái)解析數(shù)據(jù)
* System.out.println("接收前臺(tái)表單提交數(shù)據(jù):"+classes+nameListExcel);
*/ String result = attendService.upload(classes,nameListExcel);
return result;
}
/** * 查詢未簽到同學(xué) 和已簽到同學(xué)
* @return */ @GetMapping("/list")
public Map list(){ Map result = new HashMap(); /** * 已簽到 */ List
complete = attendService.findAllBySign(1);
result.put("complete",complete); /** * 未簽到 */ List
incomplete = attendService.findAllBySign(0);
result.put("incomplete",incomplete);
return result;
}
}
6.application.yml
這里使用的是mongodb的安全認(rèn)證配置
spring:
data:
mongodb:
uri:
mongodb://ckf_user:123456@192.168.85.154:27017/attend_db
默認(rèn)單例配置如下
spring:
data:
mongodb:
uri:
mongodb://localhost:27017/attend_db
這里使用的是異步實(shí)現(xiàn)的
7.list.html
代碼如下
考勤管理頁(yè)面導(dǎo)入名單
班級(jí)名稱: 請(qǐng)選擇導(dǎo)入文件未簽到的
已簽到
$.ajax({ type:"get", url:"/list", success:function(data){ console.log(data); var complete =""; var incomplete =""; $.each(data.complete,function (index,object) { complete += object.id +"?" +object.name +"
"; }) $("#complete p").html(complete); $.each(data.incomplete,function (index,object) { incomplete += object.id +"?" +object.name +"
"; }) $("#incomplete p").html(incomplete); } }); $("body").on("click","#upload",function(){ //將數(shù)據(jù)打包到formData對(duì)象中
var formData = new FormData(); formData.append("classes",$("#classes").val()); formData.append("nameListExcel",$("#nameList")[0].files[0]); $.ajax({ type:"post", url:"/upload", //dataType:"json",
data:formData, processData: false, contentType: false, success:function(data){ console.log(data); if(data=="ok"){ alert("上傳成功,即將刷新頁(yè)面") //刷新當(dāng)前頁(yè)面
location.reload(); }else { alert("上傳失敗,請(qǐng)重新上傳") } } }); }) })
簽到打卡代碼如下:
8.sign-in.html
簽到頁(yè)面location.reload(); }else { alert("簽到成功,請(qǐng)重新簽到") } } }); }) })
list.html頁(yè)面效果圖
工作表效果圖
遠(yuǎn)程工具查詢剛導(dǎo)入的數(shù)據(jù)如下 數(shù)據(jù)后面有包的路徑是因?yàn)閷?dǎo)入數(shù)據(jù)的時(shí)候沒(méi)有添加mongodb配置類,添加了就沒(méi)有了。
添加配置類之后的效果圖
注意:導(dǎo)入excel文件(xsl工作表)的時(shí)候使用2003之前版本的,后綴帶XLS。
有哪里不明白的地方記得下方留言哦。
項(xiàng)目已托管碼云
總結(jié)
到此這篇關(guān)于使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的文章就介紹到這了,更多相關(guān)使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)內(nèi)容請(qǐng)搜索我們以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持我們!
時(shí)間: 2020-05-05
總結(jié)
以上是生活随笔為你收集整理的第一行代码android网课,使用Mongodb实现打卡签到系统的实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vue双向绑定
- 下一篇: java 数据库工资管理系统设计_数据库