- 6
- 0
- 约1.27万字
- 约 12页
- 2017-12-20 发布于河南
- 举报
mybaits中sql语句新功能
1、Mybatis主键自动生成:
在MYSQL、sql server等支持主键自动增长的数据库中!mybatis插入时,对自动生成的字段的写法:
insert id=”insertAuthor” parameterType=”类型”useGeneratedKeys=”true”
keyProperty=“自动生成的字段名”
insert into(uername, password, email )
vlaues(#{username}, #{password}, #{email})
insert
对不支持自动生成功能的数据库,mybatis提供以下写法,不过,此写法生成ID,是随机的!
insert id=”insertAuthor” parameterType=“类型”
selectKey resultType=java.lang.Long order=AFTER keyProperty=id
SELECT LAST_INSERT_ID() AS id
/selectKey
Insert into 数据库名
(id, username, password, email )
Values (#{id},#{username},#{password},#{email})
/insert
2、Mybaits中SQL语句包含:
sql id=”Columns” select/update/delete/insert 等操作/sql
select id=”selectUser” parameterType=”int”resultType=”hashMap”
Select include refid=”Columns”/
From 表名或where 条件
Where id=#{id}
/select
3、Mybatis动态SQL语句
a、if语句
select 字段名 from 表名 where state=”1”
if test =” 字段名!=null”
AND 条件
/if
b、choose, when , otherwise
select 字段名 from 表名 where state=”1”
choose
when test=”字段名!=null”
And 条件
/when
when test=”条件表达式”
And 条件
/when
otherwise
And 条件
/otherwise
/choose
C、trim, where , set
1, where
Select 字段名 from 表名
where
条件
/where
注 : 加 where 后则确保一定是 where 开头
2, set
Update 表名
set
if test=”条件”字段名=#{参数}/if
/set
Where 条件
D、foreach
通常构建在in条件中
Select 字段名 from 表名
Where 字段名 in
foreach item=”参数名” index=”index” collection=”list”
Open=”(” separator=”,” close=”)”
#{参数名}
/foreach
E、作用例:批量删除
delete?id?=?delete?parameterType?=?java.util.List??
????![CDATA[??
???????delete?from?tests?where?id?in??
??? ?]]??
?? ??foreach?collection=list?item?=?要删除的id?open=(?separator=,?close=)
#{?要删除的id}??
??? ??/foreach??
/delete??
F、模糊查询:
select 字段名 from 表名 where 字段名 like % #{参数} %
原创力文档

文档评论(0)