springboot基於xml配置的mybatis整合

根據上一篇的代碼改寫

先增加配置

#指定xml配置的位置
mybatis.mapper-locations=classpath:mapper/*.xml

然後在主類中增加


@MapperScan("com.zzaxg.springbootmybatisxml.mapper")
@SpringBootApplication
public class SpringbootMybatisXmlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisXmlApplication.class, args);
    }

}

指明掃描的包名

可以去掉UserMapper的@Select註解了,改爲在mapper.Usermapper.xml中配置映射關係

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzaxg.springbootmybatisxml.mapper.UserMapper">
    <select id="findByName" resultType="com.zzaxg.springbootmybatisxml.entity.User">
        select * from user where name = #{name}
    </select>


    <insert id="insert">
        insert into user(name, age) values(#{name}, #{age})
    </insert>
</mapper>

然後測試,與上文相同

是不是很簡單?

參考程序猿DD的博客

謝謝觀看

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章