使用excel文件導(dǎo)入數(shù)據(jù),整合mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)
環(huán)境參數(shù)
- 開發(fā)工具:IDEA
- 基礎(chǔ)環(huán)境:Maven+JDK8
- 主要技術(shù):SpringBoot、Mongodb
- SpringBoot版本:2.2.6
實(shí)現(xiàn)步驟如下:
1.添加依賴
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í)例化對象列表,用于存儲Excel中的數(shù)據(jù)
ListAttend> attendList = new ArrayListAttend>(); //讀取文件對象nameListExcel 中的數(shù)據(jù)(讀取Excel中每一行數(shù)據(jù),存到對象,存到對象列表中)
try { //根據(jù)路徑獲取這個(gè)操作excel的實(shí)例
HSSFWorkbook wb = new HSSFWorkbook(nameListExcel.getInputStream()); //根據(jù)頁面index 獲取sheet頁
HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = null; //循環(huán)sesheet頁中數(shù)據(jù)從第二行開始,第一行是標(biāo)題
for (int i = 1; i sheet.getPhysicalNumberOfRows(); i++) { //獲取每一行數(shù)據(jù)
row = sheet.getRow(i); Attend attend = new Attend(); //下面cellnum對應(yīng)著下標(biāo),id是第一位對應(yīng)著下標(biāo)為0,name是第二位對應(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); /** * 如果成功就,寫入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 集合名 對應(yīng)實(shí)體的集合名
mongoTemplate.updateFirst(query, update, "attend"); return "ok"; } /** * 全查詢學(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)
{
/** * 接收到前臺傳過來的文件對象,交給service層或者Excel工具類來解析數(shù)據(jù)
* System.out.println("接收前臺表單提交數(shù)據(jù):"+classes+nameListExcel);
*/ String result = attendService.upload(classes,nameListExcel);
return result;
}
/** * 查詢未簽到同學(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>考勤管理頁面/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> 班級名稱: input type="text" name="classes" id="classes"/> 請選擇導(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 () { //初始化頁面查詢結(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對象中
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("上傳成功,即將刷新頁面") //刷新當(dāng)前頁面
location.reload(); }else { alert("上傳失敗,請重新上傳") } } }); }) }) /script>
/html>
簽到打卡代碼如下:
8.sign-in.html
!DOCTYPE html>
html lang="en">
head>
meta charset="UTF-8">
title>簽到頁面/title>
script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js" type="text/javascript" charset="utf-8">/script>
/head>
body> 請輸入你的姓名: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("簽到成功,返回簽到頁面") //刷新當(dāng)前頁面
location.reload(); }else { alert("簽到成功,請重新簽到") } } }); }) }) /script>
/html>
list.html頁面效果圖
工作表效果圖
遠(yuǎn)程工具查詢剛導(dǎo)入的數(shù)據(jù)如下 數(shù)據(jù)后面有包的路徑是因?yàn)閷?dǎo)入數(shù)據(jù)的時(shí)候沒有添加mongodb配置類,添加了就沒有了。
添加配置類之后的效果圖
注意:導(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MongoDB實(shí)現(xiàn)基于關(guān)鍵詞的文章檢索功能(C#版)
- 深入了解MongoDB 分布式集群
- 開源 5 款超好用的數(shù)據(jù)庫 GUI 帶你玩轉(zhuǎn) MongoDB、Redis、SQL 數(shù)據(jù)庫(推薦)
- JAVA代碼實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁查詢
- MongoDB設(shè)計(jì)方法以及技巧示例詳解
- MongoDB數(shù)據(jù)庫基礎(chǔ)操作總結(jié)
- express+mongoose實(shí)現(xiàn)對mongodb增刪改查操作詳解
- win7平臺快速安裝、啟動(dòng)mongodb的方法
- 淺析MongoDB 全文檢索