1. 程式人生 > >用戶模塊 之 完成查詢所有帖子、完成查詢所有回復以及點贊

用戶模塊 之 完成查詢所有帖子、完成查詢所有回復以及點贊

raise str nco 完成 imp uniq sel vat put

技術分享圖片

完成這三個數據的統計其邏輯與上一篇博文的邏輯相同,只是其sql語句不同

完成查詢所有帖子

技術分享圖片

在GetDataAction .java中加入:

private PasteService pasteService;

Integer pasteCount =pasteService.getAllPaste();
    ActionContext.getContext().put("pasteCount", pasteCount);

public PasteService getPasteService() {
    return pasteService;
}

public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; }

在service層創建PasteService.java

package com.guiyan.service;

import com.guiyan.dao.PasteDao;

public class PasteService {
    
    private PasteDao pasteDao;
    

    public Integer getAllPaste() {
        
        
return pasteDao.getAllPaste(); } public PasteDao getPasteDao() { return pasteDao; } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } }

在dao層創建類PasteDao.java

package com.guiyan.dao;

import java.math.BigInteger;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class PasteDao extends HibernateDaoSupport { public Integer getAllPaste() { Session session=getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql="select count(*) from paste"; NativeQuery query=session.createSQLQuery(sql); BigInteger result=(BigInteger) query.uniqueResult(); return result.intValue(); } }

在applicationContext.xml中註入:

<!-- 配置Action -->
    <bean name="getDataAction" class="com.guiyan.web.GetDataAction" scope="prototype">
    <property name="userService" ref="userService"></property>
    <property name="pasteService" ref="userService"></property>
    </bean>
    
     
     <!-- 配置service -->
     <bean name="userService" class="com.guiyan.service.UserService">
        <property name="userDao" ref="userDao"></property>
        
    </bean>
    <bean name="pasteService" class="com.guiyan.service.PasteService">
        <property name="pasteDao" ref="pasteDao"></property>
        
    </bean>
    
    
    
    <!-- 配置Dao -->
    <bean name="userDao" class="com.guiyan.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean name="pasteDao" class="com.guiyan.dao.PasteDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

在welcome.jsp中進行連接數據庫帖子數目的顯示:

<li class="layui-col-xs2">
                                            <a href="javascript:;" class="x-admin-backlog-body">
                                                <h3>帖子數</h3>
                                                <p>
                                                    <cite><s:property value="pasteCount"/></cite>
                                                </p>
                                            </a>
                                        </li>

技術分享圖片

數據庫中帖子的數目為:

技術分享圖片

完成查詢所有回復以及點贊

技術分享圖片

數據庫中的內容:

回復數:

技術分享圖片

點贊數:

技術分享圖片

最終顯示結果:

技術分享圖片

在GetDataAction .java中加入:

private AnswerService answerService;
    private PraiseService praiseService;
    



    Integer answerCount = answerService.getAllAnswer();
    ActionContext.getContext().put("answerCount",answerCount);
        
    Integer praiseCount =praiseService.getAllPraise();
    ActionContext.getContext().put("praiseCount", praiseCount);
        


public AnswerService getAnswerService() {
    return answerService;
}

public void setAnswerService(AnswerService answerService) {
    this.answerService = answerService;
}

public PraiseService getPraiseService() {
    return praiseService;
}

public void setPraiseService(PraiseService praiseService) {
    this.praiseService = praiseService;
}

在service層創建

AnswerService.java

package com.guiyan.service;

import com.guiyan.dao.AnswerDao;

public class AnswerService {
    
    private AnswerDao answerDao;

    public Integer getAllAnswer() {
        // TODO Auto-generated method stub
        return answerDao.getAllAnswer();
    }

    public AnswerDao getAnswerDao() {
        return answerDao;
    }

    public void setAnswerDao(AnswerDao answerDao) {
        this.answerDao = answerDao;
    }
    
    

}

PraiseService.java

package com.guiyan.service;

import com.guiyan.dao.PraiseDao;

public class PraiseService {
    
    private PraiseDao praiseDao;

    public Integer getAllPraise() {
        // TODO Auto-generated method stub
        return praiseDao.getAllPraise();
    }

    public PraiseDao getPraiseDao() {
        return praiseDao;
    }

    public void setPraiseDao(PraiseDao praiseDao) {
        this.praiseDao = praiseDao;
    }
    

}

在dao層創建類

AnswerDao.java

package com.guiyan.dao;

import java.math.BigInteger;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class AnswerDao extends HibernateDaoSupport {

    public Integer getAllAnswer() {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        String sql = "select count(*) from answer";
        NativeQuery query = session.createSQLQuery(sql);
        BigInteger result = (BigInteger) query.uniqueResult();//需要BigInteger類型
        return result.intValue();
    }

}

PraiseDao.java

package com.guiyan.dao;

import java.math.BigInteger;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class PraiseDao extends HibernateDaoSupport {

    public Integer getAllPraise() {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        String sql = "select count(*) from praise";
        NativeQuery query = session.createSQLQuery(sql);
        BigInteger result = (BigInteger) query.uniqueResult();//需要BigInteger類型
        return result.intValue();
        
        
        
        
    }

}

在applicationContext.xml中註入:

<!-- 配置Action -->
    <bean name="getDataAction" class="com.guiyan.web.GetDataAction" scope="prototype">
    <property name="userService" ref="userService"></property>
    <property name="pasteService" ref="userService"></property>
    
    <property name="answerService" ref="answerService"></property>
    <property name="praiseService" ref="praiseService"></property>
    </bean>
    
     
     <!-- 配置service -->
     <bean name="userService" class="com.guiyan.service.UserService">
        <property name="userDao" ref="userDao"></property>
        
    </bean>
    <bean name="pasteService" class="com.guiyan.service.PasteService">
        <property name="pasteDao" ref="pasteDao"></property>
        
    </bean>
    
    <bean name="answerService" class="com.guiyan.service.AnswerService">
        <property name="answerDao" ref="answerDao"></property>
        
    </bean>
    <bean name="praiseService" class="com.guiyan.service.PraiseService">
        <property name="praiseDao" ref="praiseDao"></property>
        
    </bean>
    
    
    
    <!-- 配置Dao -->
    <bean name="userDao" class="com.guiyan.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean name="pasteDao" class="com.guiyan.dao.PasteDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean name="answerDao" class="com.guiyan.dao.AnswerDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean name="praiseDao" class="com.guiyan.dao.PraiseDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    

在welcome.jsp中獲取:

<li class="layui-col-xs2">
                                            <a href="javascript:;" class="x-admin-backlog-body">
                                                <h3>回復數</h3>
                                                <p>
                                                    <cite><s:property value="answerCount"/></cite>
                                                </p>
                                            </a>
                                        </li>
                                        <li class="layui-col-xs2">
                                            <a href="javascript:;" class="x-admin-backlog-body">
                                                <h3>點贊數</h3>
                                                <p>
                                                    <cite><s:property value="praiseCount"/></cite>
                                                </p>
                                            </a>
                                        </li>

技術分享圖片

用戶模塊 之 完成查詢所有帖子、完成查詢所有回復以及點贊