Precautions and simple use of mongodb ORM framework

Time:2022-3-23

First of all, spring comes with mongodb‘s ORM, spring data mongodb, but this framework is very difficult to use. The most annoying thing is that each document has to bring one_ The class field takes up a lot of space because it is string, and it is troublesome to remove it. Therefore, the zfoo ORM framework is used

Ⅰ. brief introduction

Based on the ORM framework of mongodb, it provides the mapping between POJO objects and mongodb database

Ⅱ. matters needing attention

  • The properties of POJO objects must provide get and set methods, otherwise they cannot be mapped
  • Generics are not supported
  • If you don’t want to map an attribute, add the transient keyword directly
  • At present, it supports the mapping of basic data attributes (byte, short, int, long, float, double, Boolean), string, list, set and collection attributes, but does not support map
  • The database primary key can be integer. Try to use integer, because the default primary key of mongodb is a string, which takes up more space
  • The database uses the self-developed ORM framework, such as an entity class userentity. The set mapped to the database is user, with the initial lowercase and entity removed
  • be based oncaffeineHigh performance data cache
  • Syntax verification, such as automatic syntax prompt for fields without get and set

Ⅲ. usage method

1. Iaccessor interface is a data access interface

  • When inserting data into the database, the returned value of the ID () method of the object will be used as the primary key

OrmContext.getAccessor().insert(obj)
  • When deleting the data in the database, the returned value of the ID () method of the object will be used as the search keyword to delete the data with this ID () as the primary key

OrmContext.getAccessor().delete(obj);
  • Modify data in the database

OrmContext.getAccessor().update(obj);

2. Iquery interface, which is a complex data query interface

3. Cache usage

For example, there are the following configurations

<orm: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>

    <!--  Cache policy -- >
    <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>

    <!--  Persistence policy -- >
    <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>

There are the following notes


@EntityCaches(cacheStrategy = "tenThousand", persister = @Persister("time30s"))
public class UserEntity implements IEntity<Long> {
}
  • Database indicates which database to operate on
  • Address indicates the address of the database and supports fragment configuration
  • Strategy in caches represents a caching strategy, that is, the data in the database is first read into the entitycache cache in orm. For example, hundred means that 1000 data in the database are cached and expire in 10 minutes
  • Strategy in persisters represents a persistent strategy, such as 3S, which means that the cached data in entitycache is written to the database every 3S. Even if it goes down halfway, only 3 seconds of data will be lost
  • The annotation of entitycache indicates that it will be managed by ORM, using the hundred policy, and the persistence policy of cache is 3S

Ⅳ. course

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

The above are the precautions and details of simple use of mongodb ORM framework. For more information about mongodb ORM framework, please pay attention to other relevant articles of developeppaer!

Recommended Today

Docker MySQL master-slave installation

1. First, remove the image docker pull mysql:5.7 2. Run image It mainly maps log storage files and configuration files to the host docker run -p 13307:3306 –name mysql-master \ -v /mydata/mysql-master/log:/var/log/mysql \ -v /mydata/mysql-master/data:/var/lib/mysql \ -v /mydata/mysql-master/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \-d mysql:5.7 [[email protected] ~]# docker run -p 13307:3306 –name mysql-master \ > -v /mydata/mysql-master/log:/var/log/mysql […]