首页 / 更多教程 / Mybatis中常用sql语句
Mybatis中常用sql语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Mybatis中常用sql语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1967字,纯文字阅读大概需要3分钟。
内容图文
1.变量的输入:#,$
在xnm文件中,
1) 通过 #{参数名} 来将参数放入sql语句中,根据数据类型输入
例如:select * from A表 a where a.id=#{id}
2) 通过 ${参数名} 来表示普通字符
例如:select * from A_${id} a where a.id=#{id} ,假如String id=10,
那么等价于:select * from A_10 a where a.id=‘10‘
2.条件句
A.在执行条件条件查询时,就会用到该条语句:
select * from A表 a where 1=1
<if test="a.id !=null and a.id !=‘’ "> and a.id =#{id}</if>
<if test="a.name!=null and a.name !=’’ "> and a.name =#{name}</if>
......
B.在执行插入更新语句时,
update A表 a set
<if test="a.id !=null and a.id !=’’ "> a.id =#{id},</if>
<if test="a.name!=null and a.name !=’’ "> a.name =#{name},</if>
a.age=#{age}
......
C.模糊查询
select * from A表 a where 1=1
<if test="a.id !=null and a.id !=’’ "> and a.id =#{id}</if>
<if test="a.name!=null and a.name !=’’ "> and a.name like concat(‘%’,#{name},‘%’)</if>
......
核心思想就是:对sql语句进行拼接。同时根据需要,还可以将if条件句提取出来。然后再被引用,例如:
<select id="xxx" parameterType="com.xxx.xxxx" resultType="com.xxx.yyy">
select * from A表 a where 1=1
<include refid="yyy"><include>
</select>
<sql id="yyy">
<if test="a.id !=null and a.id !=’’ "> and a.id =#{id}</if>
<if test="a.name!=null and a.name !=’’ "> and a.name =#{name}</if>
......
</sql>
D.sql中直接使用
SELECT IF(`status`=1,‘正常‘,‘禁用‘) AS `status` FROM area
Case条件语句:将值改变成其他信息输出
SELECT `name`,CASE `status`
WHEN 1 THEN ‘草稿‘
WHEN 2 THEN ‘提交‘
WHEN 3 THEN ‘审核‘
ELSE ‘No‘ END `status`
FROM 表A
3.大于小于符号
在mybatis中是无法识别sql语句中的“>”,“<”符号的,所以应该使用 “>”来代替“>”,“<”来代替“<”
Mybatis中常用sql语句
标签:select null 无法 直接 模糊查询 大于 执行 提交 date
本文系统来源:https://www.cnblogs.com/momoweiduan/p/8149540.html
内容总结
以上是互联网集市为您收集整理的Mybatis中常用sql语句全部内容,希望文章能够帮你解决Mybatis中常用sql语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。