简单的一个月之设计实现内容1
生活随笔
收集整理的這篇文章主要介紹了
简单的一个月之设计实现内容1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求:簡單的新聞管理系統,實現簡單的增刪改查功能
1.數據庫表的建立? ?ID非空,數據類型看著給
?
2.寫實體(entity)News.java? ?要與數據庫中的字段相對應,(id,optimistic我沒寫,問題不大) 1 package com.pay.boss.entity; //封裝好的entity包,直接引用2 3 import java.util.Date; //日期工具4 5 import javax.persistence.Column;6 import javax.persistence.Entity;7 import javax.persistence.EnumType;8 import javax.persistence.Enumerated;9 import javax.persistence.SequenceGenerator;10 import javax.persistence.Table;11 12 import com.pay.boss.enums.NewsCategory;//枚舉13 import com.pay.boss.enums.NewsStatus;14 15 /**16 * Title: 新聞需求17 */18 19 @Entity20 @SequenceGenerator(name = "SEQ_STORE", sequenceName = "SEQ_NEWS_ID", allocationSize=1) //主鍵生成策略21 @Table(name = "NEWS") //映射數據庫的一個表,表名為news22 public class News extends AutoIDEntity{23 24 private String newsNo; //新聞編號25 private String summary; // 摘要26 private String title; //題目27 private String content; //內容28 private String imgUrl; // 圖片路徑29 private String author; //作者30 private NewsCategory newsCategory; //新聞類別31 private NewsStatus status; //新聞狀態; 32 private Date createTime; // 創建時間33 private Date updateTime; //修改時間34 35 36 @Column(name = "NEWS_NO") //列的注解,顯示name37 public String getNewsNo() {38 return newsNo;39 }40 public void setNewsNo(String newsNo) {41 this.newsNo = newsNo;42 }43 44 @Column(name = "SUMMARY")45 public String getSummary() {46 return summary;47 }48 public void setSummary(String summary) {49 this.summary = summary;50 }51 52 @Column(name = "TITLE")53 public String getTitle() {54 return title;55 }56 public void setTitle(String title) {57 this.title = title;58 }59 60 @Column(name = "CONTENT")61 public String getContent() {62 return content;63 }64 public void setContent(String content) {65 this.content = content;66 }67 68 69 @Column(name = "IMG_URL")70 public String getImgUrl() {71 return imgUrl;72 }73 public void setImgUrl(String imgUrl) {74 this.imgUrl = imgUrl;75 }76 77 @Column(name = "AUTHOR")78 public String getAuthor() {79 return author;80 }81 public void setAuthor(String author) {82 this.author = author;83 }84 85 @Enumerated(EnumType.STRING) //枚舉注解86 @Column(name = "NEWS_CATEGORY")87 public NewsCategory getNewsCategory() {88 return newsCategory;89 }90 public void setNewsCategory(NewsCategory newsCategory) 91 {92 this.newsCategory = newsCategory;93 }94 95 @Enumerated(EnumType.STRING)96 @Column(name = "STATUS")97 public NewsStatus getStatus() {98 return status;99 } 100 public void setStatus(NewsStatus status) { 101 this.status = status; 102 } 103 104 @Column(name = "CREATE_TIME", columnDefinition = "DATE") 105 public Date getCreateTime() { 106 return createTime; 107 } 108 public void setCreateTime(Date createTime) { 109 this.createTime = createTime; 110 } 111 112 @Column(name = "UPDATE_TIME", columnDefinition = "DATE") 113 public Date getUpdateTime() { 114 return updateTime; 115 } 116 public void setUpdateTime(Date updateTime) { 117 this.updateTime = updateTime; 118 } 119 }?3.寫對應的dao接口及其實現
接口:NewsDao
package com.pay.boss.dao;import com.pay.boss.entity.News;public interface NewsDao {public News create(News news); //增加信息;public News update(News news); //更新信息;public void delete(News news); //刪除信息;public News findByNewsNo(String newsNO); //通過編號查找 public News findById(Long id); //通過id查找 id:邏輯主鍵; }?
實現:NewsDaoImpl
package com.pay.boss.dao.impl;import com.pay.boss.dao.DAOException; //dao異常,用于拋出; import com.pay.boss.dao.NewsDao; import com.pay.boss.entity.News; import com.pay.boss.dao.EntityDao; //Ioc容器(依賴)注入sessionFactory;可以不定義這個dao,直接進行session注入;public class NewsDaoImpl implements NewsDao {private EntityDao entityDao; //注入sessionFactory;public EntityDao getEntityDao(){return entityDao; //get可以不需要; }public void setEntityDao(EntityDao entityDao) {this.entityDao = entityDao;}@Override //重寫public News create(News news) throws DAOException {entityDao.persist(news); //將實體保存在數據庫中;return news;}@Overridepublic News update(News news) throws DAOException {return entityDao.merge(news); //更新實體對象或者將實體保存在數據庫中; }@Overridepublic void delete(News news) {entityDao.remove(news);}@Overridepublic News findByNewsNo(String newsNo) {String hql = "from News where newsNo=?"; //利用hql語言進行預編譯;return entityDao.findUnique(News.class, hql, newsNo);}@Overridepublic News findById(Long id) {return entityDao.findById(News.class, id);}}接下來開始寫前端jsp頁面
轉載于:https://www.cnblogs.com/erwei/p/9485693.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的简单的一个月之设计实现内容1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奔跑吧恐龙-JAVA从入门到精通
- 下一篇: 基于jsp的网上书店_[内附完整源码和文