springboot使用mybatis操作MySQL数据库有注解和xml配置文件两种方式,xml配置文件的方式可能会有比较多的坑。这里记录一下:
1、和springmvc不同,springboot配置文件中的classpath是指项目中的resource目录,如mybatis.mapper-locations=classpath:mapper/*Mapper.xml;
2、写查询语句时,resultMap一定要写主键,否则无法启动程序,如:
<resultMap id="musicMap" type="com.example.demo.pojo.Music">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="songname" property="songname" jdbcType="VARCHAR"/>
<result column="author" property="author" jdbcType="VARCHAR"/>
</resultMap>
3、写语句时,语句中的id一定要和映射接口的方法名相同,否则报错,如:
<select id="find" resultMap="musicMap" parameterType="com.example.demo.pojo.Music">
select * from song where 1=1
</select>
(未完待续)