与article bean对应的表tbl_article
与category bean对应的表tbl_category
Article bean
/**
* 信息类
*/
@Entity
@Table(name="tbl_article")
public class Article {
private Long id;
//标题
private String title;
//内容
private String content;
//作者
private String author;
//发布日期
private Date publishDate;
//点击次数
private Integer clickTimes = 0;
//所属栏目 文章和栏目之间关系为多对一关系
private Category category;
public Article() {
super();
}
public Article(String title, String content, String author, Date publishDate,Category category) {
super();
this.title = title;
this.content = content;
this.author = author;
this.publishDate = publishDate;
this.category=category;
}
public Article(Long id, String title, String content, String author, Date publishDate, Integer clickTimes,
Category category) {
super();
this.id = id;
this.title = title;
this.content = content;
this.author = author;
this.publishDate = publishDate;
this.clickTimes = clickTimes;
this.category = category;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/*@Transient
public String getDateStr(){
if(publishDate==null){
return null;
}
return new SimpleDateFormat("yyyy-MM-dd").format(publishDate);
}*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public Integer getClickTimes() {
return clickTimes;
}
public void setClickTimes(Integer clickTimes) {
this.clickTimes = clickTimes;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="c_id")
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author
+ ", publishDate=" + publishDate + ", clickTimes=" + clickTimes + ", category=" + category + "]";
}
}
category bean
/**
* 栏目类
*/
@Entity
@Table(name="tbl_category")
public class Category {
private Long id;
//栏目名称: 财经
private String name;
//栏目码
private Integer code;
//一对多关系,一个栏目下有多个文章
private Set<Article> articles;
public Category(Long id, String name, Integer code) {
super();
this.id = id;
this.name = name;
this.code = code;
}
public Category(Long id, String name, Integer code, Set<Article> articles) {
super();
this.id = id;
this.name = name;
this.code = code;
this.articles = articles;
}
public Category(String name, Integer code) {
super();
this.name = name;
this.code = code;
}
public Category() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@OneToMany(cascade=CascadeType.REMOVE)
@JoinColumn(name="c_id")
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", code=" + code + "]";
}
}
更新文章
@Action("updateArticleInfo")
public void updateArticleInfo() {
String json=null;
try {
category=new Category();
category.setId(c_id);
article=new Article(id, title, content, author, publishDate, clickTimes, category);
articleService.updateArticle(article);
json="{\"msg\":\"OK\"}";
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
json=null;
}
WebUtil.sendResponse(json);
}
更新栏目
@Action("updateCategory")
public void updateCategory(){
String json=null;
try {
category=categoryService.findById(id);
Set<Article> articles=(Set<Article>) category.getArticles();
category.setName(name);
category.setCode(code);
category.setArticles(articles);
categoryService.updateCategory(category);
String[] str={"articles"};
json="{\"msg\":\"OK\"}";
} catch (ServiceException e) {
e.printStackTrace();
json=null;
}
WebUtil.sendResponse(json);
}