就昨天使用mybatis遇到的问题做下记录。
以前在老公司写mybatis时,全都是用 resultMap,而resultMap定义如下:
<resultMap type="java.util.Map" id="relationsMap">
<id property="column_name" column="column_name" />
<result property="url" column="url" />
<result property="collection_name" column="collection_name" />
</resultMap>
type又是Map类型。其实这是不必的,它本身就已经是map了。使用resultMap,是对外部定义的<resultMap></resultMap>的引用,一般是用来做model的映射。例如有个User,你想用mybatis直接转成User,就应该这样写:
<resultMap type="com.yicong.User" id="user">
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<select id="getColumnRelations" parameterType="string" resultMap="user">
SELECT * from user
</select>
所以如果没有要映射成model,只是要map而已,就不需要定义resultMap,直接用resultType=“map”。
resultType=“map”是通用的。如果sql只返回一条记录,外部就可以只定义Map来接收,如 public Map getJobConfByJobId(@Param("jobId") String jobId)
如果返回多条记录,用public List<Map> getJobConf()。
另外遇到一个问题:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfMapper' defined in file [E:\Welan\kisp\apache-tomcat-7.0.63\webapps\kisp-dev\WEB-INF\classes\com\szkingdom\kisp\mapper\AppConfMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.szkingdom.kisp.mapper.AppConfMapper.getBizConf
找了老半天也不知道什么错。以为是xml格式不对,校验后是正确的。后来才发现是有两个<select>的id重名了。以后留心吧