一、错误描述:springboot项目运行后提示写在mapper中的xml方法找不到,但是target目录已经有了mapper.xml。截图如下:
解释:这是由于mapper接口没有连接上mapper.xml文件。
解决方法有:
方法一:
在application.properties将mybatis配置改为mybatis-plus配置,理由:mybatis-plus插件默认路径的权限高于mybatis,我出现了文件路径规范的错误,plus默认只有一层目录mapper。我设了两层。
mybatis-plus.type-aliases-package=com.kuang.pojo//你的实体类包路径
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml//你的mapper.xml在resources的路径
方法二:点击projectstructuresh,设置module中的你的mapper文件,整个路径都要设为resoerces类型,就可以随便访问了,也不用设置application.properties的mybatis-plus.mapper-locations
二、错误描述:数据库对象中表的字段带下划线_导致mapper.xml中resulttype出错,原因实体类entity或pojo中字段也有_导致mapper.xnl对象解析错误。
解决方法:
方法一:不使用resultType=“Shop”,改用自定义一一映射resultMap(针对于没有mybatis-Plus配置:mybatis-plus.type-aliases-package=com.kuang.pojo
mybatis-plus.mapper-locations=classpath:mybatis/mapper/*.xml)
(1)启用resulttype改用resultmap一一对应,这种针对1.(1),没有设置Mybatis-plus
<select id="select" resultMap="GetsMap">
select * from gowhere.shop;
</select>
<resultMap id="GetsMap" type="com.kuang.pojo.Shop">
<result column="Sh_id" property="Sh_id"></result>
<result column="User_id" property="User_id"></result>
<result column="Sh_name" property="Sh_name"></result>
<result column="Sh_type" property="Sh_type"></result>
<result column="Sh_detail" property="Sh_detail"></result>
</resultMap>
方法二:将实体类entity或pojo中字段有_的字段手动转为大写字母,如:sh_id改为shId.
然后spring boot 驼峰转换properties文件配置mybatis.configuration.map-underscore-to-camel-case=true可以不设,亲测可以用。
这种针对于1.(2)设置了Mybatis-plus