使用 MongoDB driver POJO 和 Java
Using MongoDB driver POJO's with Java
编辑:我可以使用 POJO example with the suggested 在控制台中输出用户列表,但没有 Object id
,如下所示。这是导致输出未显示到 JSP
页面的问题吗?
User [email=didi@abc.com, fullName=Didi Dee, password=asdfwle]
User [email=lucy@abc.com, fullName=Lucy Liu, password=lalla]
我期望的结果如下所示,在浏览器的每个标题下显示用户详细信息,
这是在 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;
}
我的服务class
public List<User> listUser() {
List<User> userList = userDAO.listAll();
return userList;
}
控制器
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);
JSP显示输出。
<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> <a
href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a>
</td>
</tr>
</c:forEach>
假设如下:
你已经包含了
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
您的数据库包含以下格式的记录:
> 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"
}
用户被定义为这样的 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 + "]";
}
}
下面的代码会将数据库的内容读入到userList列表中:
List<User> userList = new ArrayList<User>();
db.getCollection("User", User.class).find().into(userList);
要输出列表:
for (User u : userList)
{
System.out.println(u.toString());
}
结果:
User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]
如果你坚持要循环遍历单个记录,那么我建议:
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());
}
产生:
User = User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User = User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]
我无法显示用户列表的原因是我忘记将数据库连接到我的控制器,这与过去使用 JPA 注释的人不同,并在其上添加了一个 JPA 层。
response.setContentType("text/html");
MongoDB.getMongoDB();
特别感谢 Spuggiehawk 提供的建议答案,以便在困难的调试期间编写更少的代码。
编辑:我可以使用 POJO example with the suggested Object id
,如下所示。这是导致输出未显示到 JSP
页面的问题吗?
User [email=didi@abc.com, fullName=Didi Dee, password=asdfwle]
User [email=lucy@abc.com, fullName=Lucy Liu, password=lalla]
我期望的结果如下所示,在浏览器的每个标题下显示用户详细信息,
这是在 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;
}
我的服务class
public List<User> listUser() {
List<User> userList = userDAO.listAll();
return userList;
}
控制器
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);
JSP显示输出。
<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> <a
href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a>
</td>
</tr>
</c:forEach>
假设如下:
你已经包含了
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
您的数据库包含以下格式的记录:
> 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"
}
用户被定义为这样的 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 + "]";
}
}
下面的代码会将数据库的内容读入到userList列表中:
List<User> userList = new ArrayList<User>();
db.getCollection("User", User.class).find().into(userList);
要输出列表:
for (User u : userList)
{
System.out.println(u.toString());
}
结果:
User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]
如果你坚持要循环遍历单个记录,那么我建议:
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());
}
产生:
User = User [fullname=Stu Dent, email=student@uni.versity, password=passWord]
User = User [fullname=A. N. Other, email=another@uni.versity, password=strongerPassWord_1]
我无法显示用户列表的原因是我忘记将数据库连接到我的控制器,这与过去使用 JPA 注释的人不同,并在其上添加了一个 JPA 层。
response.setContentType("text/html");
MongoDB.getMongoDB();
特别感谢 Spuggiehawk 提供的建议答案,以便在困难的调试期间编写更少的代码。