if标签
<if test="username !=null and username !=''">
and username like concat('%',#{username}, '%')
</if>
<if test="jobs !=null and jobs !=''">
and jobs= #{jobs}
</if>
choose标签(if-else)
在使用<if>元素时,只要test属性中的表达式为true,就会执行元素中的条件语句,但是在实际应用中,有时只需要从多个选项中选择一个去执行。
<choose>
<when test="username !=null and username !="">
and username like concat('%',#{username}, '%')
</when>
<when test="jobs !=null and jobs !="">
and jobs= #{jobs}
</when>
<otherwise>
and phone is not null
</otherwise>
</choose>
where标签
<where>
<if test="username !=null and username !="">
and username like concat('%',#{username}, '%')
<if/>
<if test="jobs !=null and jobs !="">
and jobs=#{jobs}
</if>
</where>
上述配置代码中,使用<where>元素对“where 1=1”条件进行了替换,<where>元素会自动判断组合条件下拼装的SQL语句,只有<where>元素内的条件成立时,才会在拼接SQL中加入where关键字,否则将不会添加;即使where之后的内容有多余的“AND”或“OR”,<where>元素也会自动将它们去除。
set标签
在Hibernate中,如果想要更新某一个对象,就需要发送所有的字段给持久化对象,然而实际应用中,大多数情况下都是更新的某一个或几个字段。如果更新的每一条数据都要将其所有的属性都更新一遍,那么其执行效率是非常差的。有没有办法让程序只更新需要更新的字段呢?
为了解决上述情况中的问题,MyBatis中提供了<set>元素来完成这一工作。<set>元素主要用于更新操作,其主要作用是在动态包含的SQL语句前输出一个SET关键字,并将SQL语句中最后一个多余的逗号去除。
update t_customer
<set>
<if test="username !=null and username !=''">
username=#{username},
<if/>
<if test="jobs !=null and jobs !=''">
jobs=#{jobs},
</if>
<if test="phone !=null and phone !=''">
phone=#{phone},
</if>
</set>
where id=#{id} </update>
foreach标签
在实际开发中,有时可能会遇到这样的情况:假设在一个客户表中有1000条数据,现在需要将id值小于100的客户信息全部查询出来,这要怎么做呢?有人也许会说,“我可以一条一条查出来”,那如果查询200、300甚至更多也一条一条查吗?这显然是不可取的。有的人会想到,可以在Java方法中使用循环,将查询方法放在循环语句中,然后通过条件循环的方式查询出所需的数据。这种查询方式虽然可行,但每执行一次循环语句,都需要向数据库中发送一条查询SQL,其查询效率是非常低的。那么还有其他更好的方法吗?我们能不能通过SQL 语句来执行这种查询呢?
select * from t_customer where id in
<foreach item="id" index="index" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
多对一
一个学生Student(id,name,teacher)对应一个老师
<select id="getTeacher" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher">
<result property="id" column=id/>
<result property="name" column=name/>
// 复杂的属性,我们需要单独处理,对象:association 集合:collection
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getStudent" resultType="Teacher">
select * from teacher where id=#{id}
</select>
Mysql多对一查询方式
- 子查询
- 联表查询
一对多
一个老师Teacher(id,name,students)对应多个学生
<select id="getTeacher" resultMap="TeacherStudent">
select s.id,s.name from
student s,teacher t
where s.tid = t.id
</select>
<resultMap>
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<collection property="students" ofType="Student">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
典型语句
<!-- 批量插入 -->
<insert id="insertBatch" keyProperty="" useGeneratedKeys="true">
insert into test.user(name)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.name})
</foreach>
</insert>
<!-- 批量插入或按主键更新 -->
<insert id="insertOrUpdateBatch" keyProperty="" useGeneratedKeys="true">
insert into test.user(name)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.name})
</foreach>
on duplicate key update
name = values(name)
</insert>