取得方法と結果
sqlmap
<?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>
<!-- SQLでマッピングする記述方法。ifnullはDBMSの関数 -->
<select id="getEmpByEmpno" resultClass="examples.dto.Emp">
SELECT
EMPNO,
ENAME,
JOB,
MGR as manager,
HIREDATE,
SAL as salary,
ifnull(COMM, 0.0) as commission,
DEPTNO
FROM EMP
WHERE EMPNO = #value#
</select>
<select id="allEmp1" resultClass="examples.dto.Emp">
SELECT
EMPNO,
ENAME,
JOB,
MGR as manager,
HIREDATE,
SAL as salary,
ifnull(COMM, 0.0) as commission,
DEPTNO
FROM EMP
</select>
</sqlMap>
ソース
package eo.sim.tool.service;
import java.util.Map;
import com.ibatis.sqlmap.client.SqlMapClient;
import examples.dto.Emp;
import util.MyAppSqlConfig;
public class EmpService {
public String getEmpName(String empno) throws Exception {
SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
int intEmpno = (Integer.valueOf(empno)).intValue();
// dtoに格納
Emp emp = (Emp)sqlMap.queryForObject("getEmpByEmpno",intEmpno);
// MAP<"列",<"列", "データ">>に格納
System.out.println("getEmpByEmpno");
Map<String, Object> map1 = sqlMap.queryForMap("getEmpByEmpno",intEmpno, "deptno");
Map<String, Object> map2 = sqlMap.queryForMap("getEmpByEmpno",intEmpno, "empno");
Map<String, Object> map4 = sqlMap.queryForMap("getEmpByEmpno",intEmpno, "empno","ename");
// MAP<"列",<"列", "データ">>に格納
Map<String, Object> map3 = sqlMap.queryForMap("allEmp1",intEmpno, "empno");
Map<String, Object> map5 = sqlMap.queryForMap("allEmp1",intEmpno, "empno","ename");
System.out.println(map1.toString());
System.out.println(map2.toString());
System.out.println(map4.toString());
System.out.println("");
System.out.println("allEmp1");
System.out.println(map3.toString());
System.out.println(map5.toString());
return emp.getEname();
}
}
テーブル
CREATE TABLE `EMP` (
`EMPNO` decimal(4,0) NOT NULL,
`ENAME` varchar(10) default NULL,
`JOB` varchar(9) default NULL,
`MGR` decimal(4,0) default NULL,
`HIREDATE` date default NULL,
`SAL` decimal(7,2) default NULL,
`COMM` decimal(7,2) default NULL,
`DEPTNO` decimal(2,0) NOT NULL,
PRIMARY KEY (`EMPNO`)
)
データ
“EMPNO”,”ENAME”,”JOB”,”MGR”,”HIREDATE”,”SAL”,”COMM”,”DEPTNO”
7369,”篠田”,”一般事務”,7902,1990/12/17,800,,20
7499,”山村”,”営業員”,7698,1991/02/20,1600,300,30
結果
getEmpByEmpno
{20=[empno=7369, ename=篠田, job=一般事務, manager=7902, hiredate=1990-12-17, salary=800.0, commistion=0.0, depno=20]}
{7369=[empno=7369, ename=篠田, job=一般事務, manager=7902, hiredate=1990-12-17, salary=800.0, commistion=0.0, depno=20]}
{7369=篠田}
allEmp1
{7369=[empno=7369, ename=篠田, job=一般事務, manager=7902, hiredate=1990-12-17, salary=800.0, commistion=0.0, depno=20], 7499=[empno=7499, ename=山村, job=営業員, manager=7698, hiredate=1991-02-20, salary=1600.0, commistion=300.0, depno=30]}
{7369=篠田, 7499=山村}