MyBatis动态sql_bind绑定

<select id="getEmpsTestInnerParameter" resultType="com.cn.zhu.bean.Employee">
	     <!-- bind 可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
	      <bind name="_lastName" value="'%'+lastName+'%'"/>
	       select *  from tbl_employee
	         <if test="_parameter!=null">
	           where last_name like #{_lastName}
	         </if>
	 </select>



测试方法

@Test
	public void testInnerParam() throws Exception{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
	    SqlSession openSession = sqlSessionFactory.openSession();
	    try{
	    	EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
	    	Employee employee2=new Employee();
	    	employee2.setLastName("h");	    	 
	    	List<Employee> list = mapper.getEmpsTestInnerParameter( employee2);
	    	for(Employee employee: list){
	    		System.out.println(employee);
	    	}
	    }finally{
	    	openSession.close();
	    }
	}


这种方法不够灵活,还是用另一种方法

<!-- 	public List<Employee>  getEmpsTestInnerParameter(Employee employee); -->
	 <select id="getEmpsTestInnerParameter" resultType="com.cn.zhu.bean.Employee">
	     <!-- bind 可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
	      <bind name="_lastName" value="'%'+lastName+'%'"/>
	       select *  from tbl_employee
	         <if test="_parameter!=null">
	           where last_name like #{lastName}
	         </if>
	 </select>



@Test
	public void testInnerParam() throws Exception{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
	    SqlSession openSession = sqlSessionFactory.openSession();
	    try{
	    	EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
	    	Employee employee2=new Employee();
	    	employee2.setLastName("%h%");	    	 
	    	List<Employee> list = mapper.getEmpsTestInnerParameter( employee2);
	    	for(Employee employee: list){
	    		System.out.println(employee);
	    	}
	    }finally{
	    	openSession.close();
	    }
	}

测试结果

MyBatis动态sql_bind绑定_sql