MyBatis中的动态SQL

动态 SQL 是 MyBatis 的强大特性之一。避免了开发人员在后续根据不同的条件进行拼接SQL语句;

动态 SQL 只有几个基本元素,与 JSTL 或 XML 文本处理器相似,十分简单明了,大量的判断都可以在 MyBatis 的映射 XML 文件里配置,以达到许多需要大量代码才能实现的功能。
动态 SQL 大大减少了编写代码的工作量,更体现了 MyBatis 的灵活性、高度可配置性和可维护性。

动态 SQL 只有几个基本元素,与 JSTL 或 XML 文本处理器相似,十分简单明了,大量的判断都可以在 MyBatis 的映射 XML 文件里配置,以达到许多需要大量代码才能实现的功能。

动态 SQL 大大减少了编写代码的工作量,更体现了 MyBatis 的灵活性、高度可配置性和可维护性。

  1. MyBatis中的if标签

MyBatis if 类似于 Java 中的 if 语句,是 MyBatis 中最常用的判断语句。使用 if 标签可以节省许多拼接 SQL 的工作,把精力集中在 XML 的维护上。

if 语句使用方法简单,常常与 test 属性联合使用。语法如下。

•	<if test="判断条件">
•	    SQL语句
•	</if>

当判断条件为 true 时,才会执行所包含的 SQL 语句。

当where后面的条件较为多的时候,我们一般都会把对应条件都放在对应的if标签中

对应接口文档内容:

public Student selectByCon (Student student);

对应的映射内容XML文件:有两种写法:

1

<select id="selectByCon" resultType="student">
		select * from `student`
	where 1=1
		<if test="no!=null and no!=''">
			and no = #{no}
		</if>
		<if test="name!=null and name!=''">
			and name = #{name}
		</if>
	</select>

因为不用where的标签,并且我们看到,每个if标签中,条件的前面都有加上and这个词,所以如果没有<where>标签时,我们需要在where后面加上一个恒等式,这样才能避免拼接的时候出现问题;

<if>标签中,对应的test后面写的内容是,如果是这个值不为空的时候,会被拼上SQL语句,如果为空的时候,就不会被拼接上去

2、第二种方法

<select id="selectByCon" resultType="student">
		select * from `student`
		<where>
		<if test="no!=null and no!=''">
			and no = #{no}
		</if>
		<if test="name!=null and name!=''">
			and name = #{name}
		</if>
		</where>
	</select>

通过<where>标签去进行修饰,当判断如果对应的<if>标签中的内容不为空的时候,会自动将前面的and给省略掉

If标签中,他只能判断是否有没有,没有就不会接到对应SQL中,不会有其他的一个操作

MyBatis choose、when和otherwise标签

 MyBatis 中动态语句 choose-when-otherwise 类似于 Java 中的 switch-case-default 语句。由于 MyBatis 并没有为 if 提供对应的 else 标签,如果想要达到<if>...<else>...</else> </if> 的效果,可以借助 <choose>、<when>、<otherwise> 来实现。

动态语句 choose-when-otherwise 语法如下。

<choose>
    <when test="判断条件1">
        SQL语句1
    </when >
    <when test="判断条件2">
        SQL语句2
    </when >
    <when test="判断条件3">
        SQL语句3
    </when >
    <otherwise>
        SQL语句4
    </otherwise>
</choose>

choose 标签按顺序判断其内部 when 标签中的判断条件是否成立,如果有一个成立,则执行相应的 SQL 语句,choose 执行结束;如果都不成立,则执行 otherwise 中的 SQL 语句。这类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

例子:

接口文档

	public Student selectByCon2 (Student student);

对应XML文件内容 

<select id="selectByCon2" resultType="student">
	select * from student 
	<where>
	<choose>
	<when test="no!=null and no!=''">
	no=#{no}
	</when>
	<when test="name!=null and name!=''">
	name=#{name}
	</when>
	<otherwise>
	age=#{age}
	</otherwise>
	
	</choose>
	</where>
	</select>

MyBatis Set标签

 在 Mybatis 中,update 语句可以使用 set 标签动态更新列。set 标签可以为 SQL 语句动态的添加 set 关键字,剔除追加到条件末尾多余的逗号。

根据 id 修改对应student的值

接口文档:

public void updateStudent(Student student);

对应XML文件配置

<update id="updateStudent">
	update student 
	<set>
		<if test="name!=null and name!=''">
			name=#{name},
		</if>
		<if test="age!=null and age!=''">
			age=#{age},
		</if>
	</set>
	<where>
		no=#{no}
	</where>
	</update>

 对应test文件:

private SqlSessionFactory getFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		// 初始化mybatis,创建SqlSessionFactory类实例
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		return sqlSessionFactory;
	}

@Test
	public void updateStudent() throws IOException {
		SqlSession session=getFactory().openSession();
		StudentMapper mapper =session.getMapper(StudentMapper.class);	
		Student stu =new Student();
		stu.setNo("3");
		stu.setAge(3);
		stu.setName("nini");
		mapper.updateStudent(stu);
		session.commit();
		
		
	}

其中update中的no我们也需要在test中用set方法进行放入

其他的name\age设置也是通过该方法放入

在执行完对应test后,我们一定要记得commit,这样才能将其写入我们的数据库中,进行更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值