GithubHelp home page GithubHelp logo

jmongo'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

db1.servers=192.168.180.49:27000
db1.connectionsPerHost=16
db1.threadsAllowedToBlockForConnectionMultiplier = 10
db1.connectTimeout=60000
db1.maxAutoConnectRetryTime=10
db1.maxWaitTime=120000
db1.socketTimeout=30000
db1.autoConnectRetry=true
db1.socketKeepAlive=true

step 2 define Entity

import java.util.List;
import org.bson.types.ObjectId;
import com.lamfire.jmongo.annotations.Entity;
import com.lamfire.jmongo.annotations.Id;


@Entity
public class User {
	@Id
	private String id;
    private String username;
    private String password;
    private Integer age;

	public String getId() {
		return id;
	}

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

	public String getUsername() {
		return name;
	}

	public void setUsername(String name) {
		this.name = name;
	}

	... more set and get method
}

step 3 Using DAO template

User user = new User();
user.setId("10001");
user.setAge(18);
user.setUsername("lamfire");
user.setPassword("password");

DAO<User,String> dao = DAOFactory.get("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();

No use configure file

//register mongodb host
MongoOpts opts = new MongoOpts("db1");
opts.addHost("192.168.180.49:27000");
JMongoRegistry.getInstance().register(opts);

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

//get dao instance
DAO<User,String> dao = DAOFactory.get("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's People

Contributors

lamfire avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

xuezhongyu01

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.