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" >
<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类
private Integer pageno;
private Integer pagesize;
private Integer totalcount;
private Integer totalpage;
private Integer startrow;
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" >
<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 >