创建选择NoSQL
pom依赖:
<!--mongodb依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Hutool Java工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
连接数据库
application.yml文件配置
#端口
server:
port: 9090
spring:
#连接mongodb数据库
data:
mongodb:
uri: mongodb://logsuser:123456@192.168.230.136:27017/admin_logs?ext.auth.askPassword=true&authSource=admin_logs
#项目名称
application:
name:
开始创建实体类和dao层
Student 实体类
package com.jq.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@Document(collection ="student")//数据库表名 没有会自动创建
public class Student {
@Id
private String id;
private String name;
private Integer sex ;
private Integer age ;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
}
dao层
package com.jq.dao;
import com.jq.model.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
@Repository
//<Student,String> Student为实体类 String为实体类id的数据类型
public interface StudentDao extends MongoRepository<Student,String> {
}
开始测试
package com.jq;
import com.jq.dao.StudentDao;
import com.jq.model.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.Date;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private StudentDao studentDao;
//新增
@Test
void insert() {
Student student = new Student();
student.setName("张三");
student.setAge(20);
student.setSex(1);
student.setBirthday(new Date());
studentDao.insert(student);
}
}
数据库效果:
完整测试
package com.jq;
import cn.hutool.json.JSONUtil;
import com.fh.dao.StudentDao;
import com.jq.model.Student;
import com.jq.util.MongoUtil;
import com.jq.util.PageHelper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private StudentDao studentDao;
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private MongoUtil mongoUtil;
//新增
@Test
void insert() {
Student student = new Student();
student.setName("张三");
student.setAge(20);
student.setSex(1);
student.setBirthday(new Date());
studentDao.insert(student);
}
//修改
@Test
void update(){
Optional<Student> student = studentDao.findById("6093947d674e22145177f48d");
Student student1 = student.get();
student1.setName("王丽");
student1.setSex(21);
student1.setSex(2);
studentDao.save(student1);
}
//删除
@Test
void delet(){
studentDao.deleteById("6093947d674e22145177f48d");
}
//查询所有
@Test
void findAll(){
List<Student> studentList = studentDao.findAll();
System.out.println(JSONUtil.toJsonStr(studentList));
}
//查询所有+排序
@Test
void findAllAndSort(){
Sort sort = Sort.by(Sort.Direction.DESC,"age");
List<Student> studentList = studentDao.findAll(sort);
System.out.println(JSONUtil.toJsonStr(studentList));
}
/**
* 分页查询
*/
@Test
void findByPage() {
// Query query = new Query(Criteria.where("students.name").is("name1").and("project").is("project1"));
Query query = new Query(new Criteria());
// query.with(Sort.by(Sort.Direction.DESC, "time"));
mongoUtil.start(2, 2, query);
List<Student> studentList = mongoTemplate.find(query, Student.class);
long count = mongoTemplate.count(query, Student.class);
PageHelper pageHelper = mongoUtil.pageHelper(count, studentList);
System.out.println(pageHelper);
}
}
工具类
package com.jq.util;
import lombok.Data;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import java.util.regex.Pattern;
import java.util.List;
@Data
@Component
public class MongoUtil <T>{
public Integer pageSize;
private Integer currentPage;
public void start(Integer currentPage, Integer pageSize, Query query) {
pageSize = pageSize == 0 ? 10 : pageSize;
query.limit(pageSize);
query.skip((currentPage - 1) * pageSize);
this.pageSize = pageSize;
this.currentPage = currentPage;
}
public PageHelper pageHelper(long total, List<T> list) {
return new PageHelper(this.currentPage, total, this.pageSize, list);
}
public PageHelper pageHelper(List<T> list) {
return new PageHelper(this.currentPage, this.pageSize, list);
}
public PageHelper pageHelper(long currentPage, long total, long pageSize, List<T> list) {
return new PageHelper(currentPage, total, pageSize, list);
}
public PageHelper pageHelper(long currentPage, long pageSize, List<T> list) {
return new PageHelper(currentPage, pageSize, list);
}
/**
* 用于模糊查询忽略大小写
*
* @param string
* @return
*/
public Pattern getPattern(String string) {
Pattern pattern = Pattern.compile("^.*" + string + ".*$", Pattern.CASE_INSENSITIVE);
return pattern;
}
}
分页工具类
package com.jq.util;
import lombok.Data;
import java.util.List;
@Data
public class PageHelper<T> {
private long currentPage;
private long total;
private long pageSize;
private List<T> list;
public PageHelper(long pageNum, long total, long pageSize, List<T> list) {
this.currentPage = pageNum;
this.total = total;
this.pageSize = pageSize;
this.list = list;
}
public PageHelper(long pageNum, long pageSize, List<T> list) {
this.currentPage = pageNum;
this.pageSize = pageSize;
this.list = list;
}
}