最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的實(shí)例代碼

 更新時(shí)間:2020年05月07日 09:19:10   作者:admin  
這篇文章主要介紹了使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

使用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í)例化對(duì)象列表,用于存儲(chǔ)Excel中的數(shù)據(jù)
  List<Attend> attendList = new ArrayList<Attend>(); //讀取文件對(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ù)從第二行開始,第一行是標(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); /** * 如果成功就,寫入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<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)
{
/** * 接收到前臺(tái)傳過來(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<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>考勤管理頁(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í)名稱: <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è)面查詢結(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)程工具查詢剛導(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mongodb實(shí)現(xiàn)數(shù)組對(duì)象求和方法實(shí)例

    mongodb實(shí)現(xiàn)數(shù)組對(duì)象求和方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于mongodb實(shí)現(xiàn)數(shù)組對(duì)象求和的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • MongoDB數(shù)據(jù)庫(kù)性能監(jiān)控詳解

    MongoDB數(shù)據(jù)庫(kù)性能監(jiān)控詳解

    MongoDB作為圖片和文檔的存儲(chǔ)數(shù)據(jù)庫(kù),為啥不直接存MySQL里,還要搭個(gè)MongoDB集群,麻不麻煩?這篇文章就帶你介紹MongoDB數(shù)據(jù)庫(kù)性能監(jiān)控,感興趣的同學(xué)可以參考閱讀
    2023-03-03
  • ubuntu mongodb安裝在哪個(gè)文件夾路徑詳解

    ubuntu mongodb安裝在哪個(gè)文件夾路徑詳解

    這篇文章主要為大家介紹了ubuntu mongodb安裝在哪個(gè)文件夾的安裝路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Mongodb?刪除文檔Delete與Remove的區(qū)別解析

    Mongodb?刪除文檔Delete與Remove的區(qū)別解析

    這篇文章主要介紹了Mongodb?刪除文檔Delete與Remove的區(qū)別,要從集合中刪除所有文檔,請(qǐng)將空過濾器文檔傳遞{}給該?db.collection.deleteMany()方法,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Mac中mongoDB的安裝與卸載步驟詳解

    Mac中mongoDB的安裝與卸載步驟詳解

    mongoDB是一個(gè)非常不錯(cuò)的數(shù)據(jù)庫(kù),最近也正在學(xué)習(xí)mongoDB,在使用一個(gè)數(shù)據(jù)庫(kù)前必不可少的就是安裝和卸載,所以下面這篇文章主要給大家介紹了關(guān)于Mac系統(tǒng)中mongoDB安裝與卸載的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-10-10
  • MongoDB中多表關(guān)聯(lián)查詢($lookup)的深入講解

    MongoDB中多表關(guān)聯(lián)查詢($lookup)的深入講解

    NoSql的多表關(guān)聯(lián)一直是比較復(fù)雜的問題,下面這篇文章主要給大家介紹了關(guān)于MongoDB中多表關(guān)聯(lián)查詢($lookup)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-12-12
  • Mongodb的oplog詳解

    Mongodb的oplog詳解

    這篇文章主要介紹了Mongodb的oplog詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • MongoDB中的定時(shí)索引示例詳解

    MongoDB中的定時(shí)索引示例詳解

    這篇文章主要給大家介紹了關(guān)于MongoDB中定時(shí)索引的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MongoDB具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • MongoDB balancer的使用詳解

    MongoDB balancer的使用詳解

    這篇文章主要介紹了MongoDB balancer的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用MongoDB,感興趣的朋友可以了解下
    2021-04-04
  • MongoDB的基礎(chǔ)查詢和索引操作方法總結(jié)

    MongoDB的基礎(chǔ)查詢和索引操作方法總結(jié)

    MongoDB使用JavaScript作為shell腳本,可以代替關(guān)系型數(shù)據(jù)庫(kù)中的SQL語(yǔ)句完成查詢操作,包括索引下的查詢操作,這里我們就來(lái)整理MongoDB的基礎(chǔ)查詢和索引操作方法總結(jié):
    2016-07-07

最新評(píng)論

普兰县| 泽库县| 健康| 闵行区| 孟连| 阿图什市| 公主岭市| 镇赉县| 门头沟区| 临泉县| 阜城县| 威信县| 孝感市| 阿巴嘎旗| 板桥市| 镇坪县| 林州市| 蓝田县| 博罗县| 北安市| 纳雍县| 陵川县| 庐江县| 扶绥县| 丰台区| 汉寿县| 会理县| 青海省| 华容县| 炉霍县| 新晃| 古丈县| 晋宁县| 淳化县| 随州市| 普宁市| 合江县| 吴旗县| 通辽市| 彭泽县| 凭祥市|