數據庫中數據爲空,轉換爲對象時的處理

基本數據類型中,如果數據庫中的數據爲空,需要進行轉換

以hibernate使用爲例

查詢對象VO類展示

public class ArticleDataVO {
	/** 文章id */
	protected String newsId;
	/** 閱讀次數 */
	protected long reads;
	/** 閱讀人數 */
	protected long readers;
	/** 評論數 */
	protected long comments;
	/** 點贊數 */
	protected long praises;
	/** 分享數 */
	protected long shares;
	/** 收藏數 */
	protected long favorites;

	public ArticleDataVO() {}

	/**
	 * WEB文章
	 * 
	 * @param newsId
	 *            文章id
	 * @param reads
	 *            閱讀次數
	 * @param readers
	 *            閱讀人數
	 **/
	public ArticleDataVO(String newsId, Number reads, Number readers) {
		this.newsId = newsId;
		this.reads = reads == null ? 0 : reads.longValue();
		this.readers = readers == null ? 0 : readers.longValue();
	}

	/**
	 * APP文章
	 * 
	 * @param newsId
	 *            文章id
	 * @param reads
	 *            閱讀次數
	 * @param readers
	 *            閱讀人數
	 * @param comments
	 *            評論數
	 * @param praises
	 *            點贊數
	 * @param shares
	 *            分享數
	 * @param favorites
	 *            收藏數
	 */
	public ArticleDataVO(String newsId, Number reads, Number readers, Number comments, Number praises, Number shares,
			Number favorites) {
		this.newsId = newsId;
		this.reads = reads == null ? 0 : reads.longValue();
		this.readers = readers == null ? 0 : readers.longValue();
		this.comments = comments == null ? 0 : comments.longValue();
		this.praises = praises == null ? 0 : praises.longValue();
		this.shares = shares == null ? 0 : shares.longValue();
		this.favorites = favorites == null ? 0 : favorites.longValue();
	}

	/**
	 * Get the {@link #newsId}.
	 * 
	 * @return the newsId
	 */
	public String getNewsId() {
		return newsId;
	}

	/**
	 * Set the {@link #newsId}.
	 * 
	 * @param newsId
	 *            the newsId to set
	 */
	public void setNewsId(String newsId) {
		this.newsId = newsId;
	}

	/**
	 * Get the {@link #reads}.
	 * 
	 * @return the reads
	 */
	public long getReads() {
		return reads;
	}

	/**
	 * Set the {@link #reads}.
	 * 
	 * @param reads
	 *            the reads to set
	 */
	public void setReads(long reads) {
		this.reads = reads;
	}

	/**
	 * Get the {@link #readers}.
	 * 
	 * @return the readers
	 */
	public long getReaders() {
		return readers;
	}

	/**
	 * Set the {@link #readers}.
	 * 
	 * @param readers
	 *            the readers to set
	 */
	public void setReaders(long readers) {
		this.readers = readers;
	}

	/**
	 * Get the {@link #comments}.
	 * 
	 * @return the comments
	 */
	public long getComments() {
		return comments;
	}

	/**
	 * Set the {@link #comments}.
	 * 
	 * @param comments
	 *            the comments to set
	 */
	public void setComments(long comments) {
		this.comments = comments;
	}

	/**
	 * Get the {@link #praises}.
	 * 
	 * @return the praises
	 */
	public long getPraises() {
		return praises;
	}

	/**
	 * Set the {@link #praises}.
	 * 
	 * @param praises
	 *            the praises to set
	 */
	public void setPraises(long praises) {
		this.praises = praises;
	}

	/**
	 * Get the {@link #shares}.
	 * 
	 * @return the shares
	 */
	public long getShares() {
		return shares;
	}

	/**
	 * Set the {@link #shares}.
	 * 
	 * @param shares
	 *            the shares to set
	 */
	public void setShares(long shares) {
		this.shares = shares;
	}

	/**
	 * Get the {@link #favorites}.
	 * 
	 * @return the favorites
	 */
	public long getFavorites() {
		return favorites;
	}

	/**
	 * Set the {@link #favorites}.
	 * 
	 * @param favorites
	 *            the favorites to set
	 */
	public void setFavorites(long favorites) {
		this.favorites = favorites;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章