myBatis:mapper代理方式开发对比

对比下面的代码,可以知道AccountRepository和AccountRepository.xml的对应关系。

------------------------接口------------------------
public interface AccountRepository {
    public int save(Account account);//插入一条记录
    public int update(Account account);//更新一条记录
    public int deleteById(int id);  //删除
    public List<Account> findAll(); //查询
    public Account findById(int id);//查询
}
-------------------------mapper---------------------
<!--namespace中放入接口路径-->
<mapper namespace="com.lmybatis.repository.AccountRepository">

    <insert id="save" parameterType="com.lmybatis.entity.Account">
      insert into t_account(username,password,age) values(#{username},#{password},#{age})
    </insert>
    <delete id="deleteById" parameterType="int">
        delete from t_account where id = #{id}
    </delete>
    <update id="update" parameterType="com.lmybatis.entity.Account">
        update t_account set username=#{username},password=#{password},age=#{age} where id=#{id}
    </update>
    <select id="findAll" resultType="com.lmybatis.entity.Account">
        select * from t_account
    </select>
    <select id="findById" parameterType="int" resultType="com.lmybatis.entity.Account">
        select * from t_account where id=#{id}
    </select>
</mapper>
<!--记住!要在config.xml中配置该xml文件-->

注意:1.要在config.xml中配置该xml文件;2.要在pom.xml中加入以下寻址说明,不然默认只寻找静态文件夹下的文件,不访问自定义在其他文件中的.xml文件

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
发布了126 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36880027/article/details/104458322