mybatis使用trim自定义动态sql字符串截取

 时间:2024-10-14 09:10:51

1、新建一个员工的mapper。package com.gwolf.dao;import java.util.Map;import org.apache.ibatis.annotations.MapKey;import com.gwolf.bean.Employee;public interface EmployeeMapper { public 返回的对象 getObjectById(Integer id); }

mybatis使用trim自定义动态sql字符串截取

3、上述动态sql假如我们只要员工id的话sql是有错误的。接下来我们将动态sql放在trim标签中,trim标签能够去掉后面多出的and或者or。<select id="getObjectById" resultMap="MyEmpByStep" databaseId="mysql"> select * from tbl_emp <trim prefix="" prefixOverrides="" suffix=""> <if test="empId!=null and empId!=''"> emp_id = #{empId} and </if> <if test="empName!=null and empName!=''"> emp_name = #{empName} </if> </trim> </select>

mybatis使用trim自定义动态sql字符串截取

5、<trim prefix="" prefixOverrides="" suffix="">中prefixOverrides表示去掉这个字符串前面多余的字符串。我们这些不需要去除。可以不要写这个属性。<select id="getObjectById" resultMap="MyEmpByStep" databaseId="mysql"> select * from tbl_emp <trim prefix="where" suffix=""> <if test="empId!=null and empId!=''"> emp_id = #{empId} and </if> <if test="empName!=null and empName!=''"> emp_name = #{empName} </if> </trim> </select>

mybatis使用trim自定义动态sql字符串截取
  • jquery怎么将获取的对象变成数组
  • JS中如何删除选中复选框的行
  • DBeaver如何修改连接类型
  • Myeclipse如何呈现idea中的多屏效果
  • iframe 获取父页面方法
  • 热门搜索
    正定旅游景点大全 邵家楼旅游攻略 旅游网排行榜 苏州杭州旅游攻略 澳大利亚旅游地图 惠州奥地利小镇旅游 凤凰旅游路线 木兰围场旅游攻略 旅游作文400字 杭州到云南旅游