用mbgenerator自动生成mybatis的实体和mapper(xml和接口)文件后,如果想在mapper中添加新的操作,就不方便在自动生成的文件上去改,解决办法是定义一个新的接口和新的mapper xml文件。

BlogExtMapper.java

public interface BlogExtMapper extends BlogMapper {
    List<Blog> selectAll();
    List<Blog> selectRecent();
    List<Blog> selectByYearMonth(String yearmonth);
    List<Blog> selectActiveBlogs();
    List<Blog> selectBestBlogs();
    List<BlogStatistic> groupByYearMonth();
    List<Blog> selectBlogsByCategory(int categoryid);
    List<Blog> selectBlogsByRange(int offset);
}

在新的XML定义和接口方法对应的select操作。

BlogExtMapper.xml:

<mapper namespace="com.icool.cms.data.mybatis.extend.BlogExtMapper" >
    <resultMap >
        <result column="c" property="count" jdbcType="INTEGER" />
        <result column="y" property="year" jdbcType="VARCHAR" />
        <result column="m" property="month" jdbcType="VARCHAR" />
    </resultMap>
    <select >
          select
          <include ref />
              ,
          <include ref />
          from BLOG order by createtime desc
    </select>
...

这里主要的注意点就是如何重用BlogMapper.xml中定义的一些ResultMap,其实很简单,用完整的包名即可:

com.icool.cms.data.mybatis.BlogMapper.ResultMapWithBLOBs


相关文章:

  • 2018-11-09
  • 2021-12-14
  • 2021-09-28
  • 2022-02-01
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-06-28
  • 2021-12-24
  • 2022-12-23
  • 2021-06-19
  • 2021-12-12
  • 2022-12-23
  • 2019-09-15
相关资源
相似解决方案