mybaits十九:bind绑定
生活随笔
收集整理的這篇文章主要介紹了
mybaits十九:bind绑定
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<select id="getEmpsByInnerParam" resultType="com.atChina.bean.Employee"><!-- bind標(biāo)簽可以將OGNL表達(dá)式的值綁定到一個(gè)變量中,方便后來(lái)引用這個(gè)變量的值 --><bind name="_empName" value="'%'+ename+'%'"/> <!-- value屬性中可以寫OGNL表達(dá)式 -->select * from emptest<if test="_parameter != null">where ename like #{_empName}</if></select>
如果mybatis版本比較低,可能會(huì)遇到org.xml.sax.SAXParseException: Element type "bind" must be declared的錯(cuò)誤. 因?yàn)榈桶姹镜膉ar包中是沒(méi)有定義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,程序也不報(bào)錯(cuò)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绑定的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mybaits十八:内置标签
- 下一篇: mybaits二十一:2缓存介绍