mybatis

发布时间 2023-03-30 03:32:16作者: 杨杨09265

1.ognl表达式

  $表示引用,#表示取值,%表示取传入字符串的值,@表示执行静态方法,去集合中数据 list[0],定义list {1,2,3,4,5},去map中数据 map[key], 定义map {"a":"1"}

  基本四则运算和比较运算

  投射

Person p1 = new Person(1, "11");
Person p2 = new Person(2, "22");
Person p3 = new Person(3, "33");

ArrayList<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);

#list.{id}
// [1,2,3]
#list.{name}
// ["11","22","33"]
#list.{? #this.id > 2}
// [{3,"33"}]
#list.{^ #this.id > 1} //id>1的第一个
// [{2,"22"}]
#list.{$ #this.id > 1} //id>1的最后一个
// [{3,"33"}]

  创建对象 new 全类名()

2.标签

  if 判断,true则执行其中内容

  

<if test="id != null">
        and id=#{id}
</if>

  choose when otherwise

  choose 的使用很像 Java 中的 switch 语法,当满足第一个 when 条件时,就不去看后续的条件,如果条件都不满足,则执行 otherwise

 trim

  

select * from user
    <trim prefix="where" prefixOverrides="or">
        <if test="a != null">
            or a=#{a}
        </if>
        <if test="b != null">
            or b=#{b}
        </if>
    </trim>

  where

  

<select id="demo">
    select * from tb
    <where>
        <if test="a != null">
            and a=#{a}
        </if>
        <if test="b != null">
            and b=#{b}
        </if>
    </where>
</select>

 会自动去除where条件内第一个 and 或者or

set功能与where类似,但是set是在开头加上set关键字

 

foreach 循环