ibatis 动态查询
http://www.iteye.com/topic/393042最近做了很多动态的查询,尤其是排序,以及一些状态字段,所以就做了一个总的动态查询,以不变应万变,呵呵
ibatis 里面的sql代码:
<![CDATA[
select * from p_Topic
]]>
<dynamic prepend=" WHERE ">
<isPropertyAvailable property="authorId">
<isNotNull property="authorId" prepend=" and ">
authorId=#authorId#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="marketId">
<isNotNull property="marketId" prepend=" and ">
marketId=#marketId#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="isDelete">
<isNotNull property="isDelete" prepend=" and ">
isDelete=#isDelete#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="isBest">
<isNotNull property="isBest" prepend=" and ">
isBest=#isBest#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="statusStr">
<isNotNull property="statusStr" prepend=" and ">
$statusStr$
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="marketIdList">
<isNotNull property="marketIdList" prepend=" and marketId in ">
<iterate property="marketIdList" conjunction="," close=")" open="(">
#marketIdList[]#
</iterate>
</isNotNull>
</isPropertyAvailable>
</dynamic>
<dynamic prepend=" order by ">
<isPropertyAvailable property="orderStr">
<isNotNull property="orderStr">
$orderStr$
</isNotNull>
</isPropertyAvailable>
</dynamic>
<dynamic>
<isPropertyAvailable property="begin">
<isNotNull property="begin">
limit #begin#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="max" prepend=" , ">
<isNotNull property="max">
#max#
</isNotNull>
</isPropertyAvailable>
</dynamic>
</select>
<select id="getTopicCount" resultClass="java.lang.Long"
parameterClass="map">
<![CDATA[
select count(id) from p_Topic
]]>
<dynamic prepend=" WHERE ">
<isPropertyAvailable property="authorId">
<isNotNull property="authorId" prepend=" and ">
authorId=#authorId#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="marketId">
<isNotNull property="marketId" prepend=" and ">
marketId=#marketId#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="isDelete">
<isNotNull property="isDelete" prepend=" and ">
isDelete=#isDelete#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="isBest">
<isNotNull property="isBest" prepend=" and ">
isBest=#isBest#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="statusStr">
<isNotNull property="statusStr" prepend=" and ">
$statusStr$
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable property="marketIdList">
<isNotNull property="marketIdList" prepend=" and ">
<iterate property="marketIdList" conjunction="," close=")" open=" marketId in (">
</iterate>
</isNotNull>
</isPropertyAvailable>
</dynamic>
</select>
这里需要注意的是:
①#xxx# 代表xxx是属性值,map里面的key或者是你的pojo对象里面的属性,ibatis会自动在它的外面加上引号,表现在sql语句是这样的 where xxx = 'xxx' ;
而$xxxx$ 则是把xxxx作为字符串拼接到你的sql语句中,比如 order by topicId , 如果你不用$来拼接而用#的话,外面就会被加上引号的哦 比如你的语句这样写 ... order by #xxx# (xxx就是你传进来的字符串topicId),ibatis 就会把他翻译成 order by 'topicId' 这样就报错了 ,用$的结果就是这样 order by topicId
②这里的iterate
<isNotNull property="marketIdList" prepend="and">
<iterate property="marketIdList" conjunction="," open=" marketId in (" close=")">
#marketIdList[]#
</iterate>
</isNotNull>
</isPropertyAvailable>
return getSqlMapClientTemplate().queryForList("getTopics", map);
}
Integer orderby, Integer status, Pagination pagination) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("authorId", authorId);
map.put("isDelete", false);
map.put("marketIdList", marketIdList);
map.put("orderStr", "这里你组装你的order字符串");
map.put("statusStr","这里你组装你的status字符串");
map.put("begin", pagination.getOffset());
map.put("max", pagination.getPageSize());
//这个getTopicCount()方法和getTopics()大体是一致的,所以我的dao里面省略了它
Long total = topicDao.getTopicCount(map);
if (total == 0) {
return new ArrayList<Topic>();
} else {
pagination.setTotal(total);
List<Topic> res = topicDao.getTopics(map);
return res;
}
}
/**
*
*/
private static final long serialVersionUID = -851973667810710701L;
private Long id;
private Long authorId;
private String authorName;
private Long marketId;
private String title;
private String tags;
private String content;
private Date pubdate;
private Integer isBest;
private Integer status;
private Integer isDelete;
private Integer clickCount;
private Integer replyCount;
private Date lastReplyTime;
//getter and setter 省略...
}
/**
* 要查看的页码
*/
private int page;
/**
* 每页显示数
*/
private int pageSize;
/**
* 一共有多少页
*/
private int totalPage;
/**
* 一共有多少条记录
*/
private long total;
/**
* 当前页的记录数
*/
private int size;
/**
* 只需要topxx,不需要页数信息了
*/
private boolean topOnly;
/**
*从第几条记录开始
*/
private int offset;
public void setOffset(int offset) {
this.offset = offset;
}
public Pagination(int page, int pageSize) {
this.page = page;
this.pageSize = pageSize;
}
public Pagination() {
}
public boolean require() {
return pageSize > 0 ? true : false;
}
public int from() {
return page * pageSize;
}
public int to() {
return from() + size;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
if (pageSize > 0) {
this.totalPage = (int) Math.ceil(total / (double) pageSize);
} else {
this.totalPage = 1;
}
if (page >= totalPage) {
page = totalPage - 1;
}
if (page < 0)
page = 0;
if (pageSize > 0) {
if (page < totalPage - 1)
this.size = pageSize;
else
this.size = (int) (total % pageSize);
} else {
this.size = (int) total;
}
offset=page * pageSize;
}
public int getOffset() {
return offset;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public boolean isTopOnly() {
return topOnly;
}
public void setTopOnly(boolean topOnly) {
this.topOnly = topOnly;
}
}
ibatis 动态查询的更多相关文章
- ibatis动态查询条件
ibatis的调试相对困难,出错的时候主要依据是log4生成的log文件和出错提示,这方面要能比较熟练的看懂. 下面这个配置基本上包含了最复杂的功能:分页\搜索\排序\缓存\传值Hash表\返回has ...
- ibatis动态查询
在复杂查询过程中,我们常常需要根据用户的选择决定查询条件,这里发生变化的并不只是SQL 中的参数,包括Select 语句中所包括的字段和限定条件,都可能发生变化.典型情况,如在一个复杂的组合查询页面, ...
- ibatis动态多条件查询及模糊查询(oracle,mysql,sql)
首先是模糊查询的问题,开始时我使用如下条件:select * from user where name like '%#value#%'. 可是怎么也不行,好像还报错了.后来在网上找到了解决方法,就是 ...
- Ibatis动态(dynamic)查询
Ibatis的动态查询使得数据操作变得非常的灵活,下次举出了常用的动态查询的属性信息: Ibatis配置信息 <!-- Dynamic Sql --> <typeAlias a ...
- ibatis Dynamic总结(ibatis使用安全的拼接语句,动态查询)
ibatis中使用安全的拼接语句,动态查询,ibatis比JDBC的优势之一,安全高效 说明文字在注释中 一.引入 一个小例子 <select id="selectAllProduc ...
- ibatis 取消查询动态列的缓存
ibatis在查询结果列不确定(或是动态变化)的情况下,会因为列缓存的原因导致变化后的列数据查不出来 解决方法是: select标签有个属性remapResults,该属性默认值为false,设置成r ...
- 转:ibatis动态sql
转:ibatis动态sql 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的 IF-ELSE条件语句和一连串 ...
- Ibatis动态拼装sql,常用标签总结及举栗子。
今天得到项目经理一项任务,就是拼装sql,第一次见到,不是太懂,赶紧回来睡一觉再说,由于这次的项目orm使用的是ibatis框架,所以需要使用动态拼装sql,或者是ognl语言,这门语言不是专属于ib ...
- IBATIS动态SQL(1)
转:IBATIS动态SQL 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的 ...
随机推荐
- System Center VMM请注意不同语言版本的差异
在私有云的项目中,经常需要判断System Center一些组件的连接是否OK. 我这里有开发,和测试两个环境,开发是英文版的System Center VMM,测试用的是中文版的System Cen ...
- Ubuntu 安装 tensorflow-gpu + keras
转载请注明: 仰望大牛的小清新:http://www.cnblogs.com/luruiyuan/ 本文原网址:http://www.cnblogs.com/luruiyuan/p/6660142.h ...
- 51nod 多重背包问题(动态规划)
多重背包问题 一个背包,承量有限为W,有n种物体,第i种物体,价值Vi,占用重量为 Wi,且有Ci件,选择物品若干放入背包,使得总重量不超过背包的承重.总价值最大? 输入 第1行,2个整数,N和W中间 ...
- [BZOJ3205][APIO2013]Robot(斯坦纳树)
3205: [Apio2013]机器人 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 1007 Solved: 240[Submit][Status ...
- [BZOJ 1833] 数字计数
Link: BZOJ 1833 传送门 Solution: 比较明显的数位DP 先预处理出1~9和包括前导0的0的个数:$pre[i]=pre[i-1]*10+10^{digit-1}$ (可以分为首 ...
- [xsy2369]取名字
真是道挺好的题,做一道题学了挺多东西 从操作入手比较困难,所以对硬币进行讨论 考虑一个硬币$(A,B)$,假设$A\lt B$,那么我们可以把操作分成三类 第一类$T_j\lt A$,这种操作是没用的 ...
- checkbox与文字对齐方式(同样可用于radio单选框)
checkbox 在chrome 和 firefox中 间距是由margin撑开, IE中则是由padding,并且无法设置边框: 主要参考大神的博客,http://www.zhangxinxu. ...
- 自己做的javascript简易计算器
html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ...
- ORACLE查看并修改最大连接数的具体步骤
第一步,在cmd命令行,输入sqlplus 第二步,根据提示输入用户名与密码 1. 查看processes和sessions参数 SQL> show parameter processes ...
- android多线程-AsyncTask之工作原理深入解析(上)
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...