mybatis的使用-Mapper文件各种语法介绍
一、查询
mybatis自定义查询条件,queryString、queryMap、limit,Mapper文件写法如下:
<select id="getByQueryParam" parameterType="com.systom.base.BaseDaoQueryParam" resultMap="BaseResultMap"> SELECT * FROM user WHERE 1 = 1 <if test="paramString != null"> and ${paramString} </if> <foreach collection="paramMap.keys" item="k" separator=""> <if test="null != paramMap[k]"> and ${k} = #{paramMap.${k}} </if> </foreach> <if test="paramInt1 != null and paramInt1 > 0 and paramInt2 != null and paramInt2 > 0"> limit #{paramInt1,jdbcType=INTEGER}, #{paramInt2,jdbcType=INTEGER} </if> </select>
以及传入参入的bean类:
package com.systom.base; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class BaseDaoQueryParam implements Serializable { private static final long serialVersionUID = -8917191044499296040L; private String paramString; private Map<String, Object> paramMap = new HashMap<String, Object>(); private int paramInt1; private int paramInt2; private String orderBy; private String orderType; public BaseDaoQueryParam(String paramString, Map<String, Object> paramMap, int paramInt1, int paramInt2) { super(); this.paramString = paramString; if(paramMap != null) this.paramMap = paramMap; this.paramInt1 = paramInt1; this.paramInt2 = paramInt2; } public BaseDaoQueryParam(String paramString, Map<String, Object> paramMap, int paramInt1, int paramInt2, String orderBy, String orderType) { super(); this.paramString = paramString; if(paramMap != null) this.paramMap = paramMap; this.paramInt1 = paramInt1; this.paramInt2 = paramInt2; this.orderBy = orderBy; this.orderType = orderType; } public String getParamString() { return paramString; } public void setParamString(String paramString) { this.paramString = paramString; } public Map<String, Object> getParamMap() { return paramMap; } public void setParamMap(Map<String, Object> paramMap) { this.paramMap = paramMap; } public int getParamInt1() { return paramInt1; } public void setParamInt1(int paramInt1) { this.paramInt1 = paramInt1; } public int getParamInt2() { return paramInt2; } public void setParamInt2(int paramInt2) { this.paramInt2 = paramInt2; } public String getOrderBy() { return orderBy; } public void setOrderBy(String orderBy) { this.orderBy = orderBy; } public String getOrderType() { return orderType; } public void setOrderType(String orderType) { this.orderType = orderType; } }
补充 知识:mybatis的mapper文件的大于号特殊符号使用
第一种方法:
用了转义字符把>和<替换掉,然后就没有问题了。
SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DATE
附:XML转义字符
< |
< |
小于号 |
> |
> |
大于号 |
& |
& |
和 |
' |
' |
单引号 |
" |
" |
双引号 |
第二种方法:
因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析
你的可以写成这个:
mapper文件示例代码
<![CDATA[ when min(starttime)<='12:00' and max(endtime)<='12:00' ]]>
在mybatis 的mapper配置文件sql语句中, 有时用到 大于, 小于等等的比较, 直接写在里面就被当做标签的开头来处理了, 所以不可.现在又2种解决方法:
一, 用<![CDATA[ ]]>标识,例如:
<if test="menu.authority != null"> <![CDATA[ and authority < #{menu.authority}]]> </if>
其中不但能用大于'>', 小于'<', 小于等于'<=', 大于等于'>=' 也是可以的.
二, 转义, 例如:
<if test="menu.authority != null"> and authority < #{menu.authority} </if>
如此这般......
同样可以可以和等号'='一起来使用, 来表示大于等于, 小于等于等.如
<if test="menu.authority != null"> and authority >= #{menu.authority} </if>