SSM实现新闻列表显示

1.使用SQL参照 JSP新闻列表显示

 

2.新增web项目,项目名称为ssmNewsList 

基本项目结构如下:

 

 

 

3.分别创建applicationContext-dao.xml、applicationContext-service.xml、applicationContext-transaction.xml、springmvc.xml

及在web.xml中配置加载

 

 

4.src中分别创建dao  controller  service pojo包

controller中增加显示新闻列表的方法及详情方法

 

 

dao中增加新增查询

@Override
    public List<NewsBean> getNewsList() {

        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from news ");
        
        List<NewsBean> clist = new ArrayList<NewsBean>();
        if(list!=null&&list.size()>0){
            
            for(Map<String,Object> mapl:list){
                NewsBean newsBean=new NewsBean();

            
    
                newsBean.setId(mapl.get("id").toString());
                newsBean.setTitle(mapl.get("title").toString());
                newsBean.setConetnt(mapl.get("conetnt").toString());
                newsBean.setAuthor(mapl.get("author").toString());
                newsBean.setAddtime((Date)mapl.get("addtime"));

                clist.add(newsBean);
            }
            
        }
        return clist;
    }


    @Override
    public NewsBean getNews(String id) {
        NewsBean newsBean=null;
        Object [] obj=new Object[1];
        obj[0]=id;

        List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from news where id=?",obj);
        if(list!=null&&list.size()>0){
            newsBean=new NewsBean();
            for(Map<String,Object> map:list){
                
                newsBean.setId(map.get("id").toString());
                newsBean.setTitle(map.get("title").toString());
                newsBean.setConetnt(map.get("conetnt").toString());
                newsBean.setAuthor(map.get("author").toString());

                newsBean.setAddtime((Date)map.get("addtime"));
            }
        }
        return newsBean;
    }

 

 5.分别增加以下文件分别用于用户登录及新闻显示

 

 

6.显示效果如:

 

 

 

最终源码放在码云: https://gitee.com/lg_ui/ssmNewsList

 

 

 

 

 

posted @ 2020-06-27 13:04  初学者一级  阅读(430)  评论(0编辑  收藏  举报