动态SQL

动态SQL


学习目标

1.了解常用动态SQL元素及其作用
2.掌握动态SQL中主要元素的使用

动态SQL中的元素

一.动态SQL有什么用

在这里插入图片描述

二.动态SQL中的元素

在这里插入图片描述

if元素

1.简介
在这里插入图片描述
2.实际中的作用
在这里插入图片描述
3.代码演示
在配置文件中

<!-- <if>元素使用 -->
	<!-- parameterType为传入参数类型 resultType为返回参数类型 -->
	<select id="findCustomerByNameAndJobs"
		parameterType="XXX"
		resultType="XXX">
		select * from t_customer
		<where>
			<if test="username != null and username !=''">
				and username like concat('%',#{username},'%')
			</if>
			<if test="jobs != null and jobs !=''">
				and jobs=#{jobs}
			</if>
		</where>
	</select>

根据查询对象,进行组合查询。有符合条件的就匹配输出。
测试方法:

/**
	 * 根据客户姓名和职业组合查询客户信息列表
	 */
	@Test
	public void findCustomerByNameAndJobsTest() {
		// 通过工具类生成SqlSession对象
		SqlSession session = MybatisUtils.getSession();
		// 创建Customer对象,封装需要组合查询的条件
		Customer customer = new Customer();
		customer.setUsername("jack");
		customer.setJobs("teacher");
		// 执行SqlSession的查询方法,返回结果集
		List<Customer> customers = session.selectList("XXX.findCustomerByNameAndJobs",
				customer);
		// 输出查询结果信息
		for (Customer customer2 : customers) {
			// 打印输出结果
			System.out.println(customer2);
		}
		// 关闭SqlSession
		session.close();
	}

测试结果:
在这里插入图片描述

2.choose以及其子元素

1.学前思考:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.代码演示
配置文件

<!-- <choose>(<when>,<otherwise>)元素使用 -->
	<!-- parameterType为传入参数类型 resultType为返回参数类型 -->
	<select id="findCustomerByNameOrJobs"
		parameterType="XXX"
		resultType="XXX">
		select * from t_customer
		<trim prefix="where" prefixOverrides="and">
			<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>
		</trim>
	</select>

测试文件:

@Test
	public void findCustomerByNameOrJobsTest() {
		// 通过工具类生成SqlSession对象
		SqlSession session = MybatisUtils.getSession();
		// 创建Customer对象,封装需要组合查询的条件
		Customer customer = new Customer();
		// customer.setUsername("jack");
		customer.setJobs("teacher");
		// 执行SqlSession的查询方法,返回结果集
		List<Customer> customers = session.selectList("cn.pdsu.edu.mapper.CustomerMapper.findCustomerByNameOrJobs",
				customer);
		// 输出查询结果信息
		for (Customer customer2 : customers) {
			// 打印输出结果
			System.out.println(customer2);
		}
		// 关闭SqlSession
		session.close();
	}

测试结果
在这里插入图片描述
注:当有两个查询添加时,只对第一个查询添加添加进行匹配查询。

3.where和trim元素

都是动态拼接移出元素
引出:
在这里插入图片描述
在这里插入图片描述
注解:只要有条件成立就会自动加上where和去除多余元素,例如上图中以一个and。
trim 元素与where元素大致相同,只不过不限于去除and:
在这里插入图片描述

perfix是只要有符合条件的自动添加where,prefixOverrides是设置需要去除的元素。

4.set元素

为什么要引入set元素.
在这里插入图片描述
答:使用set元素可以根据要更新的条件动态更引数据的属性,不用因为改一个数据就要更新所有的属性

在这里插入图片描述
注:会自动删除最后一个逗号
代码演示:
配置文件:

<!-- <set>元素 -->
	<!-- parameterType为传入参数类型 更新操作没有返回参数 -->
	<update id="updateCustomer"
		parameterType="XXX">
		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>

测试方法:

//根据id进行更新操作
public void updateCustomerTest() {
		// 通过工具类生成SqlSession对象
		SqlSession sqlSession = MybatisUtils.getSession();
		// 创建Customer对象,并向其中对象中添加数据
		Customer customer = new Customer();
		customer.setId(3);
//		customer.setPhone("6666666666");
//		customer.setJobs("programmer");
		customer.setUsername("Lily");
		// 执行SqlSession的更新方法,返回影响的行数
		int rows = sqlSession.update("XXX.updateCustomer", customer);
		// 通过返回结果进行判断
		if (rows > 0) {
			System.out.println("您成功修改了" + rows + "条信息");
		} else {
			System.out.println("更新失败");
		}
		// 事务提交
		sqlSession.commit();
		// 关闭SqlSession
		sqlSession.close();
	}

结果
在这里插入图片描述

5.foreach元素

引入
在这里插入图片描述
解决方法:
在这里插入图片描述
参数属性
在这里插入图片描述
代码演示:
配置文件:

<!-- <foreach>元素使用 -->
	<!-- parameterType为传入参数类型 resultType为返回参数类型 -->
	<select id="findCustomerByIds" parameterType="List"
		resultType="XXX">
		select * from t_customer where id in
		<foreach item="id" index="index" collection="list" open="("
			separator="," close=")">
			#{id}
		</foreach>

	</select>

测试文件:

/**
	 * 跟根据客户编号批量查询客户信息
	 */
	@Test
	public void findCustomerByIdsTest() {
		// 获取sqlSession
		SqlSession session = MybatisUtils.getSession();
		// 创建List集合,封装id
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(1);
		ids.add(2);
		// 执行SqlSession方法,返回结果集
		List<Customer> customers = session.selectList("XXX.findCustomerByIds", ids);
		// 打印输出结果
		for (Customer customer : customers) {
			System.out.println(customer);
		}
		session.close();
	}

运行结果:
在这里插入图片描述

6.bind元素

在这里插入图片描述
在这里插入图片描述
代码:

<!-- <bind>元素使用 -->
	<select id="findCustomerByName"
		parameterType="XXX"
		resultType="XXX">
		<bind name="pattern_username" value="'%'+_parameter.getUsername()+'%'"/>
		select * from t_customer where username like #{pattern_username}
		
		</select>
/**
	 * 根据客户信息模糊查询
	 */
	@Test
	public void findCustomerByNameTest() {
		// 获取sqlSession
		SqlSession session = MybatisUtils.getSession();
		// 创建Customer对象,封装查询条件
		Customer customer = new Customer();
		customer.setUsername("j");
		// 执行SqlSession方法,返回结果集
		List<Customer> customers = session.selectList("XXX.findCustomerByName", customer);
		for (Customer customer2 : customers) {
			System.out.println(customer2);
		}
		session.close();
	}

测试结果:
在这里插入图片描述

本章小结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值