本文主要指导以最简单的方式搭建一个以springboot整合swaggerUI并对mongodb数据库进行操作的demo,废话不对说,直接上流程:
在pom文件里面加上需要导入的jar包:
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.6.1
直接在application.properties文件里面加上配置的ip,端口号和数据库名称,有账号密码的自行添加,配置成yml的可以自行修改文件
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=Test
package com.mongodb.demo.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2).select().
apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
}
}
使用@Configuration标记这是一个启动类,需要被spring进行加载,使用@EnableSwagger2表示这是swagger的启动类
package com.mongodb.demo.entity;
/**
* title:com.mongodb.demo.entity
* Company:阿里巴巴
* author:及时雨
* date:2019/9/1
* version 1.0
*/
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* 实体层
* @author linhongcun
*
*/
@Document(collection="t_user")
public class User {
@Id // 指定ID
private String id;
@Field("userName") // 指定域名,覆盖默认
private String userName;
@Field("password") // 指定域名,覆盖默认
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Document(collection=“t_user”)表名这个是一个文档对象,t_user就是你需要插入的collection(类似于表名),@Id 为id,不写会自动创建,@Field为键值对的key值
package com.mongodb.demo.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mongodb.demo.entity.User;
import io.swagger.annotations.ApiOperation;
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.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 控制层
* @author linhongcun
*
*/
@RestController
@RequestMapping("/user")
@EnableSwagger2
public class UserController {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 1、增
* @param user
* @return
*/
@PostMapping("/insert")
@ApiOperation (value = "insert",notes = "插入")
public User insertUser(User user) {
// 可以用 save 替代
mongoTemplate.insert(user);
return user;
}
/**
* 2、查
* @param id
* @return
*/
@GetMapping("/get/{id}")
@ApiOperation (value = "get",notes = "查询")
public User getUserById(@PathVariable String id) {
return mongoTemplate.findById(id, User.class);
}
/**
* 3、删
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
@ApiOperation (value = "delete",notes = "删除")
public User deleteUserById(@PathVariable String id) {
User user = mongoTemplate.findById(id, User.class);
mongoTemplate.remove(user);
return user;
}
/**
* 4、改
* @param user
* @return
*/
@PutMapping("/update")
@ApiOperation (value = "update",notes = "修改")
public User updateUser(User user) {
// save 可增可改!
mongoTemplate.save(user);
return user;
}
/**
* 5、全
* @return
*/
@GetMapping("/get/all")
@ApiOperation (value = "getAll",notes = "获取所有")
public List getAllUsers() {
return mongoTemplate.findAll(User.class);
}
/**
* 6、查 ++:属性、分页
* @param user
* @param page
* @param size
* @return
*/
@GetMapping("/select/{page}/{size}")
@ApiOperation (value = "select",notes = "分页查询")
public Map selectUserByProperty(User user, @PathVariable int page, @PathVariable int size) {
// 条件
Criteria criteria1 = Criteria.where("userName").is(user.getUserName());
Criteria criteria2 = Criteria.where("password").is(user.getPassword());
Query query = new Query();
if (user.getUserName() != null) {
query.addCriteria(criteria1);
}
if (user.getPassword() != null) {
query.addCriteria(criteria2);
}
// 数量
long total = mongoTemplate.count(query, User.class);
// 分页
query.skip((page - 1) * size).limit(size);
List data = mongoTemplate.find(query, User.class);
Map map = new HashMap();
map.put("data", data);
map.put("total", total);
return map;
}
}
代码写完了,直接启动MongodbApplication这个启动类,打开swagger的网址进行测试(http://localhost:8080/swagger-ui.html#/):
插入数据:
查看数据库:
到这里,一个最简单的小demo就完成了.