用户模块 之 完成查询所有帖子、完成查询所有回复以及点赞

 

完成这三个数据的统计其逻辑与上一篇博文的逻辑相同,只是其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>

 

 

posted @ 2019-05-16 17:54  perfect*  阅读(520)  评论(0编辑  收藏  举报
$(function() { $('#cnblogs_post_body img').each(function() { let imgSrc = $(this).attr('src'); let year = parseInt(imgSrc.substr(imgSrc.indexOf('g')+1,4)); if(year >= 2022){ imgSrc += `?watermark/2/text/amlndWl5YW4=/font/5a6L5L2T/fontsize/15/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast`; $(this).attr('src', imgSrc) } }) })