spring boot 集成mongodb 解决MongoDbFactory已过时的问题
spring boot 集成mongodb 解决MongoDbFactory已过时的问题
·
安装
《linux centos7 docker 安装mongoDB》
不一定非要docker,但是用docker是真的简单!
pom.xml
引入mongdb
<!--mongodb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
application.yml
建议linux安装mongodb为4.X版本
spring:
data:
mongodb:
host: 192.168.18.111
port: 27017
username: admin
password: admin
database: demo
authentication-database: admin
Apple.java
实体类,没用Mongo怕重名
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
@Data
@Document("apple") //apple是集合名称建议全部小写并且和实体类名一致
@NoArgsConstructor
@AllArgsConstructor
public class Apple implements Serializable {
//jdk8版本强烈建议需要序列化的类加上此serialVersionUID变量,可由idea生成
private static final long serialVersionUID = 3984134152513452652L;
@NotNull(message = "ID不能为空")
private Long id;
@NotBlank(message = "名称不能为空")
private String name;
}
MongoConfig.java
解决MongoDbFactory已过时的问题,并去掉_class字段入库
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
/**
* mongo配置文件
*/
@Configuration
public class MongoConfig {
@Bean(name = "mongoTemplate")
public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory,MongoMappingContext mongoMappingContext) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
//去掉_class字段
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return new MongoTemplate(mongoDatabaseFactory,mappingConverter);
}
}
MongoController.java
增删改查
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.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("mongo")
public class MongoController {
@Resource
private MongoTemplate mongoTemplate;
/**
* 新增数据
* @param apple 对象
*/
@PutMapping("insert")
public void insert(@RequestBody @Valid Apple apple){
mongoTemplate.insert(apple);
}
/**
* 通过id查询一条数据
* @param id id
* @return
*/
@GetMapping("findById")
public Apple findById(@RequestParam Long id){
return mongoTemplate.findById(id,Apple.class);
}
/**
* 查询全部集合数据
* @return
*/
@GetMapping("find")
public List<Apple> find(){
return mongoTemplate.find(new Query(),Apple.class);
}
/**
* 修改一条数据
* @param apple 对象
*/
@PostMapping("update")
public void update(@RequestBody @Valid Apple apple){
Query query=new Query(Criteria.where("_id").is(apple.getId()));
Update update= new Update();
//id是无法修改的因为要先通过id来查询数据,查询完数据以后替换掉除id以外的内容
//update.set("_id",apple.getId());
update.set("name",apple.getName());
mongoTemplate.updateFirst(query,update,Apple.class);
}
/**
* 通过id删除一条数据
* @param id id
*/
@DeleteMapping("delete")
public void delete(@RequestParam Long id){
Query query=new Query(Criteria.where("_id").is(id));
mongoTemplate.remove(query,Apple.class);
}
}
启动
如果启动成功,控制台会打印如下信息:
[2022-08-01 14:50:35] [cluster-ClusterId{value='62e777bb052f04471ebe4942', description='null'}-192.168.18.111:27017] [INFO] [org.mongodb.driver.connection] >>> Opened connection [connectionId{localValue:1, serverValue:25}] to 192.168.18.111:27017
[2022-08-01 14:50:35] [cluster-ClusterId{value='62e777bb052f04471ebe4942', description='null'}-192.168.18.111:27017] [INFO] [org.mongodb.driver.cluster] >>> Monitor thread successfully connected to server with description ServerDescription{address=192.168.18.111:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=4777400}
更多推荐
已为社区贡献12条内容
所有评论(0)