mybaits十九:bind绑定
生活随笔
收集整理的這篇文章主要介紹了
mybaits十九:bind绑定
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<select id="getEmpsByInnerParam" resultType="com.atChina.bean.Employee"><!-- bind標(biāo)簽可以將OGNL表達(dá)式的值綁定到一個變量中,方便后來引用這個變量的值 --><bind name="_empName" value="'%'+ename+'%'"/> <!-- value屬性中可以寫OGNL表達(dá)式 -->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標(biāo)簽的
?
@Testpublic 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{// 關(guān)閉openSession.close();}}?
總結(jié)
以上是生活随笔為你收集整理的mybaits十九:bind绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits十八:内置标签
- 下一篇: mybaits二十一:2缓存介绍