springboot 工程引入Mongodb 简述

springboot 工程引入Mongodb  简述总结

1、引入mongodb jar

compile "org.springframework.boot:spring-boot-starter-data-mongodb"

2、application.yml 中添加配置,如下:

spring:
  data:
    mongodb:
      host: 127.0.0.1
      port: 27017
      database: test2

3、测试存储及访问,使用 MongoTemplate 接口方式

举例:

@Document(collection="mongo_user")  // 类似
class MongoUser implements Serializable{
    @Id
     String id
     Integer userId
     String userName
}

1)配置 MongoTemplate 

@CompileStatic
@TypeChecked
@Configuration
class MongoTemplateConfig {


    @Bean
    public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory,
                                       MongoMappingContext context) {
        MappingMongoConverter converter =
                new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory), context)
        converter.setTypeMapper(new DefaultMongoTypeMapper(null))
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter)


        return mongoTemplate


    }
}

2)测试类

 @Autowired
    private MongoTemplate mongoTemplate


    @Test
    public  void test(){
        MongoUser mongoUser = new MongoUser()
        mongoUser.userId=1001
        mongoUser.userName="测试1001"
        mongoUser.id=UUID.randomUUID().toString()
        mongoTemplate.insert(mongoUser)




        def tt=mongoTemplate.findOne(new Query(new Criteria("userId").is(1001)),MongoUser.class)
        println "tt:"+tt.userName


        def list=mongoTemplate.find(new Query(new Criteria("userName").in("测试1001","测试111112")),MongoUser.class)
        println "num:"

4、测试存储及访问,使用 MongoRepository 方式

通用 接口

interface IBaseMongoRepository<T,ID extends Serializable> extends MongoRepository<T,ID>{


}

通用接口实现

class BaseMongoRepository<T,ID extends  Serializable> extends SimpleMongoRepository<T,ID> implements IBaseMongoRepository<T,ID>{
    protected   MongoOperations mongoOperations
    protected  MongoEntityInformation<T, ID> entityInformation
    /**
     * Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
     *
     * @param metadata must not be {@literal null}.
     * @param mongoOperations must not be {@literal null}.
     */
    BaseMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
        super(metadata, mongoOperations)
        this.mongoOperations=mongoOperations
        this.entityInformation=metadata
    }


创建 IMongoUserRepository

interface IMongoUserRepository extends IBaseMongoRepository<MongoUser,Serializable> {


}


测试类


@NoRepositoryBean
  @Autowired
    private IMongoUserRepository mongoUserRepository;


    @Test
    public void add() {
        MongoUser mongoUser = new MongoUser()
        mongoUser.userId=111112
        mongoUser.userName="测试111112"
        mongoUser.id=UUID.randomUUID().toString()
        mongoUserRepository.save(mongoUser)
    }
    @Test
    public  void findAll(){
        def list=mongoUserRepository.findAll()
        for (MongoUser user:list){
            println user.userName
        }
    }
    @Test
    public void findBYID(){
        def obj=mongoUserRepository.findOne("97f57fdb-6db9-4402-9540-448408c0d5fb")
        println obj.userName
    }



阅读更多
换一批

没有更多推荐了,返回首页