基于上一篇
[url]http://jiaozhiguang-126-com.iteye.com/blog/1668709[/url]
为了简化我们SQL输入参数的配置,我们写一个类来专门负责处理输入参数,把它们封装成一个Map类型,代码如下:
主要修改Dept.xml 文件 和 IBatisDemo类
[url]http://jiaozhiguang-126-com.iteye.com/blog/1668709[/url]
为了简化我们SQL输入参数的配置,我们写一个类来专门负责处理输入参数,把它们封装成一个Map类型,代码如下:
package ibatis.util;
import java.util.HashMap;
public class ParameterMap extends HashMap<Object, Object> {
public ParameterMap(Object... parameters) {
for(int i=0; i<parameters.length-1; i=i+2) {
super.put(parameters[i], parameters[i+1]);
}
}
}
主要修改Dept.xml 文件 和 IBatisDemo类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Dept" type="ibatis.model.Dept" />
<!--
<select id="getAllDepts" resultClass="Dept">
select *
from dept
</select>
-->
<typeAlias alias="parameterMap" type="ibatis.util.ParameterMap" />
<select id="getAllDepts" resultClass="java.util.HashMap">
select *
from dept
</select>
<select id="getDeptByName" parameterClass="java.lang.String"
resultClass="java.util.HashMap">
select *
from dept
where DNAME=#VARCHAR#
</select>
<select id="getDeptByDeptNameAndLoc" parameterClass="parameterMap"
resultClass="java.util.HashMap">
select *
from dept
where DNAME=#dName:VARCHAR# and LOC=#loc:VARCHAR#
</select>
<insert id="addDept" parameterClass="parameterMap">
insert into dept(DEPTNO,DNAME,LOC)
values(#deptNo:NUMBER#,#dName:VARCHAR#,#loc:VARCHAR#)
</insert>
<update id="updateDept" parameterClass="parameterMap">
update dept
set LOC=#loc:VARCHAR# where DEPTNO=#deptNo:NUMBER#
</update>
<delete id="deleteDept" parameterClass="java.lang.Integer">
delete from dept
where deptNo=#INT#
</delete>
</sqlMap>
package ibatis;
import ibatis.util.ParameterMap;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class IBatisDemo {
private static String config = "ibatis/SqlMapConfig.xml";
private static Reader reader ;
private static SqlMapClient sqlMap;
static {
try {
reader = Resources.getResourceAsReader(config);
} catch (IOException e) {
e.printStackTrace();
}
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
}
public static void main(String[] args) throws IOException, SQLException {
List<HashMap> list = select();
for(HashMap dept : list) {
System.out.println(dept);
}
System.out.println("------------------------------------");
System.out.println(select("Sec"));
System.out.println("------------------------------------");
ParameterMap parameterMap = new ParameterMap("dName", "Adv","loc", "Beijing");
System.out.println(select(parameterMap));
System.out.println("------------------------------------");
System.out.println(delete(91));
System.out.println("------------------------------------");
parameterMap = new ParameterMap("deptNo", "91","dName", "财务部", "loc", "哈尔滨");
System.out.println(insert(parameterMap));
System.out.println("------------------------------------");
parameterMap = new ParameterMap("deptNo", "91", "loc", "haerbin");
System.out.println(update(parameterMap));
}
public static int delete(int deptNo) throws SQLException {
return sqlMap.delete("deleteDept", deptNo);
}
public static int update(Map parameterMap) throws SQLException {
return sqlMap.update("updateDept", parameterMap);
}
public static int insert(Map parameterMap) throws SQLException {
return sqlMap.update("addDept", parameterMap);
}
public static HashMap select(Map parameterMap) throws SQLException {
return (HashMap) sqlMap.queryForObject("getDeptByDeptNameAndLoc",
parameterMap);
}
public static HashMap select(String parameter) throws SQLException {
return (HashMap) sqlMap.queryForObject("getDeptByName", parameter);
}
public static List<HashMap> select() throws SQLException {
return sqlMap.queryForList("getAllDepts");
}
}