Write SQL directly in mapper.xml

  • Mapper.java
public interface PeopleMapper {
    List<People> limit(Map<String, Object> map);
}
Copy the code
  • Mapper.xml

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mybatis.DAO.PeopleMapper">
    <resultMap id="PMap" type="P">
        <result column="id" property="idid"/>
    </resultMap>
    <select id="limit" parameterType="map" resultType="P">
        select * from mybatis.people limit #{startindex},#{pagesize};
    </select>
</mapper>
Copy the code

Or use Java code (RowBounds)

  • interface
public interface PeopleMapper {
    List<People> limitRowBounds(a);
}
Copy the code
  • Mapper.xml

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.DAO.PeopleMapper">
    <resultMap id="PMap" type="P">
        <result column="id" property="idid"/>
    </resultMap>
    <select id="limitRowBounds" resultMap="PMap">
        select * from mybatis.people;
    </select>
</mapper>
Copy the code
  • Paging Java code
public class PeopleDAOtest {
    @Test
    public void limit01(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RowBounds bounds = new RowBounds(1.3);
        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        List<People> people = sqlSession.selectList("com.mybatis.DAO.PeopleMapper.limitRowBounds".null,bounds);
        for(People p : people) { System.out.println(p); }}}Copy the code

Or use the Mybatis plugin

  • PageHelper url