mybatis动态标签——trim
<?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.xxx.dynamicSQL.mapper.EmpMapper"> <!-- trim标签:prefix、suffix在标签中内容前面或后面添加指定内容; prefixOverrides,suffixOverrides在标签中内容前面或后面去掉指定内容; --> <select id="getEmp" resultType="emp"> select * from t_emp <trim prefix="where" suffixOverrides="and"> <if test="empName != null and empName != ''"> emp_name = #{empName} and </if> <if test="age != null and age != ''"> age = #{age} and </if> <if test="gender != null and gender != ''"> gender = #{gender} </if> </trim> </select> </mapper>
测试代码
@Test public void test1(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,"李四",11,""); List<Emp> list = mapper.getEmp(emp); list.forEach(System.out::println); }