1.1.编写dao接口方法
public interface StudentDao extends BaseMapper<Student> {
IPage<Student> selectPageWithClazz(IPage<Student> page, @Param("ew") Wrapper<Student> queryWrapper);
}
2.编写mapper映射文件的内容
<resultMap id="student" type="com.wzy.entity.Student" autoMapping="true">
<id property="id" column="id"/>
<result property="clazzId" column="clazz_id"/>
<association property="clazz" javaType="com.wzy.entity.Clazz" autoMapping="true">
<id property="cid" column="cid"/>
<result property="clazzName" column="clazz_name"/>
</association>
</resultMap>
<select id="selectPageWithClazz" resultMap="student">
select * from student s join clazz c on s.clazz_id=c.cid
<if test="ew!=null and ew.customSqlSegment!=null">
${ew.customSqlSegment}
</if>
</select>
3.测试
@Autowired
private StudentDao studentDao;
@Test
public void testSelectPageWithDept(){
IPage<Student> iPage=new Page(1,2);
QueryWrapper<Sthdent> wrapper=new QueryWrapper<>();
IPage<Student> sIPage = studentDao.selectPageWithDept(iPage, wrapper);
System.out.println("总页码:"+sIPage.getPages());
System.out.println("总条数:"+sIPage.getTotal());
System.out.println("当前页的记录:");
List<Student> records = sIPage.getRecords();
for(Student s:records){
System.out.println("姓名:"+s.getName()+";薪水:"+s.getSalary()+";所在部门名称:"+s.getDept().getDname());
}
}