com.yyself:kotmongo-spring-boot-starter

mongodb utils

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.yyself
ArtifactId

ArtifactId

kotmongo-spring-boot-starter
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

com.yyself:kotmongo-spring-boot-starter
mongodb utils
Project URL

Project URL

http://www.yyself.com
Source Code Management

Source Code Management

https://github.com/yy36295238/kotmongo-spring-boot-starter.git

Download kotmongo-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.yyself/kotmongo-spring-boot-starter/ -->
<dependency>
    <groupId>com.yyself</groupId>
    <artifactId>kotmongo-spring-boot-starter</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.yyself/kotmongo-spring-boot-starter/
implementation 'com.yyself:kotmongo-spring-boot-starter:1.0.4'
// https://jarcasting.com/artifacts/com.yyself/kotmongo-spring-boot-starter/
implementation ("com.yyself:kotmongo-spring-boot-starter:1.0.4")
'com.yyself:kotmongo-spring-boot-starter:jar:1.0.4'
<dependency org="com.yyself" name="kotmongo-spring-boot-starter" rev="1.0.4">
  <artifact name="kotmongo-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.yyself', module='kotmongo-spring-boot-starter', version='1.0.4')
)
libraryDependencies += "com.yyself" % "kotmongo-spring-boot-starter" % "1.0.4"
[com.yyself/kotmongo-spring-boot-starter "1.0.4"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 2.0.4.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.0.4.RELEASE
org.springframework.boot : spring-boot-starter-data-mongodb jar 2.0.4.RELEASE

Project Modules

There are no modules declared in this project.

kotmongo

这是一个springboot-starter项目

对MongoTemplate进行封装,spring-boot-starter-data-mongodb

注意:springboot-2.0以上版本

[使用步骤]

  1. 将项目打包至本地 mvn clean install 仓库或传入私服

  2. 引入jar

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>kotmongo-spring-boot-starter</artifactId>
   <version>1.0.0</version>
</dependency>
  1. 示例
import com.example.yycode.demo.mongo.model.UserInfo;
import kot.bootstarter.kotmongo.KotMongoTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

/**
 * @author YangYu
 */
@RestController
public class ExampleController {

    @Autowired
    private KotMongoTemplate kotMongoTemplate;


    @RequestMapping("/all")
    public String all() {
        String collection = "userinfo";
        final long countByCol = kotMongoTemplate.defaultSource().showSql().count(collection);
        System.err.println("count by collection: " + countByCol);

        final long countByBean = kotMongoTemplate.defaultSource().count(new UserInfo());
        System.err.println("count by bean: " + countByBean);

        final Map map = kotMongoTemplate.defaultSource().findOne(collection);
        System.err.println("findOne by collection: " + map);

        final UserInfo one = kotMongoTemplate.defaultSource().findOne(new UserInfo());
        System.err.println("findOne by bean: " + one.toString());

        kotMongoTemplate.defaultSource()
                .between("age", 0, 100) //范围查询
                .lte("score1", 92) // 小于
                .gt("score1", 90) // 大于
                .orderBy("_id").direction(Sort.Direction.DESC)// 排序
                .page(1, 10) // 分页
                .fields(Arrays.asList("userName", "age", "score1", "score2")) // 返回指定字段
                .showSql() // 显示执行命令(类似SQL)
                .findPage(new UserInfo("zhangsan156")).forEach(System.out::println);

        kotMongoTemplate.defaultSource().eq("userName", "zhangsan1").update(UserInfo.builder().userName("zhangsan125").build());

        kotMongoTemplate.defaultSource().eq("userName", "zhangsan2").delete(new UserInfo());


        // 更新文档
        kotMongoTemplate.defaultSource().eq("userName", "zhangsan1").update(UserInfo.builder().userName("zhangsan125").build());
        kotMongoTemplate.defaultSource().eq("userName", "zhangsan1").update(collection);

        // 删除文档
        kotMongoTemplate.defaultSource().eq("userName", "zhangsan2").delete(new UserInfo());
        kotMongoTemplate.defaultSource().eq("userName", "zhangsan2").delete(collection);

        // 删除集合
        kotMongoTemplate.defaultSource().dropCollection(new UserInfo());
        kotMongoTemplate.defaultSource().dropCollection(collection);

        // 多数据源,第二数据源
        final Map<String, Object> userInfo = kotMongoTemplate.secondSource().findOne(collection);
        System.out.println(userInfo);

        return "SUCCESS";
    }
}
  1. 多数据源
使用默认配置时可以不需要添加MongoConfig,如果使用多数据源时,参考如下配置,第二数据源必需用secondMongoTemplate不可更改名称,使用方式参考上demo
import com.mongodb.MongoClientURI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.net.UnknownHostException;

/**
 * @author yangyu
 */
@Configuration
public class MongoConfig {

    @Bean
    @Primary
    public MongoDbFactory mongoDbFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(new MongoClientURI("mongodb://192.168.117.130:27017/yydb"));
    }

    @Bean(name = "mongoTemplate")
    @Primary
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }

    @Bean("secondMongoDbFactory")
    public MongoDbFactory secondMongoDbFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(new MongoClientURI("mongodb://192.168.117.131:27017/yydb"));
    }

    @Bean(name = "secondMongoTemplate")
    public MongoTemplate secondMongoTemplate() throws Exception {
        return new MongoTemplate(secondMongoDbFactory());
    }
}

Versions

Version
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0-RELEASE