简体   繁体   English

将 MongoDB 驱动程序 POJO 与 Java 一起使用

[英]Using MongoDB driver POJO's with Java

EDIT: I can use the POJO example with the suggested post to output a list of users in the console, but no Object id as shown below.编辑:我可以将POJO 示例与建议的帖子一起使用到 output 控制台中的用户列表,但没有Object id ,如下所示。 Is that the problem causing output not showing to the JSP page?这是导致 output 没有显示到JSP页面的问题吗?

User [email=didi@abc.com, fullName=Didi Dee, password=asdfwle]
User [email=lucy@abc.com, fullName=Lucy Liu, password=lalla]

What I expect the result like below, that show user details under each heading on the browser,我期望的结果如下所示,在浏览器的每个标题下显示用户详细信息,

在此处输入图像描述

Here is the new method to list user in DAO class这是在DAO class 中列出用户的新方法

@Override
public List<User> listAll() {
    List<User> userList = new ArrayList<User>();    
    database.getCollection("User", User.class).find().into(userList);
        for (User u : userList) {
        System.out.println(u.toString());
    }

    return userList;
}

My service class我的服务 class

public List<User> listUser() {
    List<User> userList = userDAO.listAll();            
    return userList;
}

The Controller Controller

UserService userService = new UserService(request, response);

List<User> userList = userService.listUser();

request.setAttribute("userList", userList); // for jsp to get Attribute

String list_user_page = "user_list.jsp";
RequestDispatcher rd = request.getRequestDispatcher(list_user_page);
rd.forward(request, response);

The JSP to show the output. JSP显示 output。

<c:forEach items="${userList}" var="user" begin="1">
    <tr>            
        <td>${user.userId}</td>
        <td>${user.email}</td>
        <td>${user.fullName}</td>


        <td><a href="edit_user?id=${user.userId}">Edit</a> &nbsp; <a
            href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a>
        </td>
    </tr>
</c:forEach>

Assuming the following:假设如下:

You've included你已经包括

CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
                                  fromProviders(PojoCodecProvider.builder().automatic(true).build()));

Your database contains records in the following format:您的数据库包含以下格式的记录:

> db.User.find().pretty()
{
    "_id" : ObjectId("5f7f92c4d91d2c38583dbfba"),
    "fullname" : "Stu Dent",
    "email" : "student@uni.versity",
    "password" : "passWord"
}
{
    "_id" : ObjectId("5f7f9497d91d2c38583dbfbb"),
    "fullname" : "A. N. Other",
    "email" : "another@uni.versity",
    "password" : "strongerPassWord_1"
}

User is defined as a bean like this:用户被定义为这样的 bean:

public class User
{
    private String fullname;
    private String email;
    private String password;
    
    public User()
    {
        // Default bean constructor
    }

    public String getFullname()
    {
        return fullname;
    }

    public void setFullname(String fullname)
    {
        this.fullname = fullname;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    @Override
    public String toString()
    {
        return "User [fullname=" + fullname + ", email=" + email + ", password=" + password + "]";
    }
}

The following code will read the contents of the database into the userList List:下面的代码会将数据库中的内容读入到userList列表中:

List<User> userList = new ArrayList<User>();
db.getCollection("User", User.class).find().into(userList);

To output the list:到output名单:

for (User u : userList)
{
    System.out.println(u.toString());
}

Result:结果:

User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]

If you insist on looping through the individual records, then I would suggest:如果您坚持循环查看各个记录,那么我建议:

FindIterable<Document> userTbl = db.getCollection("User").find();

for (Document doc: userTbl)
{
    User user = new User();
    user.setFullname(doc.getString("fullname"));
    user.setEmail(doc.getString("email"));
    user.setPassword(doc.getString("password"));
    
    System.out.println("User = " + user.toString());
}

Which produces:哪个产生:

User = User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User = User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]

The reason I can't show the user list because I forgot put the database connection to my controller that different from people used to do with JPA annotation and add a JPA layer on it.我无法显示用户列表的原因是我忘记将数据库连接到我的 controller,这与人们过去使用 JPA 注释所做的不同,并在其上添加了 JPA 层。

response.setContentType("text/html");
MongoDB.getMongoDB();

在此处输入图像描述

Especially thanks Spuggiehawk for the suggested answer to write less code during a hard time debuggin.特别感谢 Spuggiehawk 提供的建议答案,以便在困难的调试期间编写更少的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM