使用excel文件導(dǎo)入數(shù)據(jù),整合mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)
環(huán)境參數(shù)
實(shí)現(xiàn)步驟如下:
1.添加依賴(lài)
dependency> groupId>org.springframework.boot/groupId> artifactId>spring-boot-starter-web/artifactId> /dependency> dependency> groupId>org.springframework.boot/groupId> artifactId>spring-boot-starter-data-mongodb/artifactId> /dependency> dependency> groupId>org.projectlombok/groupId> artifactId>lombok/artifactId> optional>true/optional> /dependency> !-- excel工具 --> dependency> groupId>org.apache.poi/groupId> artifactId>poi-ooxml/artifactId> version>4.0.1/version> /dependency> dependency> groupId>org.springframework.boot/groupId> artifactId>spring-boot-starter-test/artifactId> scope>test/scope> exclusions> exclusion> groupId>org.junit.vintage/groupId> artifactId>junit-vintage-engine/artifactId> /exclusion> /exclusions> /dependency> dependency> groupId>junit/groupId> artifactId>junit/artifactId> scope>test/scope> /dependency>
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ù) ListAttend> attendList = new ArrayListAttend>(); //讀取文件對(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"; } /** * 全查詢(xún)學(xué)生信息 * @param sign * @return */ @Override public ListAttend> 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)來(lái)解析數(shù)據(jù) * System.out.println("接收前臺(tái)表單提交數(shù)據(jù):"+classes+nameListExcel); */ String result = attendService.upload(classes,nameListExcel); return result; } /** * 查詢(xún)未簽到同學(xué) 和已簽到同學(xué) * @return */ @GetMapping("/list") public Map list(){ Map result = new HashMapString,Object>(); /** * 已簽到 */ ListAttend> complete = attendService.findAllBySign(1); result.put("complete",complete); /** * 未簽到 */ ListAttend> 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
代碼如下
!DOCTYPE html> html lang="en"> head> meta charset="UTF-8"> title>考勤管理頁(yè)面/title> style> #complete,#incomplete{ width: 50%; float: left; } /style> script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" type="text/javascript" charset="utf-8">/script> /head> body> h3>導(dǎo)入名單/h3> 班級(jí)名稱(chēng): input type="text" name="classes" id="classes"/> 請(qǐng)選擇導(dǎo)入文件 input type="file" name="nameList" id="nameList"/> input type="button" id="upload" value="上傳"> hr/> div id="incomplete"> h3>未簽到的/h3> p>/p> /div> div id="complete"> h3>已簽到/h3> p>/p> /div> /body> script type="text/javascript"> $(function () { //初始化頁(yè)面查詢(xún)結(jié)果 $.ajax({ type:"get", url:"/list", success:function(data){ console.log(data); var complete =""; var incomplete =""; $.each(data.complete,function (index,object) { complete += object.id +"nbsp;" +object.name +"br/>"; }) $("#complete p").html(complete); $.each(data.incomplete,function (index,object) { incomplete += object.id +"nbsp;" +object.name +"br/>"; }) $("#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)重新上傳") } } }); }) }) /script> /html>
簽到打卡代碼如下:
8.sign-in.html
!DOCTYPE html> html lang="en"> head> meta charset="UTF-8"> title>簽到頁(yè)面/title> script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" type="text/javascript" charset="utf-8">/script> /head> body> 請(qǐng)輸入你的姓名:input type="text" id="name"/> input type="button" id="sign" value="簽到"/> /body> script type="text/javascript"> $(function () { $("body").on("click","#sign",function(){ $.ajax({ type:"get", url:"/sign", data:{"name":$("#name").val()}, success:function(data){ console.log(data); if(data=="ok"){ alert("簽到成功,返回簽到頁(yè)面") //刷新當(dāng)前頁(yè)面 location.reload(); }else { alert("簽到成功,請(qǐng)重新簽到") } } }); }) }) /script> /html>
list.html頁(yè)面效果圖
工作表效果圖
遠(yuǎn)程工具查詢(xún)剛導(dǎo)入的數(shù)據(jù)如下 數(shù)據(jù)后面有包的路徑是因?yàn)閷?dǎo)入數(shù)據(jù)的時(shí)候沒(méi)有添加mongodb配置類(lèi),添加了就沒(méi)有了。
添加配置類(lèi)之后的效果圖
注意:導(dǎo)入excel文件(xsl工作表)的時(shí)候使用2003之前版本的,后綴帶XLS。
有哪里不明白的地方記得下方留言哦。
項(xiàng)目已托管碼云
地址:https://gitee.com/ckfeng/mongodb_punch.git
總結(jié)
到此這篇關(guān)于使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的文章就介紹到這了,更多相關(guān)使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:楚雄 白山 菏澤 呼倫貝爾 黃石 池州 安順 舟山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的實(shí)例代碼》,本文關(guān)鍵詞 使用,Mongodb,實(shí)現(xiàn),打卡,簽到,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。