自定义结果集结构,可用resultMap:
<resultMap type="hashmap" id="mpGoods">
<result property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result property="nMount" column="nMount" javaType="java.lang.Double" jdbcType="DOUBLE"/>
<result property="price" column="price" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result property="name" column="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result property="auName" column="auName" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result property="manufacture" column="manufacture" javaType="java.lang.String" jdbcType="VARCHAR"/>
</resultMap>
<parameterMap type="hashmap" id="ParaMap_test">
<parameter property="result"
mode="OUT"
javaType="java.sql.ResultSet"
jdbcType="OTHER"/>
</parameterMap>
<!-- 调用存储过程 -->
<select id="get_sp_Goods"
statementType="CALLABLE"
parameterMap="ParaMap_test"
resultMap="mpGoods"
>
{#{result} = call sp_getGoods()}
</select>
我的理解是resultMap定义的是数据结构,也就是说结果集的字段名,至于前台要返回的是单记录还是多记录 ,要看调用方法的定义了,如我要返回多记录:
public interface UserMapper {
List get_sp_Goods(Map returnMap);
}
调用方法:
public void callProcedure(){
SqlSession session=sqlSessionFactory.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
Map retuParam = new HashMap();
ResultSet rs=null;
retuParam.put("result", rs);
//这里生成List了,list时就是一个hashMap对象,可进行强转,里面的key就是我们在XML中定义id="mpGoods"的结构了。
List list= userMapper.get_sp_Goods(retuParam);
System.out.println(retuParam.get("result"));
System.out.println(list.size());
for (Object object : list) {
Map map=(Map) object;
System.out.println(map);
}
}
myBatis很灵活,对SQL比较熟练的人来说,很好用,自已定义sql语句强大灵活,性能也可以根据自已需要改写sql语句.