Ibatis Iterator用法小结
1、要在ibatis里面使用命名空间需要在sqlMapConfig.xml中增加如下设置
???????? <settings useStatementNamespaces="true" />
2、写JavaBean类时? 建议把属性全部写成引用类型? 对于基本类型写成它对应的包装类型
????? 这样可以避免数据库中对应的字段可以为空时?? 查询结果在映射时报错
3、 <select id="getSomeEmpByList" parameterresultMap="empResultMap"> select e.empno, e.ename, e.job, e.mgr, e.hiredate, e.sal, e.comm, e.deptno from emp e <dynamic prepend="where"> <isNotEmpty property="empNameList"> e.ename in <iterate property="empNameList" open="(" close=")" conjunction=","> #empNameList[]# </iterate> </isNotEmpty> </dynamic> </select>
?4、
<select id="getSomeEmpByListParam" parameterresultMap="empResultMap"> select e.empno, e.ename, e.job, e.mgr, e.hiredate, e.sal, e.comm, e.deptno from emp e <dynamic prepend="where"> <isNotEmpty> e.ename in <iterate open="(" close=")" conjunction=","> #empNameList[]# </iterate> </isNotEmpty> </dynamic> </select>
?
?
?
配置4?? parameterClass为java.util.ArrayList/List? 这时在遍历时? 不要property属性,否则报错。String index out of range: -1
?
具体可以参考http://hongzhguan.iteye.com/blog/1222353这篇博客? 写的很详细了!
5、批量插入时? 配置文件的写法:
<insert id="insertBatchEmp" parameterproperty="empVoList"> into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values ( #empVoList[].empno#, #empVoList[].ename#, #empVoList[].job#, #empVoList[].mgr#, sysdate, #empVoList[].sal#, #empVoList[].comm#, #empVoList[].deptno# ) </iterate> select * from dual </insert>
同样empVoList也是empVO类的一个属性? 必须写;#empVoList[].empno#, 说明:前面的empVoList代表的是VO中的属性 且这个属性必须是List OR是Iterabale类型的;
这个List里面放的还是empVO对象? 后面的empno为empVO对象的一个属性? 后面依次类推
如果传入的参数是List/Arraylist时? []前面的内容是不需要定义的? 也就是说跟前面那个名没关系? 但是[]必须要写? 以让Ibatis知道你传入的是一个集合? 下面的写法可以证明这一点
6、 <insert id="insertBatchEmp1" parameter> into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values ( #a[].empno#, #b[].ename#, #c[].job#, #d[].mgr#, sysdate, #e[].sal#, #f[].comm#, #g[].deptno# ) </iterate> select * from dual </insert>
?
?
?
?
?? 上面那种写法传入的参数也是个List?<Map>? List里面放的是一个Map? #a[].empno#, 后面的empno就是map的key值? 后面依次类推 前面的a,b,c,....不影响? 可以证明第五点的说法
?
7、VO类代码:
public class EmpVO implements Serializable{private int empno;private String ename;private String job;private Integer mgr;private Date hiredate;private Float sal;private Float comm;private int deptno;private String[] empNames;private List<String> empNameList;private List<EmpVO> empVoList;}
?
?8、调用类代码:??
public class CRUDTest {/** * @param args */public static void main(String[] args) throws Exception{CRUDTest test = new CRUDTest();List<EmpVO> empList = test.getEmpList();for(Iterator<EmpVO> ite = empList.iterator(); ite.hasNext();) {System.err.println(ite.next());}/* 通过员工姓名来查询*/EmpVO empVO = new EmpVO();empVO.setEmpNames(new String[]{"SMITH", "ALLEN", "WARD", "JONES"});List<EmpVO> empList1 = test.getEmpList(empVO);for(Iterator<EmpVO> ite = empList1.iterator(); ite.hasNext();) {System.err.println(ite.next());}List<String> empNameList = new ArrayList<String>();empNameList.add("SMITH");empNameList.add("ALLEN");empNameList.add("WARD");empNameList.add("JONES");empVO.setEmpNameList(empNameList);List<EmpVO> empList2 = test.getEmpListByListParam(empVO);for(Iterator<EmpVO> ite = empList2.iterator(); ite.hasNext();) {System.err.println(ite.next());}List<EmpVO> empList3 = test.getEmpListByListParam(empNameList);for(Iterator<EmpVO> ite = empList3.iterator(); ite.hasNext();) {System.err.println(ite.next());}List<EmpVO> empList4 = new ArrayList<EmpVO>();for(int i = 0; i < 4; i++) {EmpVO empVo = new EmpVO();empVo.setComm(Float.valueOf(20 + i));empVo.setDeptno(20);empVo.setEmpno(7935 + i);empVo.setEname("Andy-yong" + i);empVo.setJob("MANAGER" + i);empVo.setMgr(7902);empVo.setSal(Float.valueOf(1003 + i));Map empVoMap = new HashMap<String, Object>();empVoMap.put("empno", 7935 + i);empVoMap.put("ename", "Andy-yong" + i);empVoMap.put("job", "MANAGER" + i);empVoMap.put("mgr", 7902);empVoMap.put("sal", Float.valueOf(1003 + i));empVoMap.put("comm", Float.valueOf(20 + i));empVoMap.put("deptno", 30);empList4.add(empVo);}empVO.setEmpVoList(empList4);//int rowNums = Integer.valueOf((test.insertBatchEmpByListParam(empList4)).toString());System.err.println(test.insertBatchEmpByListParam(empVO) + "**************");}/** select without parameters * @throws SQLException */public List<EmpVO> getEmpList() throws SQLException{return DaoUtil.getInstance().queryForList("emp.getSomeEmp");}/** * select employees by enames 数组属性 * @return empVO list * @throws SQLException */public List<EmpVO> getEmpList(EmpVO empVO) throws SQLException{return DaoUtil.getInstance().queryForList("emp.getSomeEmpByName", empVO);}/** parameter is a listProperty of the vo object 集合属性*/public List<EmpVO> getEmpListByListParam(EmpVO empVO) throws SQLException {return DaoUtil.getInstance().queryForList("emp.getSomeEmpByList", empVO);}/** parameter is a list 传入参数是个List*/public List<EmpVO> getEmpListByListParam(List<String> empNameList) throws SQLException {return DaoUtil.getInstance().queryForList("emp.getSomeEmpByListParam", empNameList);}public Object insertBatchEmpByListParam(EmpVO empVOList) throws SQLException {return DaoUtil.getInstance().insert("emp.insertBatchEmp", empVOList);}}
?
?
?9、参考博客:? http://hongzhguan.iteye.com/blog/1222353;?http://hi.baidu.com/xiami9910/blog/item/8caacc1f45fe9ccda686697b.html;
http://www.cnblogs.com/eugenewu0808/archive/2011/03/18/1987668.html;http://www.javadn.com/read.php?tid-790.html
?
??
?
?
?
?