mybatis-210721-04---动态sql-set与if结合的动态更新

mybatis-210721-04—动态sql-set与if结合的动态更新


<!--
		更新员工
			要求:
			携带了那个字段,更新哪个字段
-->

EmployeeMapper.java

package com.bgy.mybatis.dao;

import com.bgy.mybatis.bean.Employee;

public interface EmployeeMapper {
	
	public void updateEmp(Employee employee);
}

EmployeeMapper.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.bgy.mybatis.dao.EmployeeMapper">
	
	<update id="updateEmp">
		update 
			tbl_employee
		
		<!-- 使用trim标签 -->
		<trim prefix="set" suffixOverrides=",">
			<if test="lastName!=null and lastName!=&quot;&quot;">
				last_name=#{lastName},
			</if>
			<if test="email!=null and email.trim()!=&quot;&quot;">
				email=#{email},
			</if>
			<if test="gender!=null and gender.trim()!=&quot;&quot;">
				gender=#{gender}		
			</if>
		</trim>
		
		<!--  使用set标签
		<set>
			<if test="lastName!=null and lastName!=&quot;&quot;">
				last_name=#{lastName},
			</if>
			<if test="email!=null and email.trim()!=&quot;&quot;">
				email=#{email},
			</if>
			<if test="gender!=null and gender.trim()!=&quot;&quot;">
				gender=#{gender}		
			</if>
		</set>
		-->
		
		<!--  不使用set标签  
		set
			<if test="lastName!=null and lastName!=&quot;&quot;">
				last_name=#{lastName}
			</if>
			<if test="email!=null and email.trim()!=&quot;&quot;">
				email=#{email}		
			</if>
			<if test="gender!=null and gender.trim()!=&quot;&quot;">
				gender=#{gender}		
			</if>
		-->
		<where>
			id=#{id}
		</where>
	</update>
</mapper>

MybatisTest.java

package com.bgy.mybatis.test;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import com.bgy.mybatis.bean.Employee;
import com.bgy.mybatis.dao.EmployeeMapper;

class MybatisTest {
	
	public SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	
	/**·
	 * 测试sql——set与if结合的动态更新
	 * @throws IOException
	 */
	@Test
	public void test() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		try {
			EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
			
			Employee employee = new Employee(1,"zhangsan",null,null);
			mapper.updateEmp(employee);
			
			sqlSession.commit();
		} finally {
			sqlSession.close();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值