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>
(未完待續)