您的位置:首页 > 数据库

Mybatis动态sql语句

2016-04-13 18:16 211 查看
在上一篇文章中有对象的实体类,测试的方法,及怎么映射接口的,这里就只写映射文件中的代码


语句一:

使用场景:当我们要获得当前插入对象的主键(主键是使用序列产生的)

这是映射文件的代码:



语句二:

使用场景:在插入数据的时候我们是使用对象去插入的,但是有时候我们不用插入表字段的全部内容,这是就要分很多中sql语句

这是就可以使用标签

其中的属性:

prefixOverrides:去掉前缀为什么字符

suffixOverrides:去掉后缀为什么字符

prefix:添加前缀为什么字符

suffix:添加后缀为什么字符

可以使用标签进行判断是不是为空

如果有多个判断不用写多个语句使用便签

其中具体的解释在代码中有注释

<insert id="insertCourse" parameterType="Course">
<!-- trim 去掉或加上字符 prefixOverrides:去掉前缀为什么字符 suffixOverrides:去掉后缀为什么字符 prefix:添加前缀为什么字符
suffix:添加后缀为什么字符 -->
<trim prefixOverrides="," suffixOverrides="," prefix="insert into course("
suffix=")">
id,
<if test="courseCode!=null">
courseCode,
</if>
<if test="courseName!=null">
courseName
</if>
</trim>
<trim prefixOverrides="," suffixOverrides="," prefix=" values("
suffix=")">
<!-- 当id中有值的时候就使用id的值 当id没有值的时候就使用序列
方法一:使用if  写两个if语句  判断条件相互对立
方法二:choose  when otherwise
-->
<choose>
<when test="id!=0">
#{id},
</when>
<otherwise>
seq_course_id.nextval,
</otherwise>
</choose>

<if test="courseCode!=null">
#{courseCode}
</if>
<if test="courseName!=null">
#{courseName}
</if>
</trim>
</insert>


语句三:

使用场景:我们在查询的数据的时候有很多条件,但是使用我们原来的语句我们就要写很多的sql语句,我们现在使用映射文件的配置我们就可以完美的解决我们所面临的问题

我们使用set标签很容易的帮我们解决问题,当存在逗号的多余的时候能帮我们解决逗号的问题

是去掉后缀的逗号:注意

<update id="updateCourse" parameterType="Course">
update course
<!-- set 自动添加一个前缀set,然后去掉多余的逗号 -->
<set>
<if test="courseName !=null">
courseName=#{courseName},
</if>
<if test="courseCode !=null">
courseCode=#{courseCode}
</if>
</set>
<if test="id!=0">
where id=#{id}
</if>
</update>


语句三:

使用场景:在使用查询语句的时候我们也会面临我们查询的条件的不同,也要写很多的sql语句

我们是用where很容易实现

是去掉前缀的or或and

<select id="getCourse" parameterType="Course" resultType="Course">
select * from course
<!-- 自动添加一个where 并且去掉前缀的 or 或者 and  记得 set是去后缀  where是去前缀-->
<where>
<if test="id!=0">
id=#{id}
</if>
<if test="courseName!=null">
or courseName=#{courseName}
</if>
<if test="courseCode!=null">
and courseCode=#{courseCode}
</if>
</where>
</select>


语句四:

场景:操作参数是一个集合或数组时,要使用循环来读取参数 foreach

在查询的参数因为里面是int的类型,所以我在这里是使用的parameterType:int,mybatis中对这个类型的验证不是很严格

注意参数的类型: List集合对应的list,数组对应的是array

<select id="getCourseByList" parameterType="int" resultType="course">
<!-- 操作参数是一个集合或数组时,要使用循环来读取参数 foreach
select * from course where id in(10,11,12);-->
<!-- collection:集合的类型  list  array
open:前缀
close:后缀
separator:数据与数据之间的分割符
item:集合的数据
-->
<foreach collection="list" open="select * from course where id in(" separator="," close=")" item="ids">
#{ids}
</foreach>
</select>


语句五:

使用场景:批量插入对象信息

在说这个批量操作的时候我们先回顾一下一些sql语句吧,我在学习的时候,因为经常没有写过这些sql语句我都忘记了

sql语句:
--备份表 含数据
create table bak_course as select * from course;
--备份表  不含数据
create table bak_course2 as select * from course where 1!=1;

--复制表的数据
insert into bak_course2 select * from  course;
--一次插入多条数据
insert into COURSE
select 10001,'yc001','java1' from dual union
select 10002,'yc002','java2' from dual union
select 10003,'yc003','java3' from dual union
select 10004,'yc004','java4' from dual


<!-- 批量插入数据 -->
<insert id="insertCourses" parameterType="Course">
<foreach collection="list" item="courses" separator="union" open="insert into course ">
select #{courses.id},#{courses.courseCode},#{courses.courseName} from dual
</foreach>
</insert>


这就是我现在所学习的几种常见的动态sql语句,后面的学习我会在这里一一记录的!也是一个学习的见证吧!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis sql