1. Mybatis 调用存储过程, 十分的简便
Interface: public Integer saveEmp(TbEmployee emp); 插入 一个 人, 把其主键返回
<!-- 存储过程 -->
<select id="saveEmp"
statementType="CALLABLE" 存储过程
useCache="false" 不使用 缓存
parameterType="com.xt.entity.TbEmployee"
resultType="int"> 返回类型
<![CDATA[
{call UP_SE(#{empName},#{empSex},#{empBirth},#{empSalary},
# { empId,mode=OUT,jdbcType=INTEGER } 存入这个 mode 的 empId
)
}
]]>
</select>
Mysql 语句就是:
create PROCEDURE UP_SE
(
in _empName varchar(20),
in _empSex int,
in _empBirth date,
in _empSalary double,
out _key int
)
BEGIN
insert into tbemployee values(default,_empName,_empSex,_empBirth,
_empSalary,2);
commit; 必须提交
select max(emp_id) into _key from tbemployee;
end;
2. 当传入的 参数 是 Map 集合 时,
Interface: public void findEmpCntByDept(Map<String, Object> params);
<select id="findEmpCntByDept"
statementType="CALLABLE"
useCache="false"
parameterType="java.util.HashMap">
<![CDATA[ 这是 Map 的key名 再 得到 的 对应 value,
{call UP_findebydcnt(#{deptName},#{empCnt,mode=OUT,jdbcType=INTEGER})}
]]>
</select>
Mysql :create PROCEDURE UP_findebydcnt
(
in _deptName varchar(20),
out _empCnt int
)
BEGIN
declare _deptId int;
select dept_id into _deptId from tbdept where dept_name = _deptName;
select count(*) into _empCnt from tbemployee where dept_id = _deptId;
end;
主要 是 调用时:
SqlSession s = sf.openSession();
IEmployeeMapper empDao = s.getMapper(IEmployeeMapper.class);
Map<String, Object> ps = new HashMap<String, Object>();
ps.put("deptName", "飞到");
ps.put("empCnt", null);
empDao.findEmpCntByDept(ps);
System.out.println(ps.get("empCnt"));
3. 当传入 Map 返回 List
Interface: public List<TbEmployee> findEByTID(Map<String, Object> params);
<select id="findEByTID"
statementType="CALLABLE"
useCache="false"
resultMap="result_Emp_map" 作为 把 数据库的列名和 java Bean 对应起来
parameterType="java.util.HashMap">
<![CDATA[
{call UP_findEmpByDeptId(#{deptId})}
]]>
</select>
<cache eviction="FIFO"
flushInterval="10000"
readOnly="true"
size="1024"></cache>
<resultMap type="com.xt.entity.TbEmployee" id="result_Emp_map">
<id column="emp_id" property="empId" />
<result column="emp_name" property="empName" />
<result column="emp_sex" property="empSex" />
<result column="emp_birth" property="empBirth" />
<result column="emp_salary" property="empSalary" />
</resultMap>
调用 IEmployeeMapper empDao = s.getMapper(IEmployeeMapper.class);
Map<String, Object> ps = new HashMap<String, Object>();
ps.put("deptId", 2);
List<TbEmployee> list = empDao.findEByTID(ps);
mysql:SqlSession s = sf.openSession();
Idept Iemps=s.getMapper(Idept.class);
Map<String,Object> paa=new HashMap<String, Object>();
paa.put("deptid", 2);
List<Map<String,Object>> pas=Iemps.findbys(paa);
for(Map<String,Object> ss:pas){
System.out.println(">>>>"+ss.get("emp_name"));
System.out.println("<<<"+ss.get("dept_name"));
}