mybatis动态sql

本文详细介绍MyBatis框架中动态SQL的运用,包括if条件判断、where组合查询及foreach循环,通过具体案例演示如何根据参数灵活拼接SQL语句,实现高效的数据查询与更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis框架可以对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

如我们进行多条件查询的时候,查询条件是动态变化的,这时候我们就可以使用动态SQL对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

 

if条件查询:

案例:

员工表实体类:

SQL语句:

<!-- 修改数据 当设置修改为空的时候 不覆盖原有的值 -->
	<update id="updateEmp" parameterType="Emp">
		update emp 
		<set>
			<if test="ename!=null and ename!=''">
				ename=#{ename},
			</if>
			<if test="job!=null and job!=''">
				job=#{job},
			</if>
			<if test="hiredate!=null">
				hiredate=#{hiredate},
			</if>
			<if test="sal!=null and sal>0.0">
				sal=#{sal},
			</if>
		</set>
		where empno=#{empno}
	</update>

测试:

          
		InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		EmpMapper empMapper = session.getMapper(com.neusoft.mapper.EmpMapper.class);
		
		Emp emp = new Emp(8859,"钢铁侠","程序猿", new Date(),20000);
		empMapper.updateEmp(emp);
		session.commit();
		session.close();

 

更新前:

更新后:


 where组合查询:

SQL语句: 

<select id="selectEmpCondition" parameterType="java.util.Map" resultType="emp">
		select * from emp
		<where>
			<if test="ename!=null and ename!=''">
				and ename like '%${ename}%'
			</if>
			<if test="job!=null and job!=''">
				and job like '%${job}%'
			</if>
			<if test="hiredate!=null">
				and hiredate <![CDATA[<=]]>#{hiredate}
			</if>
			<if test="sal!=null and sal>0.0">
				and sal=#{sal}
			</if>
		</where>
	</select>

 

测试: 


		InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		EmpMapper empMapper = session.getMapper(com.neusoft.mapper.EmpMapper.class);
		
		
		Map<String,Object> map= new HashMap();
		map.put("ename", "钢");
		map.put("job","程序猿");
		List<Emp> emp = empMapper.selectEmpCondition(map);
		for (Emp emp2 : emp) {
			System.out.println(emp2.toString());
		}

查询结果:

 

 

 


foreach:

List类型

SQL语句:

    <select id="selectList" parameterType="java.util.List" resultType="emp"> 	
		select * from emp
		where empno in
		<foreach collection="list" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
	</select>

测试:

        List<Integer> empnos = new ArrayList<>();
		empnos.add(8859);
		empnos.add(7788);
		List<Emp> emp = empMapper.selectList(empnos);
		for (Emp emp2 : emp) {
			System.out.println(emp2.toString());
		}

 查询结果:

 

Array数组类型

SQL语句

    <select id="selectArray" resultType="emp"> 	
    	select * from emp
		where empno in
		<foreach collection="array" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
	</select>

 

测试:

        Integer[] arr = new Integer[] {8859,7788,7876};
		List<Emp> emp = empMapper.selectArray(arr);
		for (Emp emp2 : emp) {
			System.out.println(emp2.toString());
		}

 

查询结果:

 

Map类型

 

SQL语句:

<select id="selectMap" parameterType="java.util.Map" resultType="emp"> 	
		select * from emp
		where empno in
		<foreach collection="empnos" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
		and sal <![CDATA[>]]>#{sal}
	</select>

 

测试:

        Integer[] arr = new Integer[] {8859,7788,7934,7876};
		Map<String,Object> map= new HashMap();
		map.put("sal", 2000);
		map.put("empnos", arr);
		List<Emp> emp = empMapper.selectMap(map);
		for (Emp emp2 : emp) {
			System.out.println(emp2.toString());
		}

查询结果:查询的四个empno中只有两个工资大于两千

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值