首页 > 其他分享 >mongoTemplate实现分表

mongoTemplate实现分表

时间:2023-02-06 17:25:57浏览次数:47  
标签:name 实现 userId sex mongoTemplate User 分表 new String

测试方法:

com.kiis.mongodb_demo.mongo.User为实体类
分别根据条件向userOne与userTwo两个集合(表)插入数据

    @Test
    void testInsert(){
        List<User> users1 = new ArrayList<>();
        users1.add(new User("0001","kiis01","M"));
        users1.add(new User("0002","kiis02","F"));
        users1.add(new User("0003","kiis03","M"));

        List<User> users2 = new ArrayList<>();
        users2.add(new User("0004","kiis04","F"));
        users2.add(new User("0005","kiis05","M"));
        users2.add(new User("0006","kiis06","F"));

        template.insert(users1,"userOne");
        template.insert(users2,"userTwo");

        List<User> usersOne = template.find(new Query(), User.class, "userOne");
        System.out.println("==============usersOne===================");
        for (User user:usersOne) {
            System.out.println(user);
        }

        System.out.println();
        List<User> usersTwo = template.find(new Query(), User.class, "userTwo");
        System.out.println("==============userTwo===================");
        for (User user:usersTwo) {
            System.out.println(user);
        }
    }
程序输出结果:
==============usersOne===================
User{userId='0001', name='kiis01', sex='M'}
User{userId='0002', name='kiis02', sex='F'}
User{userId='0003', name='kiis03', sex='M'}

==============userTwo===================
User{userId='0004', name='kiis04', sex='F'}
User{userId='0005', name='kiis05', sex='M'}
User{userId='0006', name='kiis06', sex='F'}
数据库查询结果:
replica:PRIMARY> db.userOne.find();
{ "_id" : "0001", "name" : "kiis01", "sex" : "M", "_class" : "com.kiis.mongodb_demo.mongo.User" }
{ "_id" : "0002", "name" : "kiis02", "sex" : "F", "_class" : "com.kiis.mongodb_demo.mongo.User" }
{ "_id" : "0003", "name" : "kiis03", "sex" : "M", "_class" : "com.kiis.mongodb_demo.mongo.User" }
replica:PRIMARY> db.userTwo.find();
{ "_id" : "0004", "name" : "kiis04", "sex" : "F", "_class" : "com.kiis.mongodb_demo.mongo.User" }
{ "_id" : "0005", "name" : "kiis05", "sex" : "M", "_class" : "com.kiis.mongodb_demo.mongo.User" }
{ "_id" : "0006", "name" : "kiis06", "sex" : "F", "_class" : "com.kiis.mongodb_demo.mongo.User" }

实体类:

可以不设置Document,如果没有注解,代码也不设置,则默认为“user”

package com.kiis.mongodb_demo.mongo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * @Description:
 * @Author: caozz
 * @Date: 2022/12/15 15:49
 * @Version: v1.0
 **/
@Document("user")
public class User {

    @Id
    private String userId;

    private String name;

    private String sex;

    public User() {

    }

    public User(String userId, String name, String sex) {
        this.userId = userId;
        this.name = name;
        this.sex = sex;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

标签:name,实现,userId,sex,mongoTemplate,User,分表,new,String
From: https://www.cnblogs.com/caozz/p/mongo_template.html

相关文章