xiaowei8782088 发表于 2016-11-27 12:49:25

通过注解多参数方式解决mybatis物理分面

1. 所有的mapper 都是通过mybatis-generator-core-1.3.1.jar(之前的博文中有介绍) 生成了..
   唯独没有提供分页方法..
2百度很时间终打到满意答案.
   废话不多说,直接上代码
    1 mapper 基类
      public interface BaseMapper<T, E, K> {
int countByExample(E e);
int deleteByExample(E e);
int deleteByPrimaryKey(K k);
int insert(T t);
int insertSelective(T t);
List<T> selectByExample(E e);
T selectByPrimaryKey(K k);
int updateByExampleSelective(@Param("record") T t, @Param("example") E e);
int updateByExample(@Param("record") T t, @Param("example") E e);
int updateByPrimaryKeySelective(K k);
int updateByPrimaryKey(T t);
List<T> queryForList(E e, int start, int end);
List<T> selectOnPage(@Param("example")E example, @Param("skipResults")int skipResults,
@Param("maxResults")int maxResults);
}

2 serivce 片断
      
public interface TrmDayBookMapper extends BaseMapper<TrmDayBook, TrmDayBookExample, TrmDayBookKey> {}
空实现一下
@Service
public class TrmDayBookBSImp extends BaseServiceImp<TrmDayBook, TrmDayBookExample, TrmDayBookKey> implements
ITrmDayBookBS<TrmDayBook, TrmDayBookExample, TrmDayBookKey> {
@Autowired
TrmDayBookMapper trmDayBookMapper;
public JFTResponse<TrmDayBookResVO> selectOnPage(TrmDayBookReqVO reqVO) {
try {
JFTResponse<TrmDayBookResVO> jftResponse = new JFTResponse<TrmDayBookResVO>("0");
reqVO.reckonPage();
TrmDayBookExample e = new TrmDayBookExample();
e.createCriteria().andCardNoEqualTo(reqVO.getCardNo()).andTrDateBetween(reqVO.getStartDate(),
reqVO.getEndDate());
e.setOrderByClause("SER_NOasc , ORG_ID desc");
List<TrmDayBook> list = trmDayBookMapper.selectOnPage(e,reqVO.getCurrPage(),reqVO.getPageSize());
Business bus = new Business();
bus.setResultset(bean2Vo(list));
...

   3...mapper.xml
    此sql generator 不会自动生成.只能每一个mapper.xml手工加上..累啊,
    有大侠研究源码,加上这些东西,就完美了..顺便分享一下..
      <select id="selectOnPage" resultMap="BaseResultMap">
SELECT * FROM (
SELECT page.*, ROWNUM AS rn FROM
(
select
<if test="example.distinct">
distinct
</if>
<include refid="Base_Column_List" />
from TRM_DAY_BOOK
<if test="_parameter!= null">
<include refid="Update_By_Example_Where_Clause" />
</if>
<if test="example.orderByClause != null">
order by ${example.orderByClause}
</if>
)
page WHERE ROWNUM &lt;= #{skipResults}
)
WHERE rn &gt;= #{maxResults}
</select>
       这里的代码和selectByExample 生成的类似..请注意多出的 examp.
4 mybatis 自动生成的sql

SELECT *
FROM (SELECT page.*, ROWNUM AS rn
FROM (select
*                     
from TRM_DAY_BOOK
WHERE (CARD_NO = ? and TR_DATE between ? and ?)
order by SER_NO asc, ORG_ID desc) page
WHERE ROWNUM <= ?)
WHERE rn >= ?

时间有限,写在不明了的请轻拍.
参考 以下博客,特此感谢
http://hi.baidu.com/cheung_ming/item/a18bb11c449c01cc38cb303b
http://www.iyunv.com/topic/1122976
页: [1]
查看完整版本: 通过注解多参数方式解决mybatis物理分面