使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的實(shí)例代碼
使用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ù)
List<Attend> attendList = new ArrayList<Attend>(); //讀取文件對象nameListExcel 中的數(shù)據(jù)(讀取Excel中每一行數(shù)據(jù),存到對象,存到對象列表中)
try { //根據(jù)路徑獲取這個操作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 List<Attend> 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 HashMap<String,Object>(); /** * 已簽到 */ List<Attend>
complete = attendService.findAllBySign(1);
result.put("complete",complete); /** * 未簽到 */ List<Attend>
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 +" " +object.name +"<br/>"; }) $("#complete p").html(complete); $.each(data.incomplete,function (index,object) { incomplete += object.id +" " +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ù)的時候沒有添加mongodb配置類,添加了就沒有了。

添加配置類之后的效果圖

注意:導(dǎo)入excel文件(xsl工作表)的時候使用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動態(tài)條件之分頁查詢
- MongoDB設(shè)計方法以及技巧示例詳解
- MongoDB數(shù)據(jù)庫基礎(chǔ)操作總結(jié)
- express+mongoose實(shí)現(xiàn)對mongodb增刪改查操作詳解
- win7平臺快速安裝、啟動mongodb的方法
- 淺析MongoDB 全文檢索
相關(guān)文章
MongoDB 學(xué)習(xí)筆記(一)-MongoDB配置
MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。接下來通過本文給大家介紹MongoDB 學(xué)習(xí)筆記(一)MongoDB配置的相關(guān)資料,需要的朋友可以參考下2016-05-05
mongodb主從復(fù)制_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了mongodb主從復(fù)制的相關(guān)資料,討論mongodb的部署技術(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
MongoDB查詢性能優(yōu)化驗(yàn)證及驗(yàn)證
這篇文章主要介紹了MongoDB查詢性能驗(yàn)證及優(yōu)化的相關(guān)知識,涉及到MongoDB 查詢優(yōu)化原則知識點(diǎn),本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
MongoDB特點(diǎn)與體系結(jié)構(gòu)等簡介
今天小編就為大家分享一篇關(guān)于MongoDB特點(diǎn)與體系結(jié)構(gòu)等簡介,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
MongoDB添加仲裁節(jié)點(diǎn)報錯:replica set IDs do not match的解決方法
這篇文章主要給大家介紹了關(guān)于MongoDB添加仲裁節(jié)點(diǎn)報錯:replica set IDs do not match的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
MongoDB運(yùn)行日志實(shí)現(xiàn)自動分割的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于MongoDB運(yùn)行日志實(shí)現(xiàn)自動分割的方法,文中以一個MongoDB實(shí)例為例,寫了一個腳本來實(shí)現(xiàn)自動分割MongoDB日志,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01

