关于使用tk.mybatis中写自定义的mapper的问题

问题

使用tk.mybatis能满足大多数操作,但是想添加自己的查询方法时候今天遇到了坑,总结一下

官方教程

大致分两种

1. 使用纯接口注解方式时

在mapper接口中自定义方法上添加如@Select,@insert类似的注释,里边写相应的sql语句,如下

import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

public interface CountryMapper extends Mapper<Country> {
    @Select("select * from country where countryname = #{countryname}")
    Country selectByCountryName(String countryname);
}

这种方式没有问题

2. 如果使用 XML 方式,需要提供接口对应的 XML 文件

例如提供了 CountryMapper.xml 文件,内容如下:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="tk.mybatis.sample.mapper.CountryMapper">
    <select id="selectByCountryName" resultType="tk.mybatis.model.Country">
        select * from country where countryname = #{countryname}
    </select>
</mapper>

这里会有点问题
注意: resultType 如果映射的是个实体类最好写 包的全路径名tk.mybatis.model.Country 如果写 Country会报错,导致项目无法启动。

猜你喜欢

转载自blog.csdn.net/weixin_41016986/article/details/84203200
今日推荐