这篇文章主要介绍了Mybatis中的常用OGNL表达式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
在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、method(args):调用类的静态方法
<bind name="name" value="@ex.mybatis.rbac.mapper.UserMaperTest@setName()"/>15、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>以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
本文标题为:Mybatis中的常用OGNL表达式
				
        
 
            
        基础教程推荐
- JavaWeb 实现验证码功能(demo) 2024-04-14
 - 运用El表达式截取字符串/获取list的长度实例 2023-08-01
 - 是否适合从javabean类更新数据库? 2023-11-04
 - 使用Java和WebSocket实现网页聊天室实例代码 2024-02-25
 - 深入理解约瑟夫环的数学优化方法 2024-03-07
 - Java编写实现窗体程序显示日历 2023-01-02
 - JSP 动态树的实现 2023-12-17
 - Java中EnvironmentAware 接口的作用 2023-01-23
 - Java+mysql实现学籍管理系统 2023-03-16
 - springboot下使用shiro自定义filter的个人经验分享 2024-02-27
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				