returnType是自定义的类或者jdk自带的类
resultMap是在mapperXMl文件中通过resultMap节点定义出来的
例如:
<resultMap id="BaseResultMap" type="com.sinosoft.reins.POJO.model.PrpMaxNo" >
<id column="GROUPNO" property="groupno" jdbcType="VARCHAR" />
<id column="TABLENAME" property="tablename" jdbcType="VARCHAR" />
<id column="MAXNO" property="maxno" jdbcType="VARCHAR" />
<result column="FLAG" property="flag" jdbcType="VARCHAR" />
</resultMap>
1)于是我们就可以在后面的查询节点中使用这个map的id啦
例如:
<select id="genMaxMinNo" resultMap="BaseResultMap">
select GROUPNO,TABLENAME,MAXNO,FLAG from TEST where id=#{id}
</select>
2)当然我们也可以在没有定义resultMap的情况下,直接(保证结果列的key和model的properties一致就可以)
<select id="genMaxMinNo" resultType="com.sinosoft.reins.POJO.model.PrpMaxNo">
select GROUPNO,TABLENAME,MAXNO,FLAG from TEST where id=#{id}
</select>
3)当然没定义resultMap的情况下,也可以使用mybatis的默认机制返回一个map出来
<select id="genMaxMinNo" resultType="map">
select GROUPNO,TABLENAME,MAXNO,FLAG from TEST where id=#{id}
</select>
在使用的时候,可以将返回值强转成HashMap,key默认key是大写的可以通过起别名使得key变成小写(别名加不加引号,as用不用必须写,自己去测验吧)
<select id="genMaxMinNo" resultType="map">
select GROUPNO as "groupNo",TABLENAME as "tableName",MAXNO as "maxNo",FLAG as "flag" from TEST where id=#{id}
</select>
以上就是我们常用的三种方式!!!!
——————————————————————————————————————————
mabatis配置文件偷懒的几个点:
如果参数只有一个且是简单类型可以不写prameterType
如果返回值是简单类型resultType或者resultMap可以不写
————————————————————————————————————————
我们之所以能在mapperxml中使用简写int,string等等是因为mybatis默认在
org.apache.ibatis.type.TypeAliasRegistry 中为我们定义了类的别名:
registerAlias("string", String.class);
registerAlias("byte", Byte.class);
registerAlias("long", Long.class);
registerAlias("short", Short.class);
registerAlias("int", Integer.class);
registerAlias("integer", Integer.class);
registerAlias("double", Double.class);
registerAlias("float", Float.class);
registerAlias("boolean", Boolean.class);
registerAlias("byte[]", Byte[].class);
registerAlias("long[]", Long[].class);
registerAlias("short[]", Short[].class);
registerAlias("int[]", Integer[].class);
registerAlias("integer[]", Integer[].class);
registerAlias("double[]", Double[].class);
registerAlias("float[]", Float[].class);
registerAlias("boolean[]", Boolean[].class);
registerAlias("_byte", byte.class);
registerAlias("_long", long.class);
registerAlias("_short", short.class);
registerAlias("_int", int.class);
registerAlias("_integer", int.class);
registerAlias("_double", double.class);
registerAlias("_float", float.class);
registerAlias("_boolean", boolean.class);
registerAlias("_byte[]", byte[].class);
registerAlias("_long[]", long[].class);
registerAlias("_short[]", short[].class);
registerAlias("_int[]", int[].class);
registerAlias("_integer[]", int[].class);
registerAlias("_double[]", double[].class);
registerAlias("_float[]", float[].class);
registerAlias("_boolean[]", boolean[].class);
registerAlias("date", Date.class);
registerAlias("decimal", BigDecimal.class);
registerAlias("bigdecimal", BigDecimal.class);
registerAlias("biginteger", BigInteger.class);
registerAlias("object", Object.class);
registerAlias("date[]", Date[].class);
registerAlias("decimal[]", BigDecimal[].class);
registerAlias("bigdecimal[]", BigDecimal[].class);
registerAlias("biginteger[]", BigInteger[].class);
registerAlias("object[]", Object[].class);
registerAlias("map", Map.class);
registerAlias("hashmap", HashMap.class);
registerAlias("list", List.class);
registerAlias("arraylist", ArrayList.class);
registerAlias("collection", Collection.class);
registerAlias("iterator", Iterator.class);
registerAlias("ResultSet", ResultSet.class);