这里记录一个简单的前后翻页功能。
首先晒下主要效果页面:
声明,这里笔者还未完全开发完模块功能,只是实现了基本的数据和翻页功能。
这里先记录几点,实用操作:
一、分页sql
笔者使用的是mysql,分页关键字是limit,之后跟的内容,首个变量从0开始,意义是从第几条数据进行分页,次个变量意义是本次查询获取几条数据。如下:
<select id="getList" resultMap="ArticleResult">
SELECT *
from t_article a
WHERE a.username=#{username} ORDER BY a.postTime DESC LIMIT #{pageStart},#{pageSize}
</select>
二、级联查询
笔者在查询文章实体时需要获取文章类别的中文名,而article数据表储存了类别的id,所以可以通过id级联查询,如下:
实体内类别属性:private ArticleType articleType;
<resultMap type="Article" id="ArticleResult">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="postTime" column="postTime"/>
<result property="clickHits" column="clickHits"/>
<result property="replyHits" column="replyHits"/>
<result property="username" column="username"/>
<association property="articleType" column="type_id" select="com.gcc.modules.frontstage.dao.ArticleTypeDao.getTypeById"></association>
</resultMap>
同时,在使用resultMap作为返回结果类型时,也解决了实体属性和表字段不一致而无法赋值的问题,即在resultMap内取别名。
三、mybatis if 条件参数问题
如果使用parameterType="String",作为参数,在test条件里不能直接写属性如:
<if test="username!=null and username!='' ">
a.username=#{username}
</if>
会报错如:
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'class java.lang.String'
at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:381)
at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
……
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:280)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:80)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:386)
... 62 more
解决方案如下:
1、使用_parameter,用此关键字代替username
2、使用mybatis默认的对象名:value,用此关键字代替username
3、(推荐)有复数个判断条件参数时,修改parameterType="String",将传参类型改为map,如下:
public int getTotalArticle(String username){
Map<String,Object> map = new HashMap<String, Object>();
map.put("username",username);
return articleDao.getTotalArticle(map);
}
前端主要代码如下:
<%--主体--%>
<div class="w3-col" style="width: 55%; margin-top: 1px;margin-bottom: 15px;">
<div class="w3-container w3-white w3-round">
<div class="w3-row" style="margin-top: 5px">
<div class="w3-col" style="width: 8%;">
<span style="display: block;width: 60px;">
<img src="${pageContext.request.contextPath}${currentUser.picUrl}" style="width: 100%;height: 60px"/>
</span>
</div>
<div class="w3-col" style="width: 92%;margin-top: 20px;margin-left: 0px">
<div style="color:#666666">${currentUser.nickname}</div>
</div>
</div>
<hr style="margin-top: 10px"/>
</div>
<div class="w3-container w3-white w3-round" style="margin-top: 20px;">
<div class="w3-text-grey" style="margin-top: 5px;font-size: 11px"> ${currentUser.nickname}创建的所有主题</div>
<hr style="margin-top: 5px"/>
<div class="w3-light-grey w3-right-align w3-round" style="margin-top: -18px;font-size: 11px">
<ul class="w3-pagination w3-round" style="margin-top: 3px">
<li><a href="${pageContext.request.contextPath}/articlePersonal.html?pagination=${pagination}&flag=0" class=" w3-round">❮</a></li>
<li><a href="${pageContext.request.contextPath}/articlePersonal.html?pagination=${pagination}&flag=1" class=" w3-round">❯</a></li>
</ul>
</div>
<hr style="margin-top: 0px"/>
<c:forEach items="${articleList}" var="list">
<div class="w3-row w3-text-grey" style="font-size: 12px;margin-top: -10px;margin-bottom: 15px">
<div class="w3-col" style="width: 90%;">
<div style="font-size: 14px">${list.title}</div>
<div style="margin-top: 3px">
<span class="w3-light-grey w3-round w3-badge w3-text-dark-grey"> ${list.articleType.typeName} </span>
•
<span class="w3-text-grey"><strong>${currentUser.nickname}</strong></span>
• ${list.postTime} • 最后回复来自 zjl03505
</div>
</div>
<div class="w3-col" style="width: 10%;margin-top: 15px"><span class="w3-badge w3-text-white w3-light-grey w3-round">12</span></div>
</div>
<hr style="margin-top: 5px"/>
</c:forEach>
</div>
</div>
后端逻辑代码如下:
/**
* 文章列表
*/
@RequestMapping("articlePersonal")
public String articlePersonal(Model model, HttpServletRequest request){
String pagination = request.getParameter("pagination");
String flag = request.getParameter("flag");
Map<String,Object> articleMap=articleService.getList(pagination,flag);
model.addAttribute("articleList",articleMap.get("articleList"));
model.addAttribute("pagination",articleMap.get("pagination"));
return "modules/frontstage/articlePersonal";
}
public Map<String,Object> getList(String pagination,String flag) {
String username = (String) SecurityUtils.getSubject().getPrincipal();
int pageStart=0;//默认开始项
int pageSize=9;//每次加载项数
String temp = pagination;
int next=0;
if("".equals(pagination) || null==pagination){
pagination="0";//默认第一页
}else if("1".equals(flag)){//1表示下一页,0表示上一页
next = Integer.parseInt(pagination) + 1;
}else if("0".equals(flag)){
next = Integer.parseInt(pagination) - 1;
}
pageStart=next*9;
pagination=String.valueOf(next);
if(pageStart<0){
pageStart=0;
pagination="0";
}
if(pageStart>getTotalArticle(username)){
int lastPageSize = getTotalArticle(username) % 9;
pageStart=getTotalArticle(username)-lastPageSize;
pagination=temp;
}
List<Article> articleList = articleDao.getList(username,pageStart,pageSize);
for (Article list:articleList){
list.setPostTime(DateUntil.friendlyTime( DateUntil.stringToDate(list.getPostTime()) ));
}
Map<String,Object> map = new HashMap<String, Object>();
map.put("pagination",pagination);
map.put("articleList",articleList);
return map;
}
public int getTotalArticle(String username){
Map<String,Object> map = new HashMap<String, Object>();
map.put("username",username);
return articleDao.getTotalArticle(map);
}
sql如下:
<resultMap type="Article" id="ArticleResult">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="postTime" column="postTime"/>
<result property="clickHits" column="clickHits"/>
<result property="replyHits" column="replyHits"/>
<result property="username" column="username"/>
<association property="articleType" column="type_id" select="com.gcc.modules.frontstage.dao.ArticleTypeDao.getTypeById"></association>
</resultMap>
<sql id="articleColumns">
a.id AS "id",
a.title AS "title",
a.content AS "content",
a.type_id AS "typeId",
a.postTime AS "postTime",
a.clickHits AS "clickHits",
a.replyHits AS "replyHits",
a.username AS "username"
</sql>
<select id="getList" resultMap="ArticleResult">
SELECT *
from t_article a
WHERE a.username=#{username} ORDER BY a.postTime DESC LIMIT #{pageStart},#{pageSize}
</select>
<select id="getTotalArticle" resultType="int">
SELECT COUNT(*)
from t_article a
<where>
<if test="username!=null and username!='' ">
a.username=#{username}
</if>
</where>
</select>