package com.songshu.mongo.test; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.songshu.mongo.model.json.JsonString; import com.songshu.mongo.model.dto.BookDTO; import com.songshu.mongo.service.BookMongoService; import com.songshu.mongo.service.StaticScheduleTaskService; import com.songshu.mongo.service.UpdateTingcheService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; 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.test.context.junit4.SpringRunner; import java.util.List; @SpringBootTest @RunWith(SpringRunner.class) public class BookMongoControllerTest { @Autowired private BookMongoService bookMongoService; @Autowired private StaticScheduleTaskService staticScheduleTaskService; @Autowired private UpdateTingcheService updateTingcheService; /** * 测试json转对象(for:拿示例json数据做批量新增) * * @throws JsonProcessingException */ @Test public void jsonToObj() throws JsonProcessingException { String jsonString = JsonString.jsonString; // json转对象集合 List bookDTOList = JSON.parseArray(jsonString, BookDTO.class); System.out.println(bookDTOList); //集合转对象 // String json = new ObjectMapper().writeValueAsString(bookDTOList); } /** * 新增)(单个新增) */ @Test public void insert() { BookDTO bookDTO = new BookDTO(); bookDTO.setId("5"); bookDTO.setName("万界圣主"); bookDTO.setPrice("17.8"); bookDTO.setType("仙侠"); bookDTO.setReadedNum(Double.parseDouble("10.7")); bookMongoService.inserBookInfo(bookDTO); } /** * 新增(批量新增) */ @Test public void insertBatch() { // 拿到示例json数据 String jsonString = JsonString.jsonString; // json转对象集合 List bookDTOList = JSON.parseArray(jsonString, BookDTO.class); bookMongoService.inserBatchBookInfo(bookDTOList); } /** * 修改 */ @Test public void update() { // 条件 Query query = Query.query(Criteria.where("_id").is("2")); // 修改内容 Update update = Update.update("price", "19.9"); bookMongoService.updateBookInfo(query, update); } /** * 查询所有 */ @Test public void findAll() { List allBookInfo = bookMongoService.findAllBookInfo(); System.out.println(allBookInfo); } /** * 根据书名查询:精准 */ @Test public void findByName() { String name = "盗墓笔记"; List allBookInfo = bookMongoService.findBookInfoByName(name); System.out.println(allBookInfo); } /** * 根据书名查询: 模糊查 */ @Test public void findReName() { String name = "主"; List allBookInfo = bookMongoService.findBookInfoReName(name); System.out.println(allBookInfo); } /** * 根据阅读量:范围查询,排序 */ @Test public void findReRange() { // 注意,这里范围查询得时候,直接用作字符串去查,并不会把字符串里面得数字提取出来然后做范围查询,所以这里改成Double,改成Long坑太多. Double minReadedNum = Double.parseDouble("8.3"); Double maxReadedNum = Double.parseDouble("18.0"); List allBookInfo = bookMongoService.findBookInfoReRange(minReadedNum, maxReadedNum); System.out.println(allBookInfo); } /** * 根据阅读量:范围查询,排序,分页(页码是物理下标,入数是逻辑下标) */ @Test public void findPage() { // 注意,这里范围查询得时候,直接用作字符串去查,并不会把字符串里面得数字提取出来然后做范围查询,所以这里改成Double,改成Long坑太多. Double minReadedNum = Double.parseDouble("7.0"); Double maxReadedNum = Double.parseDouble("35.9"); Integer pageNum = 2; Integer pageSize = 2; List allBookInfo = bookMongoService.findBookInfoPage(minReadedNum, maxReadedNum, pageNum, pageSize); System.out.println(allBookInfo); } // 高级查询待写........ /** * 测试用 */ @Test public void findAll3() { // staticScheduleTaskService.SynchronousDataRuildings(); // staticScheduleTaskService.SynchronousDataContractsRoom(); // staticScheduleTaskService.SynchronousDataRuildings(); // staticScheduleTaskService.SynchronousDataRoom(); // staticScheduleTaskService.SynchronousDataContracts(); // staticScheduleTaskService.SynchronousDataAccounts(); // staticScheduleTaskService.SynchronousDataContractReceipts(); // staticScheduleTaskService.SynchronousDataContractAuditRecords(); // List allBookInfo = bookMongoService.findAllBookInfo3(); // // updateTingcheService.updateTjTcsjtjn(); // updateTingcheService.updateTjCljrsctjn(); // updateTingcheService.updateTjCljrsctjy(); // updateTingcheService.updateTjCljrsctjr(); // updateTingcheService.updateTjJrcrktcsj(); 不用了 // updateTingcheService.updateTjParkingPayment(); // updateTingcheService.updateTjVehicleRecords(); } }