[使用通用语言处理器、MySQL、MVC、服务包、JSP]如果我从数据库 LEFT JOIN-ing
三个表(DAO 对象的方法内部)读取一些数据,我应该如何格式化该结果,以便我可以将其设置为请求属性(在 servlet 中)并转发到 JSP 页面?
实体(数据库中的表):
发布
:
@Entity
@Table(name = "post")
public class Post implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "post_id", unique = true, nullable = false)
private Integer id;
@Column(name = "post_title", length=300, unique = false, nullable = false)
private String title;
@Column(name = "post_date", unique = false, nullable = false)
private Date date;
@Column(name = "post_summary", length=1000, unique = false, nullable = true)
private String summary;
@Column(name = "post_content", length=50000, unique = false, nullable = false)
private String content;
@Column(name = "post_visitors", unique = false, nullable = false)
private Integer visitors;
@ManyToOne
@JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
private User user;
@ManyToOne
@JoinColumn (name = "category_id", referencedColumnName="category_id", nullable = false)
private Category category;
@OneToMany(cascade = { ALL }, fetch = LAZY, mappedBy = "post")
private Set<Comment> comments = new HashSet<Comment>();
...
实体注释
:
@Entity
@Table(name = "comment")
public class Comment implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "comment_id", unique = true, nullable = false)
private Integer id;
@Column(name = "comment_title", length=300, unique = false, nullable = false)
private String title;
@Column(name = "comment_date", unique = false, nullable = false)
private Date date;
@Column(name = "comment_content", length=600, unique = false, nullable = false)
private String content;
@ManyToOne
@JoinColumn (name = "user_id", referencedColumnName="user_id", nullable = false)
private User user;
@ManyToOne
@JoinColumn (name = "post_id", referencedColumnName="post_id", nullable = false)
private Post post;
...
实体用户
:
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer id;
@Column(name = "user_name", length=45, unique = false, nullable = false)
private String name; // "first_name" + ' ' + "last_name"
//URL address to user's image
@Column(name = "user_image", length=500, unique = false, nullable = true)
private String image;
@Column(name = "user_username", length=45, unique = false, nullable = false)
private String username;
@Column(name = "user_password", length=45, unique = false, nullable = false)
private String password;
...
因此,我想创建一个方法,可能在<code>PostDAO</code>对象中,它看起来像这样:
public <SomeDataTypeFormat???> getPostsSummaries(){
Query q = em.createNativeQuery("SELECT
post_title,
post_summary,
post_date,
COUNT(comment_id) AS comment_cnt,
user.user_name
FROM
post
LEFT JOIN user USING(user_id)
LEFT JOIN comment USING(post_id)
GROUP BY
post_id
ORDER BY
comment_cnt DESC");
...
}
方法从数据库中的所有三个表中返回一些字段。我是否需要创建单独的类并将这些数据存储在该类的对象中?还是JSON(尽管我还没有使用它)?
做法是什么?对于由于连接多个表而获得的某些字段,从 servlet 到 JSP 的最简单使用和转发的数据格式是什么?
这取决于您的目标;要将数据传输到浏览器,JSON和AJAX是一个不错的选择。要将数据传输到JSP(从Servlet),您可能需要一个数据传输对象(或者可能是一个不可变的值对象)。