MyBatis ( 三)分页查询

1.db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/questions?characterEncoding=utf8
username=root
password=123

2.MyBatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--所有节点都严格按照一下顺序执行-->
    <properties resource="db.properties"/>

    <typeAliases>
        <typeAlias type="com.page.PageUtil" alias="PageUtil"/>
        <typeAlias type="com.pojo.Answers" alias="Answers"/>
        <typeAlias type="com.pojo.Questions" alias="Questions"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                 <!--${xxx}-->
                <property name="url" value="${url}"/>
                <property name="driver" value="${driver}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/mapper/QuestionMapper.xml"/>
        <mapper resource="com/mapper/AnswersMapper.xml"/>
    </mappers>
</configuration>

3. PageUtil类

    // 1.
    private Integer pageno;
    private Integer pagesize; 
    private Integer totalcount;

    private Integer totalpage;
    private Integer startrow;
    // 2.
     public PageUtil(Integer pageno, Integer pagesize, Integer totalcount) {
        this.pageno = pageno;
        this.pagesize = pagesize;
        this.totalcount = totalcount;
        this.setStartrow(pageno,pagesize);
        this.setTotalpage(pageno,pagesize);
    }
    public void setTotalpage(Integer totalcount,Integer pagesize) {
        this.totalpage = totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1;
    }
    public void setStartrow(Integer pageno,Integer pagesize) {
        this.startrow = (pageno-1)*pagesize;
    }

4.Mapper.xml

<select id="findByPrimaryKey" parameterType="Questions"
            resultMap="findQuestionsMap">
<!--动态 SQL: 参数必须对象,test的必须的是属性的对象值-->
        <choose>
            <when test="count>0">
                SELECT q.*,a.id aid,a.content,a.ansdate
                from
                questions q,answers a
                WHERE
                q.id=a.qid
                AND
                q.id=#{id}
            </when>
            <otherwise>
                select * from questions where id=#{id};
            </otherwise>
        </choose>
    </select>
      <!--一对多-->
       <collection property="answersList" ofType="Answers">
           <id property="id" column="AID"/>
           <result property="content" column="CONTENT"/>
           <result property="ansdate" column="ANSDATE"/>
       </collection>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值