您的位置:首页 > 编程语言 > Java开发

mybatis+springMVC对新闻的增删改查操作(查询分页)

2016-07-13 13:57 645 查看
查询页面代码:

Action

/*

 * @author zxs

 * date:2016/6/24

 */

@Controller

@RequestMapping("/admin/news")

public class AdminNewsAction {

@Autowired
private AdminNewsService adminNewsService;
@RequestMapping(value="/news")
//遍历
public String searchQuery(@ModelAttribute("query") NewsQuery query,Model model){
query.setPageSize(8);
// System.out.println("你好,遍历");
adminNewsService.getPagination(query,model);
return "admin/news/newsManager";
}

service

import java.util.List;

import org.springframework.ui.Model;

import com.hundsun.jresplus.quickstart.biz.bo.Result;

import com.hundsun.jresplus.quickstart.biz.po.News;

import com.hundsun.jresplus.quickstart.biz.query.NewsQuery;

public interface AdminNewsService {

void getPagination(NewsQuery query, Model model);

}

ServiceImpl

ServiceImpl实现service这个接口

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.ui.Model;

import com.hundsun.jresplus.quickstart.biz.bo.Result;

import com.hundsun.jresplus.quickstart.biz.dao.AdminNewsDAO;

import com.hundsun.jresplus.quickstart.biz.po.News;

import com.hundsun.jresplus.quickstart.biz.query.NewsQuery;

import com.hundsun.jresplus.quickstart.biz.service.AdminNewsService;

import com.hundsun.jresplus.quickstart.biz.service.BaseService;

@Service("adminNewsService")

public class AdminNewsServiceImpl extends BaseService implements AdminNewsService{

@Autowired
private AdminNewsDAO adminNewsDAO;

@Override
public void getPagination(NewsQuery query, Model model) {
// TODO Auto-generated method stub
adminNewsDAO.getPagination(query,model);
}

}

dao接口

package com.hundsun.jresplus.quickstart.biz.dao;

import java.util.List;

import org.springframework.ui.Model;

import com.hundsun.jresplus.quickstart.biz.bo.Result;

import com.hundsun.jresplus.quickstart.biz.po.News;

import com.hundsun.jresplus.quickstart.biz.query.NewsQuery;

public interface AdminNewsDAO {

public void getPagination(NewsQuery query, Model model);

}

DAOImpl

import java.util.List;

import org.springframework.stereotype.Repository;

import org.springframework.ui.Model;

import com.hundsun.jresplus.quickstart.biz.bo.Result;

import com.hundsun.jresplus.quickstart.biz.dao.AdminNewsDAO;

import com.hundsun.jresplus.quickstart.biz.po.Building;

import com.hundsun.jresplus.quickstart.biz.po.DsIndex;

import com.hundsun.jresplus.quickstart.biz.po.News;

import com.hundsun.jresplus.quickstart.biz.query.NewsQuery;

import com.hundsun.network.common.dao.BaseDAO;

@Repository("adminNewsDAO")

public class AdminNewsDAOImpl extends BaseDAO implements AdminNewsDAO{
private static final String SQL_SPACE = "ADMINNEWS.";
@Override
public void getPagination(NewsQuery query, Model model) {
// TODO Auto-generated method stub
this.getPagination(query, SQL_SPACE + "queryCount", SQL_SPACE+ "queryList");
}

sqlmap

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >

<sqlMap namespace="ADMINNEWS">

<!-- 查询字段 -->
<typeAlias alias="newsQuery"
type="com.hundsun.jresplus.quickstart.biz.query.NewsQuery" />
<resultMap id="baseResult" class="newsQuery">
<result column="id" property="id" />  
<result column="title" property="title" />
<result column="image" property="image" />
<result column="is_scroll" property="is_scroll" />
<result column="type" property="type" />
<result column="status" property="status" />
<result column="url" property="url" />
  <result column="building_id" property="building_id"/>
  <result column="building_name" property="building_name" />

   <result column="news_id" property="news_id" />
</resultMap>

<!-- 注意,查询是需要分页,分页执行第一个newsQuery,因为分页中涉及到时间,用第二个resultMap会报错,分开来用,查询用第一个,修改和增加用第二个  -->

 
<!-- 修改和插入字段 -->
<typeAlias alias="newsManage" type="com.hundsun.jresplus.quickstart.biz.po.News"/>
<resultMap id="newsbaseResult" class="newsManage">

  <result column="id" property="id"/>
<result column="title" property="title" />
<result column="image" property="image" />
<result column="is_scroll" property="is_scroll"/>
<result column="sort" property="sort"/>
<result column="content" property="content"/>
<result column="type" property="type"/>
<result column="status" property="status" />
<result column="url" property="url" />
<result column="gmt_publish" property="gmt_publish" />
<result column="gmt_create" property="gmt_create" />
<result column="gmt_modify" property="gmt_modify" />

</resultMap>

<!-- 所有字段 -->
<sql id="allColumn">
id,title,image,is_scroll,content,status,type,status,url,sort,gmt_publish,gmt_create,gmt_modify
</sql>
<!-- 获取新闻列表 -->
<select id="queryList" resultMap="baseResult">
select n.id ,title,image,is_scroll,n.`status`,url,n.`type`,building_id,building_name,news_id
from
news n  inner join assoc_building_news abn on n.id=abn.news_id inner join building b on b.id=abn.building_id

<dynamic prepend="where">
<isNotEmpty prepend="and" property="title">
title like
concat('%',#title#, '%')
</isNotEmpty>
</dynamic>
order by id
limit #pageFristItem# , #pageSize#
</select>
<!-- 获取列表个数 -->
<select id="queryCount" resultClass="int">
select
count(1)
from
news n where 1=1
<isNotEmpty prepend="and" property="title">
title like
concat('%',#title#, '%')
</isNotEmpty>
</select>

</sqlMap>

bean建了两个,为了区分查询中时间问题

NewsQuery.java 继承BaseQuery

public class NewsQuery extends BaseQuery {

/**

*/
private static final long serialVersionUID = -4825138265776719877L;

  private int id;

  private String title;

  private String image;

  private String is_scroll;

  private String type;

  private String status;

  private String url;

private Long building_id;

private String building_name;

private Long news_id;

BaseQuery

public class BaseQuery extends QueryPage {

    /**

     * UID

     */

    private static final long serialVersionUID = -3631158632931413835L;

    public static final Log   logger           = LogFactory.getLog(QueryBase.class);

    public int getPageFristItem() {

        return super.getPageFristItem() - 1;

    }

    @Override

    public Map<String, String> getParameters() {

        String[] removeProperties = new String[] { "totalCount", "pageSize", "pageNo", "startRow",

                "endRow", "orderStr", "serialVersionUID" };

        Map<String, String> param = new HashMap<String, String>();

        Field[] fields = this.getClass().getDeclaredFields();

        for (Field field : fields) {

            if (!ArrayUtil.contains(removeProperties, field.getName())) {

                try {

                    String properyValue = BeanUtils.getProperty(this, field.getName());

                    param.put(field.getName(), properyValue);

                } catch (IllegalAccessException e) {

                    logger.error("copy fildValue error", e);

                } catch (InvocationTargetException e) {

                    logger.error("copy fildValue error", e);

                } catch (NoSuchMethodException e) {

                    logger.error("copy fildValue error", e);

                }

            }

        }

        return param;

    }

}

界面

使用vm来写的

#set($layout="")

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>新闻管理系统</title>

 <link href="/cssAdmin/style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="/jsAdmin/jquery.js"></script>

<script type="text/javascript">

$(document).ready(function(){

  $(".click").click(function(){

  $(".tip").fadeIn(200);

  });

  

  $(".search").click(function(){

  document.getElementById("contact").submit(); 

});

  $(".tiptop a").click(function(){

  $(".tip").fadeOut(200);

});

  $(".sure").click(function(){

  $(".tip").fadeOut(100);

});

  $(".cancel").click(function(){

  $(".tip").fadeOut(100);

});

});

</script>

</head>

<body  >

 <div class="place">

    <span>位置:</span>

    <ul class="placeul">

    <li><a href="#">首页</a></li>

    <li><a href="#">新闻管理系统</a></li>

    <li><a href="#">基本内容</a></li>

    </ul>

    </div>

    

    <div class="rightinfo">

    

    <div class="tools">

    

    <ul class="toolbar">

    #* <form action="newsIncreaseNN.htm" method="get"> *#

        <li class="click"><span><img src="$appServer/imagesAdmin/t01.png" /></span>添加</li>

      #* </form>*#

        </ul>

        <ul class="toolbar1">

        <form  action="search.htm" method="post"  id="contact"  name="contact">

        <li>标题 <input name="title" type="text" class="dfinput" style="width:200px;"/></li>

        <li class="search" ><span><img src="$appServer/imagesAdmin/t05.png" /></span>查询</li>

     </form>

        </ul>

    </div> 

   

 <table class="tablelist">

   

    <thead>

    <tr>      

        <th>新闻编号<i class="sort"><img src="$appServer/imagesAdmin/px.gif" /></i></th>

       <th>楼宇号</th>

       <th>楼宇名称</th>

        <th>标题</th>

     #*   <th>图片</th> *#

          <th>是否滚动</th>

         <th>类型</th>

        <th>状态</th>

         <th>链接地址</th>

        <th>操作</th>

        </tr>

        </thead>

<tbody>

      #foreach($item in $query.items) 

        <tr>

        <td>$!item.id</td>

          <td>$!item.building_id</td>

          <td>$!item.building_name</td>

        

        <td>$!item.title</td>

  #*    <td> <img width="150" height="100"  src="$!item.image"></img></td>  *#

      #if($!item.is_scroll=='Y')

      <td>是</td>

      #else

      <td>否</td>

      #end

   <td>

    #if($item.type==0)

    图片切换新闻

    #end

    #if($item.type==1)

    下载新闻

    #end

    #if($item.type==2)

   综合资讯

    #end

      #if($item.type==3)

   快车网资讯

    #end

      #if($item.type==4)

   活动公告

    #end

    </td>

      #if($item.status=='0')

      <td>链接类型</td>

      #else

      <td>图文类型</td>

      #end 

          <td> <a href="$!item.url">预览</td>

          

        <td><a href="updateSer.htm?id=$item.id" class="tablelink">编辑</a>   

 <a onclick="if(window.confirm('您确定要删除吗!'))window.location.href='delete.htm?id=$item.id'"  class="tablelink" style='cursor: pointer'>删除</a>

</td>

        </tr> 

#end

  </tbody>

</table>

#pager($query,"/admin/news/news.htm")  

    

    

  <div class="tip">

    <div class="tiptop"><span>提示信息</span><a></a></div>

        

      <div class="tipinfo">

        <span><img src="$appServer/imagesAdmin/ticon.png" /></span>

        <div class="tipright">

       <p>是否确认对信息的增加 ?</p>

        <cite>如果是请点击确定按钮 ,否则请点取消。</cite> 

        </div>

        </div>

        

        <div class="tipbtn">

       <form action="newsIncreaseNN.htm" method="get"> 

        <input  type="submit"  class="sure" value="确定" /> 

        <input  type="button"  class="cancel" value="取消" />

     </form>

       </div> 

    

    </div>

    </div>

    

    <script type="text/javascript">
$('.tablelist tbody tr:odd').addClass('odd');
</script>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息