Giter Club home page Giter Club logo

fdd-common-mongodb's Introduction

fdd-common-mongodb

fdd-common-mongodb是对mongodb-java-driver的一层封装。主要扩展了:

  1. 实现了自动识别的CodeC,让使用者可以直接通过Pojo类操作mongodb(ORM功能)

  2. 支持自增ID,会自动在添加一个collection,名称为counter,用于计数

  3. 实现了一些safeUpdate / getEntityById等便捷方法

  4. 实现了一些属性注解,用于控制字段是否可写、可读等

除了增加的一些方法外,大部分的官方驱动都直接暴露出去,可直接调用

使用

  1. 添加依赖
<dependency>
    <groupId>com.fangdd</groupId>
    <artifactId>common-mongodb</artifactId>
    <version>1.0.0</version>
</dependency>
  1. 添加配置
########## cp_article 连接参数 ############
#连接参数
mongodb.article.connections=[{"host":"mongodb1.tp.fdd","port":27017,"user":"test_user","password":"N0zncU#B1s8"},{"host":"mongodb2.tp.fdd","port":27017},{"host":"mongodb3.tp.fdd","port":27017}]
#每台服务器连接池连接个数
mongodb.article.connections-per-host=20
#最大等待时间(毫秒)
mongodb.article.max-wait-time=2000
  1. Spring托管
@Configuration
@ComponentScan
public class MongoDataSourceConfig {
    /**
     * article库
     *
     * @return
     */
    @Bean("articleMongoClient")
    @ConfigurationProperties("mongodb.article")
    public YMongoClient getArticleMongoClient() {
        return new YMongoClient();
    }
}
  1. 编写库基类(推荐)
@Repository
public class ArticleDao extends ArticleEntityDao<Article> {
    @Override
    protected String getCollectionName() {
        return "article";
    }
}
  1. 使用
@Autowired
private ArticleDao articleDao;

@Test
public void getById() {
    Article article = articleDao.getEntityById(423584L);
    Assert.assertEquals("测试文章", article.getTitle());
} 

fdd-common-mongodb's People

Contributors

ycoe avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

Hzm avatar Schubert Zhang avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.