In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What the editor wants to share with you this time is how to achieve the Mongodb sign-in system. The article is rich in content. Interested friends can learn about it. I hope you can get something after reading this article.
Use excel file to import data and integrate mongodb to realize sign-in system
Environmental parameters
Development tools: IDEA basic environment: Maven+JDK8 main technologies: SpringBoot, MongodbSpringBoot version: 2.2.6
The implementation steps are as follows:
1. Add dependency
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
two。 Physical layer
3. Business service layer
4. Service implementation layer
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 / * upload file * @ param classes * @ param nameListExcel * @ return * / @ Override public String upload (String classes, MultipartFile nameListExcel) {String result = "no"; if (nameListExcel = = null) {return result;} / / instantiated object list, which is used to store data in Excel List attendList = new ArrayList () / / read the data in the file object nameListExcel (read each row of data in the Excel, save it to the object list) try {/ / get the instance of this operation excel according to the path HSSFWorkbook wb = new HSSFWorkbook (nameListExcel.getInputStream ()); / / get the sheet page HSSFSheet sheet = wb.getSheetAt (0) according to the page index; HSSFRow row = null; / / the data in the circular sesheet page starts from the second line, and the first line is the title for (int I = 1) I < sheet.getPhysicalNumberOfRows (); iTunes +) {/ / get each row of data row = sheet.getRow (I); Attend attend = new Attend (); / / below cellnum corresponds to the subscript, id is the first subscript corresponding to 0, and the second subscript is 1, and so on. 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 ("parsing data in Excel:" + attendList) / * if successful, write * / attendRepository.saveAll (attendList) to mongodb; result = "ok"; return result;} / * * check in * @ param name * @ return * / @ Override public String sign (String name) {Query query = Query.query (Criteria.where ("name") .is (name)); / / locally modified content Update update = new Update (); update.set ("sign", 1) / / the collection name of the entity corresponding to the attend collection name mongoTemplate.updateFirst (query, update, "attend"); return "ok";} / * * full query student information * @ param sign * @ return * / @ Override public List findAllBySign (Integer sign) {return attendRepository.findAllBySign (sign);}}
5.controller layer
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) {/ * pass the name to the service layer, mongodb modify the login status * / attendService.sign (name); return "ok" } / * upload file * @ param classes * @ param nameListExcel * @ return * / @ PostMapping ("/ upload") public String upload (String classes, MultipartFile nameListExcel) {/ * receive the file object from the foreground and send it to the service layer or Excel utility class to parse the data * System.out.println ("receive foreground form submission data:" + classes+nameListExcel); * / String result = attendService.upload (classes,nameListExcel); return result } / * query unchecked-in students and checked-in students * @ return * / @ GetMapping ("/ list") public Map list () {Map result = new HashMap (); / * * checked-in * / List complete = attendService.findAllBySign (1); result.put ("complete", complete); / * * not checked-in * / List incomplete = attendService.findAllBySign (0); result.put ("incomplete", incomplete); return result;}}
6.application.yml
Mongodb's security authentication configuration is used here.
Spring: data: mongodb: uri: mongodb://ckf_user:123456@192.168.85.154:27017/attend_db
The default singleton configuration is as follows
Spring: data: mongodb: uri:mongodb://localhost:27017/attend_db
The asynchronous implementation is used here.
7.list.html
The code is as follows
Attendance management page # complete,#incomplete {width: 50%; float: left;} Import list Class name: please select the import file that is not checked in
Signed in
$(function () {/ / initialize page query results $.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 () {/ / package data into formData objects 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 ("page is uploaded successfully") / refresh the current page location.reload () } else {alert ("upload failed, please upload again");})})
The check-in code is as follows:
8.sign-in.html
Please enter your name: $(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 ("check in successfully, return to check in page") / / refresh the current page location.reload ();} else {alert ("check in successfully, please re-check in");})})
List.html page effect picture
Worksheet effect drawing
The remote tool queries the newly imported data as follows: there is a path to the package after the data is imported because there is no mongodb configuration class added when the data is imported.
Effect diagram after adding configuration class
Note: import excel files (xsl worksheets) with versions prior to 2003, with the suffix XLS.
After reading this article about how Mongodb implements the sign-in system, if you think the content of the article is good, you can share it with more people.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.