记录三个在学习ssm
发生的错误
1. mapper.xml中的parameterType与parameterMap, resultType与resultMap注意不要写错。
<mapper namespace="yang.mapper.AccountMapper">
<insert id="save" parameterType="account">
insert into account values(#{id},#{name},#{money})
</insert>
<select id="findAll" resultType="account">
select * from account
</select>
</mapper>
附:resultType
与resultMap
的区别
1)resultType
:当使用resultType
做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType
中的内容就是pojo在本项目中的位置。
因此对于单表查询的话用resultType
是最合适的
2)resultMap
:当使用resultMap
做SQL语句返回结果类型处理时,通常需要在mapper.xml
中定义resultMap
进行pojo和相应表字段的对应。
resultMap
对于一对一表连接的处理方式通常为在主表的pojo中添加嵌套另一个表的pojo,然后在mapper.xml
中采用association节点元素进行对另一个表的连接处理
3)resultType
是直接表示返回类型的(对应着我们的model对象中的实体)
resultMap
则是对外部ResultMap
的引用(提前定义了db和model之间的隐射key-->value
关系),但是resultType
跟resultMap
不能同时存在。
2. resource包下的mapper的名称注意与java包下的名称需要一样(包括前缀例如com.yang.AccountMapper.java与com.yang.AccountMapper.xml)。
3. sqlMapConfig.xml中的default注意填写
引起的异常:
- 由于Type写成Map而引起的
Type interface is not known to the MapperRegistry” exception using mybatis
- 由于default中未写入变量而引起的
空指针异常NullPointException
- 由于2即名称前缀不一样而引起的
org.apache.ibatis.binding.BindingException:…