一,项目完整目录如下
二、在pom文件中引入相关依赖
<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
三、在yml文件进行连接配置
spring:
data:
mongodb:
url: mongodb://localhost:27017/test # 本地安装的MongoDB,所以没有用户名和密码。
# url: mongodb://name:pass@localhost:27017/test # name: 用户名, pass: 密码
# url: mongodb://192:168:1:1:20000,192:168:1:2:20000,192:168:252:12:20000/test # 配置多个数据库用,则中间用 “,” 分割
四、创建实体类
package com.swagger.entity;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* MongoDB实体类
*/
@NoArgsConstructor// 生成无参的构造方法
@AllArgsConstructor// 生成满参的构造方法
@Accessors(chain = true)// 使用链式调用
@Data// 自动生成get/set方法、重写toString方法等方法
public class UserMongoDB implements Serializable {
@ApiModelProperty(value = "用户id")
private Long id;
@ApiModelProperty(value = "用户名称")
private String userName;
@ApiModelProperty(value = "用户年龄")
private int age;
@ApiModelProperty(value = "用户地址")
private String address;
}
五、创建service层
package com.swagger.service;
import com.swagger.entity.UserMongoDB;
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 java.util.List;
@Service
public class UserMongoDBService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
*
* @param user
*/
public UserMongoDB saveUser(UserMongoDB user) {
UserMongoDB save = mongoTemplate.save(user);
return save;
}
/**
* 更新对象
*
* @param user
*/
public void updateUser(UserMongoDB user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update().set("userName", user.getUserName()).set("age", user.getAge()).set("address", user.getAddress());
mongoTemplate.updateFirst(query, update, UserMongoDB.class);
}
/**
* 根据用户名查询对象
*
* @param name
* @return
*/
public UserMongoDB findUserByName(String name) {
Query query = new Query(Criteria.where("userName").is(name));
UserMongoDB user = mongoTemplate.findOne(query, UserMongoDB.class);
return user;
}
/**
* 查询所有对象
*
* @return
*/
public List<UserMongoDB> findUserList() {
List<UserMongoDB> all = mongoTemplate.findAll(UserMongoDB.class);
return all;
}
/**
* 删除对象
*
* @param id
*/
public void deleteUserById(Long id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, UserMongoDB.class);
}
}
六、创建controller层
package com.swagger.controller;
import com.swagger.entity.UserMongoDB;
import com.swagger.service.UserMongoDBService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Controller
*/
@Api("用户管理相关接口--MongoDB")
@RestController //@Controller + @ResponseBody
@RequestMapping("/user")
public class UserMongoDBController {
@Autowired
private UserMongoDBService userMongoDBService;
/**
* 查询全部用户
*
* @return
*/
@ApiOperation("查询全部用户")
@GetMapping("/findList")
public List<UserMongoDB> findUserList() {
List<UserMongoDB> userList = userMongoDBService.findUserList();
return userList;
}
/**
* 根据name查询用户
*
* @param name
* @return
*/
@ApiOperation("根据name查询用户")
@GetMapping("/findByName")
public UserMongoDB findUserByName(@RequestParam String name) {
UserMongoDB userByName = userMongoDBService.findUserByName(name);
return userByName;
}
/**
* 添加用户
*
* @param id
* @param name
* @param age
* @param address
* @return
*/
@ApiOperation("添加用户")
@GetMapping("/save")
public UserMongoDB saveUser(@RequestParam long id, @RequestParam String name, @RequestParam Integer age, @RequestParam String address) {
UserMongoDB user = new UserMongoDB();
user.setUserName(name);
user.setAge(age);
user.setAddress(address);
user.setId(id);
return userMongoDBService.saveUser(user);
}
/**
* 修改用户
*
* @param id
* @param name
* @param age
* @param address
* @return
*/
@ApiOperation("修改用户")
@GetMapping("/update")
public String updateUser(@RequestParam long id, @RequestParam String name, @RequestParam Integer age, @RequestParam String address) {
UserMongoDB user = new UserMongoDB();
user.setUserName(name);
user.setAddress(address);
user.setAge(age);
user.setId(id);
userMongoDBService.updateUser(user);
return "Success";
}
/**
* 根据id删除用户
*
* @param id
* @return
*/
@ApiOperation("根据id删除用户")
@GetMapping("/delete")
public String deleteUserById(@RequestParam Long id) {
userMongoDBService.deleteUserById(id);
return "Success";
}
}
七、发布项目,打开浏览器访问swagger的ui进行测试
http://localhost:8080/swagger-ui.html
源码地址:https://gitee.com/wang-jian-fei/springboot-swagger-mongoDB.git