Coder Social home page Coder Social logo

jmongo-3's Introduction

jmongo

This library provides clean and powerful mapping between Java POJOs and MongoDB DBObject.

step 1 setting jmongo.properties in resources and class path

db0.servers=10.18.98.228:27000
db0.connectionsPerHost=64
db0.threadsAllowedToBlockForConnectionMultiplier = 10
db0.connectTimeout=60000
db0.maxWaitTime=120000
db0.socketTimeout=30000
db0.socketKeepAlive=true
db0.minConnectionsPerHost=8
db0.maxConnectionsPerHost=64
db0.serverSelectionTimeout=30000
db0.maxConnectionIdleTime=0
db0.maxConnectionLifeTime=0

step 2 define Entity

import com.lamfire.jmongo.annotations.*;

@Indexes(value={@Index(value = "nickname_age_index",fields = {@Field(value = "nickname"),@Field(value = "age")})})
@Entity
public class User {
    @Id
    private String id;

    @Indexed
    private String nickname;

    @Indexed
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

step 3 Using DAO template

User user = new User();
user.setId("10001");
user.setAge(18);

DAO<User,String> dao = JMongo.getDAO("db0","UserDB","User", User.class);

//save
dao.save(user);
System.out.println(dao.count());

//get
user = dao.get("10001");

//query
List<User> users = dao.createQuery().asList();

No use configure file

//register mongodb host
String zone = "db1";
JMongoZoneOptions options = new JMongoZoneOptions(zone);
options.addHost("10.18.98.228:27000");
options.setConnectionsPerHost(4);
JMongo.registerZoneOptions(options);

//new entity instance
User user = new User();
user.setId("10001");
user.setAge(18);

//get dao instance
DAO<User,String> dao = JMongo.getDAO("db1","test",User.class);

//save
dao.save(user);
System.out.println(dao.count());

//get
user = dao.get("10001");

//query
List<User> users = dao.createQuery().asList();

jmongo-3's People

Contributors

lamfire avatar

Watchers

James Cloos 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.