MyBatis——动态SQL

本文深入探讨MyBatis框架中动态SQL的使用方法,包括if、choose、where、trim、foreach和set元素的详细解析及示例代码,帮助读者掌握动态SQL的灵活性和强大功能。

动态SQL

第一节 if条件

第二节 choose,where和otherwise条件

第三节 where条件

1.自动加上where

2.如果where字句以and 或者 or开头,则自动删除第一个and或or

第四节 trim条件

功能和where元素类似,提供了前缀、后缀功能,更加灵活

第五节 foreach条件

第六节 set条件

1.自动加上set

2.自动删除最后一个逗号“,”

代码

Student.java

省略了getter和setter、toString方法

package com.fzhiy.entity;

public class Student {
	
	private Integer id;//自动生成
	private String name;
	private Integer age;
}

StudentDao

package com.fzhiy.dao;

import java.util.List;
import java.util.Map;

import com.fzhiy.entity.Student;

public interface StudentDao {
	
	public List<Student> searchStudents(Map<String,Object>map);	
	
	public List<Student> searchStudents2(Map<String,Object>map);

	public List<Student> searchStudents3(Map<String,Object>map);

	public List<Student> searchStudents4(Map<String,Object>map);
	
	public List<Student> searchStudents5(Map<String,Object>map);

	public int updateStudent(Student student);
}

StudentDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fzhiy.dao.StudentDao">
	
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </resultMap>
    
    <select id="searchStudents" parameterType="Map" resultMap="StudentResult">
        select * from t_student
         where gradeId=#{gradeId}
         <if test="name!=null">
             and name like #{name}
         </if>
         <if test="age!=null">
             and age=#{age}
         </if>
    </select>
    
    <select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
        select * from t_student
         <choose><!-- 标志通过某个字段来查询 ,  注意是 ==   -->
             <when test="searchBy=='gradeId'" >
                 where gradeId=#{gradeId}
             </when>
             <when test="searchBy=='name'">
                 where name like #{name}
             </when>
             <otherwise>
                 where age=#{age}
             </otherwise>
         </choose>
    </select>
    
    <select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
        select * from t_student
		<where>
	         <if test="gradeId!=null">
	             gradeId=#{gradeId}
	         </if>
	         <if test="name!=null">
             	and name like #{name}
	         </if>
	         <if test="age!=null">
	             and age=#{age}
	         </if>
         </where>
    </select>
    
    <select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
        select * from t_student
         <trim prefix="where" prefixOverrides="and/or">     <!-- 和where元素功能相同 -->
	         <if test="gradeId!=null">
	             gradeId=#{gradeId}
	         </if>
	         <if test="name!=null">
             	and name like #{name}
	         </if>
	         <if test="age!=null">
	             and age=#{age}
	         </if>
         </trim>
    </select>
    
    <select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
        select * from t_student
		<if test="gradeIds!=null">
		    <where>
		        gradeId in 
		        <foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
		            #{gradeId}
		        </foreach>
		    </where>
		</if>
    </select>
    
    <update id="updateStudent" parameterType="Student">
        update t_student
        <!--  set元素自动删除最后一个逗号  -->
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>

小结

where和set元素较为常用,必须掌握。trim元素很强大,灵活,多用。

好的,我可以回答你关于MyBatis中if标签的使用方式。 在MyBatis中,if标签通常用于动态生成SQL语句。它可以用来判断某个字段是否为空,从而根据不同情况生成不同的SQL语句。 以下是一些使用if标签的示例: 1. 判断一个参数是否为null或空字符串: ``` <select id="selectUser" parameterType="java.lang.String" resultType="User"> SELECT * FROM users <where> <if test="username != null and username != ''"> AND username = #{username} </if> </where> </select> ``` 这个例子中,如果传入的username参数不为null或空字符串,则会将其加入到SQL语句中,生成类似于“AND username = 'xxx'”这样的条件语句。 2. 判断一个对象的某个属性是否为null: ``` <select id="selectUser" parameterType="User" resultType="User"> SELECT * FROM users <where> <if test="email != null"> AND email = #{email} </if> </where> </select> ``` 这个例子中,如果传入的User对象的email属性不为null,则会将其加入到SQL语句中,生成类似于“AND email = 'xxx'”这样的条件语句。 3. 判断一个集合是否为空: ``` <select id="selectUser" parameterType="java.util.Map" resultType="User"> SELECT * FROM users <where> <if test="ids != null and ids.size() > 0"> AND id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </if> </where> </select> ``` 这个例子中,如果传入的Map对象中包含一个名为ids的集合属性且不为空,则会将其加入到SQL语句中,生成类似于“AND id IN (1,2,3)”这样的条件语句。 以上是一些if标签的使用示例,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值