用戶模塊 之 完成查詢全部帖子、完成查詢全部回覆以及點贊

 

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

 

完成查詢全部帖子

 

在GetDataAction .java中加入: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.javaweb

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.javaspring

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中注入:sql

<!-- 配置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>

 

數據庫中帖子的數目爲:session

 

 

 

 

完成查詢全部回覆以及點贊

 

 

數據庫中的內容:app

回覆數:jsp

 

點贊數:ui

 

最終顯示結果:

 

在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>

 

相關文章
相關標籤/搜索