First of all, Spring comes with the MONGODB ORM, Spring Data mongodb, but this framework is very difficult to use, the most frustrating thing is that every document has a _class field, because this is a string, so it takes up a lot of space, and it is difficult to remove.

Here I recommend zfoo ORM framework based on the MongoDB official orM lightweight encapsulation, only to the official provided a simple encapsulation, but also do some official does not support the syntax verification.

Introduction of Ⅰ.

  • Orm framework based on MongoDB provides mapping between POJO objects and MongoDB databases
  • Zfoo&java & Netty&mongodb & Vue High performance learning Communication Group: 876280300

ⅱ. Precautions

  • Properties of POJO objects must provide get and set methods or they cannot be mapped
  • Generics are not supported
  • If you do not want to map an attribute, add the transient keyword
  • Supports mapping of basic data attributes (byte, short, int, Long, float, double, Boolean), String, List, and Set attributes, but does not support Map
  • Use integers as the primary key of the database, because MongoDB default primary key is a string, which takes up space
  • The database uses a self-developed ORM framework, such as an Entity class UserEntity, which maps to the database as user, with the first letter lowercase and Entity removed
  • High performance data caching based on Caffeine
  • Syntax verification, such as automatic syntax prompt for fields without get and set

ⅲ. Method of use

1. The IAccessor interface is the data access interface

  • Inserting data into the database takes the return value of the object’s ID () method as the primary key
OrmContext.getAccessor().insert(obj)
Copy the code
  • Deleting data from the database takes the return value of the object’s ID () method as the lookup key and deletes data with this ID () as the primary key
OrmContext.getAccessor().delete(obj);
Copy the code
  • Modify data in the database
OrmContext.getAccessor().update(obj);
Copy the code

2. IQuery interface, for complex data query interface

3. Cache usage

  • For example, the following configuration is available
<orm:config id="config" entity-package="com.zfoo.orm.**.entity"> <orm:host database="test" user="" password=""> < orm: address name = "server0" url = "127.0.0.1:27017" / > < / orm: host > <! <orm:caches> <orm:cache strategy="ten" size="10" expire-millisecond="600000"/> <orm:cache strategy="hundred"  size="100" expire-millisecond="600000"/> <orm:cache strategy="thousand" size="1000" expire-millisecond="600000"/> <orm:cache strategy="threeThousand" size="3000" expire-millisecond="600000"/> <orm:cache strategy="tenThousand" size="10000" expire-millisecond="600000"/> </orm:caches> <! <orm:persisters> <orm:persister strategy="cronDefault" type="cron" config="0,30 * * * *?" /> <orm:persister strategy="cron3s" type="cron" config="0/3 * * * * ?" /> <orm:persister strategy="cron15s" type="cron" config="0/15 * * * * ?" /> <orm:persister strategy="cron30s" type="cron" config="0/30 * * * * ?" /> <orm:persister strategy="cron1m" type="cron" config="0 0/30 * * * ?" /> <orm:persister strategy="time30s" type="time" config="30000"/> </orm:persisters> </orm:config>Copy the code
  • There are the following notes
@EntityCaches(cacheStrategy = "tenThousand", persister = @Persister("time30s"))
public class UserEntity implements IEntity<Long> {
}
Copy the code
  • Database indicates which database to operate on
  • Address Indicates the address of the database, which supports fragment configuration
  • The strategy in Caches represents a caching strategy to read data from the database into the EntityCaches cache in the Orm first, such as hundred, which caching 1000 data from the database and expiring in 10 minutes
  • The strategy in Persisters represents a persistent strategy, such as a 3S strategy that writes cached data from EntityCaches to the database every three seconds, with only three seconds of data lost even if an outage occurs
  • The EntityCaches annotation means that it will be managed by the Orm, using hundred policy, and the caching persistence policy is 3s

Ⅳ tutorial.

  • Test contains all the tutorials for adding, deleting, changing and checking. Please install MongoDB before running