java实习_day09

中软国际实训第九天——新闻管理条件查询分页展示+新增+编辑

创建新闻实体类

package com.zr0726.news.po;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "t_news")
public class News {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    @Basic(fetch = FetchType.LAZY)
    @Lob
    private String content;
    private String firstPicture;
    private String flag;
    private String views;
    private boolean appreciation;
    private boolean shareStatement;
    private boolean commentabled;
    private boolean published;
    private boolean recommend;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    @ManyToOne
    private Type type;

    @ManyToOne
    private User user;

    @ManyToMany(cascade = CascadeType.PERSIST)   //级联
    private List<Tag> tags = new ArrayList<>();

    @Transient//只存在于实体当中,不会映射到数据库中
    private String tagIds;

    private String description;

    public News(){

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    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 getFirstPicture() {
        return firstPicture;
    }

    public void setFirstPicture(String firstPicture) {
        this.firstPicture = firstPicture;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getViews() {
        return views;
    }

    public void setViews(String views) {
        this.views = views;
    }

    public boolean isAppreciation() {
        return appreciation;
    }

    public void setAppreciation(boolean appreciation) {
        this.appreciation = appreciation;
    }

    public boolean isShareStatement() {
        return shareStatement;
    }

    public void setShareStatement(boolean shareStatement) {
        this.shareStatement = shareStatement;
    }

    public boolean isCommentabled() {
        return commentabled;
    }

    public void setCommentabled(boolean commentabled) {
        this.commentabled = commentabled;
    }

    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }

    public Date getCreatetime() {
        return createTime;
    }

    public void setCreatetime(Date createtime) {
        this.createTime = createtime;
    }

    public Date getUpdatetime() {
        return updateTime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updateTime = updatetime;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public String getTagIds() {
        return tagIds;
    }

    public void setTagIds(String tagIds) {
        this.tagIds = tagIds;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void init(){
        this.tagIds = tagsToIds(this.getTags());
    }

    //1,2,3
    private String tagsToIds(List<Tag> tags){
        if(!tags.isEmpty()){
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for(Tag tag:tags){
                if(flag){
                    ids.append(",");
                }else {
                    flag = true;
                }
                ids.append(tag.getId());
            }
            return ids.toString();
        }else{
            return tagIds;
        }
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", firstPicture='" + firstPicture + '\'' +
                ", flag='" + flag + '\'' +
                ", views='" + views + '\'' +
                ", appreciation=" + appreciation +
                ", shareStatement=" + shareStatement +
                ", commentabled=" + commentabled +
                ", published=" + published +
                ", recommend=" + recommend +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", type=" + type +
                ", user=" + user +
                ", tags=" + tags +
                ", tagIds='" + tagIds + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

}

dao层

@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

CommoditytypeDaoImpl实现类

public class CommoditytypeDaoImpl implements CommoditytypeDao{

	@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

	@Override
	public List<Commoditytype> getCommoditytypeList() {
		List<Commoditytype> cts = new ArrayList<>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commoditytype ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
				cts.add(ct);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return cts;
	}
}

view层

创建对应的servlet

public class IstCommodityServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_name = req.getParameter("c_name");
		String c_madein = req.getParameter("c_madein");
		String c_type = req.getParameter("c_type");
		Integer c_inprice = new Integer(req.getParameter("c_inprice"));
		Integer c_outprice = new Integer(req.getParameter("c_outprice"));
		Integer c_num = new Integer(req.getParameter("c_num"));
		
		int line = new CommodityController().istCommodity(c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
public class DeleteCommodityById extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_id = req.getParameter("c_id");
		
		int line = new CommodityController().delCommodityById(c_id);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

结果展示

在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页