mybaits十九:bind绑定

<select id="getEmpsByInnerParam" resultType="com.atChina.bean.Employee">
     	<!-- bind标签可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
     	<bind name="_empName" value="'%'+ename+'%'"/>  <!-- value属性中可以写OGNL表达式 -->
     	select * from emptest
     	<if test="_parameter != null">
     		where ename like #{_empName}
     	</if>
     </select>

如果mybatis版本比较低,可能会遇到org.xml.sax.SAXParseException: Element type "bind" must be declared的错误. 因为低版本的jar包中是没有定义bind标签的

@Test
	public void test34() throws IOException, ParseException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession();
		try{
				
			// 命名空间.id,这样别的配置文件里有同名的id,程序也不报错
			EmployeeMapperDynamicSql eds = openSession.getMapper(EmployeeMapperDynamicSql.class);
			Employee e1 = new Employee();
			e1.setEname("t"); // 这样就直接传入 t就行了,, 但还是推荐使用'%t%'这种方式
			List<Employee> ees = eds.getEmpsByInnerParam(e1);
			for(Employee e : ees){
				System.out.println(e);
			}
		
			openSession.commit();
			
		}finally{
			// 关闭
			openSession.close();
		}
	}

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/88984378
今日推荐