简单的一个月之设计实现内容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 @Entity
 20 @SequenceGenerator(name = "SEQ_STORE", sequenceName = "SEQ_NEWS_ID", allocationSize=1)  //主键生成策略
 21 @Table(name = "NEWS")   //映射数据库的一个表,表名为news
 22 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") //列的注解,显示name
 37     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;
    }

    @Override
    public News update(News news) throws DAOException {
        
        return entityDao.merge(news);  //更新实体对象或者将实体保存在数据库中;
    }

    @Override
    public void delete(News news) {
        entityDao.remove(news);
    }

    @Override
    public News findByNewsNo(String newsNo) {
        String hql = "from News where newsNo=?";  //利用hql语言进行预编译;
        return entityDao.findUnique(News.class, hql, newsNo);

    }

    @Override
    public News findById(Long id) {
        
        return entityDao.findById(News.class, id);
    }
    

}

接下来开始写前端jsp页面

转载于:https://www.cnblogs.com/erwei/p/9485693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值