Mybatis中的常用OGNL表达式
目录
- Mybatis常用的OGNL表达式如下
 - Mybatis jstl表达式
 
在Mybatis的动态SQL和${}形式的参数中都用到了OGNL表达式。
Mybatis常用的OGNL表达式如下
1、e1 or e2:或
<if test="userEmail != null or userEmail == '1'"> </if>
2、e1 and e2:且
<if test="userEmail != null and userEmail != ''"> </if>
3、e1 == e2 或e1 eq e2:相等
<if test="userEmail == null and userEmail == ''"> </if>
4、e1 != e2 或 e1 neq e2:不等
<if test="userEmail != null and userEmail != ''"> </if>
5、e1 lt e2:小于
<if test="age lt 10">
        #{userEmail,jdbcType=VARCHAR},
</if>
6、e1 lte e2:小于等于
7、e1 gt e2:大于
8、e1 gte e2:大于等于
9、 e1 + e2(加),e1 - e2(减),e1 * e2(乘),e1/e2(除),e1%e2(余)
10、!e或not e:非,取反
11、e.method(args):调用对象方法
<if test="list != null and list.size() > 0 ">
        #{userEmail,jdbcType=VARCHAR},
</if>
12、e.property:对象属性值
<!-- 多接口参数的查询方法(@Param + javaBean方式) -->
  <select id="selectByUserIdAndEnabledUseBean" resultMap="BaseResultMap">
    select r.id, r.role_name, r.enabled, r.create_by, r.create_time, 
    u.user_name as "user.userName", u.user_email as "user.userEmail"
    from sys_user u 
    inner join sys_user_role ur on u.id = ur.user_id 
    inner join sys_role r on ur.role_id = r.id 
    where u.id = #{user.id} and r.enabled = #{role.enabled}
</select>
13、e1[e2]:按索引取值(List、数组和map)
14、@class@method(args):调用类的静态方法
<bind name="name" value="@ex.mybatis.rbac.mapper.UserMaperTest@setName()"/>
15、@class@field:调用类的静态字段值
<bind name="name" value="@ex.mybatis.rbac.mapper.UserMaperTest@NAME"/>
Mybatis jstl表达式
写了一个特别简单的小例子,使用struts1+mybatis+spring,,,其中做了一个增删改查,
结果遇到了一个特别无知的错误!以后一定要记住,不能再犯了!
我在数据库中建的表的字段是xx_xx这种格式的,例如notice_title,在pojo实体类中定义的属性是noticeTitle这种形式的,
在做查找所有数据的时候,sql语句中对各个字段起了别名,但是别名没有与pojo类的属性名对应,导致resultMap对应的类不能与自己起的别名对应,导致不能进行实体类封装值



 public ActionForward show(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        List<Notice> noticeList = noticeService.getNoticeList();
        request.setAttribute("noticeList", noticeList);
        return mapping.findForward("begin");
    }
<table border="1">
    <tr>
        <td>选择</td>
        <td>主题</td>
        <td>内容</td>
        <td>发表时间</td>
        <td>备注</td>
        <td>编辑人员</td>
    </tr>
    <c:forEach var="notices" items="${requestScope.noticeList }" >
    <tr>
        <td><input type="checkbox" name="keyid" value="${notices.keyid}"/></td>
        <td>${notices.noticeTitle}</td>
        <td>${notices.noticeContent }</td>
        <td>${notices.noticePublishTime}</td>
        <td>${notices.noticeComment}</td>
        <td>${notices.noticeEditor }</td>
    </tr>
    </c:forEach>
</table>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
 赞 (0)
                        