select 类型 Mapped Statement 的本质并无改变,只是通过 dynamic 元素定义了动态 where 子句。dynamic 元素属性 prepend=”where”在一元判定元素 isNotNull 返回”true”时有效。isNotNull 元素属性 property="id" 用于判定 People.id 是否为 null,假如未传入包含主键值的 People 实例,dynamic 元素将会无效,反之则亦然,这样就达到了在不改变映射文件的前提下实现动态 SQL 的目的。关于一元判定元素和二元判定元素的详细资料请查阅官方文档,如能恰当利用这些判定元素,动态 SQL 将会更灵活。
相应程序代码:
package test; import java.io.Reader;import java.util.List; import com.ibatis.sqlmap.client.*;import com.ibatis.common.resources.*; import bo.*; public class AutoMag { private Reader reader; private People people=new People(); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public List findPeople() throws Exception{ reader = Resources.getResourceAsReader(resource); sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader); List list=sqlMap.queryForList("getPeople",null); return list; }}
package test;
import java.io.Reader;import java.util.List;
import com.ibatis.sqlmap.client.*;import com.ibatis.common.resources.*;
import bo.*;
public class AutoMag {
private Reader reader; private People people=new People(); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public List findPeople() throws Exception{ reader = Resources.getResourceAsReader(resource); sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader); List list=sqlMap.queryForList("getPeople",null); return list; }}
传入 Mapped Statement id,再传入未包含主键的对象实例,也可以干脆传 null 进去。重载方法 sqlMap.queryForList(String arg0, Object arg1, int arg2, int arg3) 用于分页,arg2、arg3分别代表页码和每页个数。
int arg2, int arg3
好了,使用动态 Mapped Statement,再试试
上一页 [1] [2] [3] [4] [5] 下一页
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18