JPA之集成mongodb

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsoftchenfei/article/details/82907433

JPA已成为ORM事实的标准,已经深入人心。不清楚的可以参阅《JPA之Spring Data JPA

目录

原生使用

spring-data-mongodb

MongoTemplate


原生使用

先了解下MongoClient的用法

项目中引入

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.3.0</version>
</dependency>

基本使用

public class MongoDriver_test {
    private MongoClient mongo;
    private MongoDatabase db;
    private MongoCollection<Document> users;
    private String host = "127.0.0.1";
    private int port = 27017;
    private String dbname = "javadb";

    @Before
    public void init() {
        MongoClientOptions.Builder build = new MongoClientOptions.Builder();
        //与数据最大连接数50
        build.connectionsPerHost(50);
        //如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待
        build.threadsAllowedToBlockForConnectionMultiplier(50);
        build.connectTimeout(1 * 60 * 1000);
        build.maxWaitTime(2 * 60 * 1000);
        MongoClientOptions options = build.build();
        //mongo = new MongoClient(host,options);
        mongo = new MongoClient(host, port);
        db = mongo.getDatabase(dbname);
        // 获取temp DB;如果默认没有创建,mongodb会自动创建
        // 获取 DBCollection;如果默认没有创建,mongodb会自动创建
        //users=db.createCollection("users");
        users = db.getCollection("users");

    }

    @After
    public void destory() {
        if (mongo != null) {
            mongo.close();
        }
        db = null;
    }

    @Test
    public void test_db() {
        //所有的db
        for (String name : mongo.getDatabaseNames()) {
            System.out.println("dbName:" + name);
        }
    }

    @Test
    public void test_queryAll() {
        MongoCursor<Document> cursor = users.find().iterator();
        iterator_dis(cursor);
    }

    private void iterator_dis(MongoCursor<Document> cursor) {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }

    /**
     * 也是支持BasicDBObject的
     */
    @Test
    public void test_search() {
        System.out.println("find age <= 24:");
        MongoCursor<Document> cursor = users.find(Filters.lte("age", 24)).iterator();
        //MongoCursor<Document> cursor = users.find(new BasicDBObject("age", new BasicDBObject("$lte", 24))).iterator();
        iterator_dis(cursor);

        System.out.println("查询age not in 25/26/27:");
        cursor = users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.NIN, new int[]{25, 26, 27}))).iterator();
        iterator_dis(cursor);

        System.out.println("查询age exists 排序:");
        cursor = users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.EXISTS, true))).iterator();
        iterator_dis(cursor);

        System.out.println("findAndRemove 查询age=25的数据,并且删除: " + users.findOneAndDelete(new BasicDBObject("age", 20)));

        // users.findOneAndUpdate(new BasicDBObject("age", 28),)
    }

    @Test
    public void test_add() {
        Document user = new Document().append("name", "MongoDB").append("age", 20);
        Document address = new Document().append("city", "上海").append("area", "长宁");
        user.append("address", address);
        users.insertOne(user);
    }

    @Test
    public void test_remove() {
        Long result = users.deleteOne(Filters.eq("_id", new ObjectId("583fce65c181182f7c51df97"))).getDeletedCount();
        System.out.println("delete id:" + result);
        result = users.deleteMany(Filters.gte("age", 24)).getDeletedCount();
        System.out.println("delte all age>=24:" + result);
    }

    @Test
    public void test_modify() {
        long result = users.updateOne(Filters.eq("_id", new ObjectId("583fcb40c1811828bc8d4495")), new Document("$set",
                new Document("age", "26"))).getModifiedCount();
        System.out.println("updateMany:" + result);
    }

    @Test
    public void json_test() {
        DBObject user = new BasicDBObject();
        user.put("name", "lixiaolong");
        user.put("age", 25);
        //JSON 对象转换
        System.out.println("serialize:" + JSON.serialize(user));
        System.out.println("parse:" + JSON.parse("{ \"name\" : \"yaoming\" , \"age\" : 24}"));

    }
}

spring-data-mongodb

step1,pom中引入

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.9.3.RELEASE</version>
</dependency>

step2,初始化spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
    <!--已过时-->
    <!--<mongo:mongo id="mongo" host="127.0.0.1" port="27017"/>-->
    <!--<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">-->
    <!--<constructor-arg ref="mongo"/>-->
    <!--<constructor-arg value="javadb"/>-->
    <!--</bean>-->
    <mongo:mongo-client id="mongo" host="127.0.0.1" port="27017">
        <mongo:client-options connect-timeout="10000" />
    </mongo:mongo-client>
    <mongo:db-factory id="mongoDbFactory" dbname="javadb" mongo-ref="mongo"/>
    <!--<mongo:template db-factory-ref="mongoDbFactory"/>-->
    <mongo:repositories base-package="com.xfboy.mongo.dao"/>

    <!--解决save时_class问题-->
    <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
        <constructor-arg name="typeKey">
            <null/>
        </constructor-arg>
    </bean>
    <bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
    <mongo:mapping-converter id="mappingMongoConverter" db-factory-ref="mongoDbFactory"
                             mapping-context-ref="mappingContext"
                             type-mapper-ref="defaultMongoTypeMapper"/>
    <mongo:template db-factory-ref="mongoDbFactory" converter-ref="mappingMongoConverter"/>
</beans>

step3,定义domain

@Document(collection = "phone")
public class Phone implements Serializable{
    @Id
    private String id;
    private String name;
    private String pic;
    private double price;

    public Phone() {
    }

    public Phone(String pic) {
        this.pic = pic;
    }

    public Phone(String id, String name, String pic, double price) {
        this.id = id;
        this.name = name;
        this.pic = pic;
        this.price = price;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Phone{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", pic='" + pic + '\'' +
                ", price=" + price +
                '}';
    }
}

step4,定义Repository接口

/**
 * 此处命名准确,jpa规范(面向查询)即可定义处各种接口
 */
public interface PhoneRepository extends MongoRepository<Phone, String>, QueryByExampleExecutor<Phone> {
    Phone findById(String id);

    List<Phone> findByName(String name);
    Page<Phone> findAll(Pageable pageable);//获取所有数据,带分页排序
    Page<Phone> findByNameContaining(String name, Pageable pageable);

}

step5,让它运行起来

@ContextConfiguration(locations = "classpath*:mongo-config.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class PhoneRepository_Test {
    @Autowired
    private PhoneRepository phoneRepository;

    //灰常强大
    @Autowired
    private MongoTemplate mongoTemplate;

    //_class怎么去掉
    @Test
    public void test_add() {
        for (int i = 0; i < 8; i++) {
            Phone phone = new Phone(UUID.randomUUID().toString(), "iphone" + i + 1, "img" + i, 5000);
            phone = phoneRepository.insert(phone);
            System.out.println(phone);
        }
    }

    @Test
    public void test_delete() {
        //默认按id
        phoneRepository.delete(new Phone() {{
            setId("3");
        }});
    }

    //$set?????
    @Test
    public void test_modify() {
        Phone phone = phoneRepository.save(new Phone() {{
            setId("08ed2cb0-423d-4b98-9bf9-06314effb1f7");
            setPrice(5800);
        }});
        System.out.println(phone);
    }

    @Test
    public void test_findById() {
        Phone phone = phoneRepository.findById("4d09b6a5-20bc-4ca3-bcb1-df24a6384a45");
        System.out.println(phone);
    }

    @Test
    public void test_findByNameContaining() {
        List<Phone> result = phoneRepository.findByName("iphone");
        System.out.println(com.alibaba.fastjson.JSON.toJSON(result));
    }

    @Test
    public void test_count() {
        System.out.println(phoneRepository.count());
    }

    @Test
    public void test_query_Example() {
        //implement QueryByExampleExecutor
        Phone phone = new Phone() {{
            setName("iphone");
        }};
        Example<Phone> example = Example.of(phone, matching().withIncludeNullValues());
        System.out.println(phoneRepository.findAll(example).size());

        //example = Example.of(phone, matching().withMatcher("price", matcher -> matcher.transform(v -> Integer.valueOf(50))));

        example = Example.of(new Phone("IMG1"),
                matching().
                        withIgnorePaths("price").
                        withMatcher("pic", ignoreCase()));
        System.out.println(phoneRepository.count(example));


    }


    @Test
    public void test_query_Criteria() {
        Query query = new Query();
        query.addCriteria(new Criteria("name").is("iphone"));
        List<Phone> result = mongoTemplate.find(query, Phone.class);
        System.out.println(result.size());
    }

    @Test
    public void test_page() {
        Pageable pageable = new PageRequest(1, 2, Sort.Direction.DESC, "name");
        Page<Phone> page = phoneRepository.findByNameContaining("phone", pageable);
        System.out.println(com.alibaba.fastjson.JSON.toJSON(page));
        System.out.println(page.getTotalElements());//total rows
        System.out.println(page.getTotalPages());
        System.out.println(page.isFirst());
        System.out.println(page.isLast());
        System.out.println(page.hasNext());
    }
}

MongoTemplate

没错,这个是关键实现,想想RestTemplate、RedisTemplate、RetryTemplate。。。。它肯定也有一个方法execute(Callback input),O(∩_∩)O哈哈~,就是这个套路。以此为切入点,就很容易找到原始驱动(DB、DBCollection)

//MongoTemplate部分源码
public <T> T execute(DbCallback<T> action) {
        Assert.notNull(action);

        try {
            DB e = this.getDb();
            return action.doInDB(e);
        } catch (RuntimeException var3) {
            throw potentiallyConvertRuntimeException(var3, this.exceptionTranslator);
        }
    }

    public <T> T execute(Class<?> entityClass, CollectionCallback<T> callback) {
        return this.execute(this.determineCollectionName(entityClass), callback);
    }

    public <T> T execute(String collectionName, CollectionCallback<T> callback) {
        Assert.notNull(callback);

        try {
            DBCollection e = this.getAndPrepareCollection(this.getDb(), collectionName);
            return callback.doInCollection(e);
        } catch (RuntimeException var4) {
            throw potentiallyConvertRuntimeException(var4, this.exceptionTranslator);
        }
    }

猜你喜欢

转载自blog.csdn.net/itsoftchenfei/article/details/82907433