MyBatis的动态sql_set与if结合的动态更新
来源:互联网 发布:js获取自定义属性data 编辑:程序博客网 时间:2024/06/05 23:42
MyBatis的动态sql_set与if结合的动态更新
EmployeeMapperDynamicSQL.java
package com.cn.mybatis.dao;import java.util.List;import com.cn.zhu.bean.Employee;public interface EmployeeMapperDynamicSQL {//<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->public List<Employee> getEmpsByConditionIf(Employee employee);//测试trim截取字符串public List<Employee> getEmpsByConditionTrim(Employee employee);// 测试choosepublic List<Employee> getEmpsByConditionChose(Employee employee);public void updateEmp(Employee employee);}EmployeeMapperDynamicSQL.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.cn.mybatis.dao.EmployeeMapperDynamicSQL"> <!-- public void updateEmp(Employee employee); --><update id="updateEmp"><!-- set方法的使用 update tbl_employee <set><if test="lastName!=null">last_name=#{lastName}, </if><if test="email!=null">email=#{email}, </if><if test="gender!=null">gender=#{gender} </if> </set>where id=#{id}</update>--><!-- 更新拼串 --> update tbl_employee <trim prefix="set" suffixOverrides=","><if test="lastName!=null">last_name=#{lastName}, </if><if test="email!=null">email=#{email}, </if><if test="gender!=null">gender=#{gender} </if></trim>where id=#{id}</update></mapper>MyBatisTest.java
@Testpublic void testDynamicSql() throws IOException{SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();SqlSession openSession=sqlSessionFactory.openSession();try {EmployeeMapperDynamicSQL mapper=openSession.getMapper(EmployeeMapperDynamicSQL.class);//测试if where Employee employee=new Employee(1, "Admin11", null, null);Emp(employee);openSession.commit();} catch (Exception e) {// TODO: handle exceptionopenSession.close();e.printStackTrace();}}测试结果
阅读全文
0 0
- MyBatis的动态sql_set与if结合的动态更新
- mybatis的动态SQL(一)if标签的使用
- mybatis if 动态生成SQL的一个注意点
- mybatis if test 相等的情况如何动态拼接sql
- mybatis的动态sql之if test用法
- Mybatis中动态SQL(if,where,foreach 的使用)
- Mybatis的动态sql语句if和choose
- Mybatis的动态sql语句if和choose
- Mybatis的动态sql语句if和choose
- Mybatis中动态SQL(if,where,foreach 的使用)
- java实现的highcharts与ajax结合动态实时获取数据更新图表
- java实现的highcharts与ajax结合动态实时获取数据更新图表
- mybatis的批量插入与动态查询
- mybatis的动态SQL与模糊查询
- mybatis-动态sql-与hibernate的区别
- MyBatis--动态SQL---if标签
- Mybatis动态拼接if语句
- MyBatis的动态SQL
- 遍历List,Set的方法
- 第一节点习题
- fork之写时拷贝技术
- java 动态代理和静态代理浅析
- PatchMatch
- MyBatis的动态sql_set与if结合的动态更新
- 【java学习记】—勤快式、懒汉式单例
- 吐槽。
- MySQL常用操作
- Single Threaded Execution Pattern
- 简单理解操作系统中的PV操作
- JAVA学习笔记(一)类的多态
- 文件的基本操作总结
- HTML第二节点学习笔记