ibatis

ibatis:
ibatis中dao中的方法在使用sqlMapClient时只能传递一个参数,若是多个要转换,最好用Map param=new HashMap();
在sqlMapClient调用查询方法时,一般返回结果为单行的要使用queryForObject,
        返回结果为多行时使用queryForList

1、若参数为基本类型,怎##中可任意命名
<delete id="deleteDept" parameterClass="int">
delete from dept where deptno=#aaa#;
</delete>
2、若返回值是带有类的list等,则返回值类型还要配置为类
<select id="findDepts" resultClass="com.puckasoft.po.Dept">
select * from dept
</select>
3、若需要用模糊查询时则#要换成'$
<select id="findEmpsByEname" parameterClass="java.lang.String"
resultClass="com.puckasoft.po.Emp">
select * from emp where ename like '$abc$'
</select>
4、带多个参数:
public List<Emp> findEmps(Date beginDate, Date endDate, Float hisal,
Float losal) {
Map params=new HashMap();
params.put("beginDate", beginDate);
params.put("endDate", endDate);
params.put("hisal", hisal);
params.put("losal", losal);

try {
return sqlMapClient.queryForList("findEmps", params);
} catch (SQLException e) {
throw new DaoException();
}
}

<select id="findEmps" parameterClass="hashMap" resultClass="com.puckasoft.po.Emp">
select * from emp where hiredate between #beginDate# and #endDate# and sal between #losal# and #hisal#
</select>
5、ibatis会自动识别数据库,根据不同的数据库进行不同的分页,因此不需要自己写,对于分页只需select * from **即可
public List<Emp> findEmpsByPage(int firstResult, int maxResults) {

try {
return sqlMapClient.queryForList("findEmpsByPage", firstResult, maxResults);
} catch (SQLException e) {
throw new DaoException();
}
}

<select id="findEmpsByPage" resultClass="com.puckasoft.po.Emp">
select * from emp
</select>
6、返回结果为map类型的,配置时也要写为hashMap:
public List<Map> findHisalByDept() {

try {
return sqlMapClient.queryForList("findHisalByDept");
} catch (SQLException e) {
throw new DaoException();
}
}

<select id="findHisalByDept" resultClass="hashMap">
select deptno,max(sal) from emp group by deptno order by max(sal) desc
</select>

ibatis+spring:

1.拷贝工程
2.加入 sping db pool的jar
3.加入applicationContext.xml
4.删除sqlMapConfig.xml 的tx标签
5.编写applicationContext.xml
6.去掉单例,去掉sqlMapClient的取值,加入setter,getter
7.测试

你可能感兴趣的:(spring,ibatis)