访问对象属性:person.name
调用方法:person.getName()
调用静态属性:@java.lang.Math@PI
调用静态方法:@java.lang.UUID@randomUUID()
调用构造器 new com.abc.entry.Person(“张三”).name
运算符: ±*/%
逻辑运算符: in , not in ,> , >=, < , <= , == ,!= (注意xml中要使用转义符)
if
choos(when,otherwise)
trim(where,set)
foreach
<select id="getUserById" resultMap="MyUser">
select * from user where
<if test="id!=null">
id=#{id}
if>
<if test="lastName!=null && lastName!=""">
and last_name like #{lastName}
if>
<if test="age!=null && lastName!=""">
and age like #{age}
if>
select>
这时,当id=null时,sql语句就会出问题
select * from user where and last_name like #{lastName} and age like #{age}
可以解决这个问题,where会判断第一个and需不需要,如果需要and会添加,不需要就省去
<select id="getUserById" resultMap="MyUser">
select * from user
<where>
<if test="id!=null">
id=#{id}
if>
<if test="lastName!=null && lastName!=""">
and last_name like #{lastName}
if>
<if test="age!=null && lastName!=""">
and age like #{age}
if>
where>
select>
但是如果把and写在语句后面就不行了,当age==null时
select * from user where id=#{id} and last_name like #{lastName} and
因为where解决where后面贴着的一个and,所以使用where要求把and写在前面.
这时,也可以
解决这个问题
<select id="getUserById" resultMap="MyUser">
select * from user
<trim prefix="where" suffixOverride="and">
<if test="id!=null">
id=#{id}
if>
<if test="lastName!=null && lastName!=""">
and last_name like #{lastName}
if>
trim>
select>
类似于switch case
<select id="getUserById" resultMap="MyUser">
select * from user
<where>
<choose>
<when test="id!=null">
id=#{id}
when >
<when test="lastName!=null && lastName!=""">
and last_name like #{lastName}
when >
<otherwise>
and age like #{age}
otherwise >
choose>
where>
select>
进行更新
<select id="getUserById" resultMap="MyUser">
update user
<set>
<if test="id!=null">
id=#{id},
if>
<if test="lastName!=null && lastName!=""">
last_name like #{lastName} ,
if>
<if test="age!=null && lastName!=""">
age like #{age}
if>
set>
select>
也可以使用trim,设置prefix=“set”,suffixOverride=","
collection;指定要遍历的集合,list类型的参数会特殊处理封装在map中,map的key就叫list
item;将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:拼接在结果的最开头
close:拼接在结果的最后面
index:索引;遍历list的时候是索引,item是当前值; 遍历map的时候是key,item就是map的value
<select id="getUserById" resultMap="MyUser">
select * from user where id in
<foreach collection="ids" item="item_id" separator="," open="(" close=")">
#{item_id}
foreach>
select>
mybatis两个内置参数
_parameter : 代表整个参数,单个参数_parameter 就是这个参数,多个参数会封装成一个map, _parameter就是这个map
_databaseId :如果databaseIdProvider标签, _databaseId就代表当前数据库别名
<select id="getUserById" resultMap="MyUser">
<if test=" _databaseId == 'mysql'">
select * from user1
<if test="_parameter!=null">
where last_name=#{lastName}
if>
<if test=" _databaseId == 'oracle'">
select * from user2
<if test="_parameter!=null">
where last_name=#{lastName}
if>
if>
select>
<select id="getUserById" resultMap="MyUser">
<bind name="_lastName" value="'%'+lastName+'%'" />
<if test=" _databaseId == 'mysql'">
select * from user1
<if test="_parameter!=null">
where last_name like #{_lastName}
if>
<if test=" _databaseId == 'oracle'">
select * from user2
<if test="_parameter!=null">
where last_name=#{lastName}
if>
if>
select>
抽取可重用的sql片段.方便后面使用
1.sql抽取,经常将要查询的列名,或者插入用的列名抽取出来
2.include来引用已经抽取的sql
3.include里面还可以定义< property name="" value="" > ,sql标签内部能使用自定义的属性${}
<sql id="insertColumn">
username,password,${test}
sql>
<insert id="add">
insert into user
( <include refid="insertColumn">
<property name="test" value="abc">
include>)
insert>