mybatis 动态Sql实验,bind foreach where ,其中包含实例,更改了xml跟测试类,建议理清思路,自己

需要的包

image-20210429093526464

建表

image-20210429092432272

(1) 在实验2结果代码的基础上,修改CustomerMapper.xml,使用元素编写动态SQL语句,完成根据客户职业查找客户信息。

<!-- 动态SQL测试,通过工作查个人 -->
	<select id="findCustomerByJob" parameterType="com.xiucai.po.Customer" resultMap="resultMap">
	select * from t_customer where 1=1
	<if test="jobs!=null and jobs!='' ">
	and jobs=#{jobs}
	</if>
	</select>

(2) 在测试类中编写测试方法findCustomerByJobTest()进行测试

package com.xiucai.test;

import java.util.ArrayList;
import java.util.List;

import javax.websocket.Session;

import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import com.xiucai.po.Customer;
import com.xiucai.util.SqlSessionUtil;

public class findCustomerByJobTest {
   
//	测试根据工作动态sql测试顾客
	@Test
	public void findByJobs() {
		   SqlSession sqlSession=SqlSessionUtil.getSqlSession();
		
		Customer customer=new Customer();
		
		customer.setJobs("cooker");
	
		List<Customer>customers=new ArrayList<Customer>();
	customers=sqlSession.selectList("com.xiucai.Mapper.CustomerMapper.findCustomerByJob", customer);
		for (Customer customer2 : customers) {
			System.out.println(customer2);
		}
	}
	
}

执行结果:

image-20210429095123735

(3) 使用choose>、、元素编写动态SQL语句,完成如下场景操作:当客户名称不为空,则根据客户名称进行客户筛选;当客户名称为空,而客户职业不为空,则只根据客户职业进行客户筛选;当客户名称、客户职业都为空,则要求查询出所以电话不为空的客户信息;

<select id="findCustomerByNameOrJobs" parameterType="com.xiucai.po.Customer" resultMap="resultMap">
	select * from t_customer where 1=1
	<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>
	</select>

(4) 在测试类中编写测试方法findCustomerByNameOrJobTest()进行测试

当姓名不为空的时候

代码:

@Test
	public void findCustomerByNameOrJobTest() {
		 SqlSession sqlSession=SqlSessionUtil.getSqlSession();
		
	    Customer customer=new Customer();
		//当姓名不为空的时候
	    customer.setUsername("huang4");;
	    //当客户姓名为空 职业不为空的时候
//		customer.setJobs("cooker");
	
		List<Customer>customers=new ArrayList<Customer>();
	customers=sqlSession.selectList("com.xiucai.Mapper.CustomerMapper.findCustomerByNameOrJobs", customer);
		for (Customer customer2 : customers) {
			System.out.println(customer2);
		}
	}

结果:

image-20210429100510534

当姓名为空

代码:

@Test
	public void findCustomerByNameOrJobTest() {
		 SqlSession sqlSession=SqlSessionUtil.getSqlSession();
		
	    Customer customer=new Customer();
		//当客户姓名为空 职业不为空的时候
		customer.setJobs("cooker");
	
		List<Customer>customers=new ArrayList<Customer>();
	customers=sqlSession.selectList("com.xiucai.Mapper.CustomerMapper.findCustomerByNameOrJobs", customer);
		for (Customer customer2 : customers) {
			System.out.println(customer2);
		}
	}

结果:

image-20210429100240717

姓名优先级大

image-20210429100755198

都为空时

image-20210429100910102

(5) 使用、对(3)代码块进行修改,要求程序功能不变

对于where的使用

image-20210429114750458

使用where

<!-- 使用where跟trim对代码进行修改 -->
	<select id="findCustomerByNameOrJobs2"
		parameterType="com.xiucai.po.Customer" resultMap="resultMap">
		select * from t_customer
		<where>
			<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>

	</select>

image-20210429114716327

使用trim

<!-- 使用where跟trim对代码进行修改 -->
	<select id="findCustomerByNameOrJobs2"
		parameterType="com.xiucai.po.Customer" resultMap="resultMap">
		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>

(6) 使用和元素对username和jobs进行更新判断,并动态组装SQL。只传入想要更新的字段,编写测试类完成将“jasmine”的电话改为“18966547895”

	<update id="updateCustomerByNameOrJobs" parameterType="com.xiucai.po.Customer" >
	update t_customer
	<set>
	
		<if test="jobs!=null and jobs!='' ">
			 jobs=#{jobs},
		</if>
		<!-- <if test="username!=null and username='' "> </if>-->
			<if test="username !=null and username !=''"></if>
			 username=#{username},
		
		
		<if test="phone!=null and phone !=''">
			 phone=#{phone},
		</if>
	
	</set>
	where id=#{id}
	</update>

image-20210429233218215

(7) 使用动态SQL的元素完成对结果集的遍历,编写测试类完成查找id是(1,2,3)中的信息。

<sql id="customerColumns">id,username,jobs,phone</sql>
	<!-- 给定条件查大量数据集合 -->
	<select id="findListByForeach" parameterType="List"
		resultType="com.xiucai.po.Customer">
		select * from t_customer where id in
		<foreach item="id" index="index" collection="list" open="("
			separator="," close=")">
		#{id}
		</foreach>
	</select>

image-20210429234913425

image-20210429234614845

(8) 使用元素完成模糊查询SLQ语句的改进。

<select id="findCustomerByBind" parameterType="com.xiucai.po.Customer"
		resultType="com.xiucai.po.Customer">
		<bind name="usernameLike" value="'%'+username+'%'"/>
		select * from t_customer where 
		username like #{usernameLike} 
		
	</select>

测试:

@Test
	public void  findByBind() {
		 SqlSession sqlSession=SqlSessionUtil.getSqlSession();	
		Customer customer=new Customer();
		customer.setUsername("sername");
		List<Customer>customers=new ArrayList<Customer>();
		customers=sqlSession.selectList("com.xiucai.Mapper.CustomerMapper.findCustomerByBind", customer);
for (Customer customer1 : customers) {
	System.out.println(customer1);
}
	}

image-20210429235807154

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秀才大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值