一,项目结构
二,使用的依赖
dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
三,创建Employee,Employee里有需要插入到数据库中的各项信息,如_id,name,age,createTime
2)server 层
3)在application.yml
spring:
data:
mongodb:
uri: mongodb://localhost:27017/admin
4)test 测试
@Resource
EmployDao employDao;
@Test
void contextLoads() {
//查询所有用户
List<Employee> all = employDao.findAll();
for (Employee employee : all) {
System.out.println(employee);
}
}
@Test
void test1(){
//新增用户
Employee employee = new Employee();
employee.setName("掉毛");
employee.setAge(22);
employee.setCreateTime(new Date());
Employee addemploy = employDao.addemploy(employee);
System.out.println(addemploy);
}
@Test
void test2(){
UpdateResult updateemploy = employDao.updateemploy("掉毛"); //根据名称修改
System.out.println(updateemploy);
}
@Test
void test3(){
DeleteResult deleteemploy = employDao.deleteemploy("kzi"); //根据名称删除
}