1 实现一对多
Myabtis的resultMap带集合,实现一对多
单个主键:
复合主键:
2 @MapKey
Mapper:
@MapKey("id")
Map<Long,Test> selectTests (String id);
Mapper.xml:
<select id="selectTests" resultType="test.Test">
SELECT *
FROM test
WHERE id = #{id}
</select>
说明:将查询返回的结果集中的Test的id属性作为map的key。
注意:mapper.xml中的select节点里面的resultType为Test对象含包名的全路径
3分页
1>导包
compile('com.github.pagehelper:pagehelper:5.0.1')
2>配置
import com.github.pagehelper.PageInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; /** * 使用myabtis的pageHelper分页插件 * Created by Administrator on 2017/6/7. */ @Configuration public class PageConfiguration { @Bean public PageInterceptor pageInterceptor() { PageInterceptor interceptor = new PageInterceptor(); Properties pageHelperProps = new Properties(); pageHelperProps.setProperty("helperDialect", "mysql"); pageHelperProps.setProperty("offsetAsPageNum", "true"); pageHelperProps.setProperty("pageSizeZero", "true"); pageHelperProps.setProperty("rowBoundsWithCount", "true"); interceptor.setProperties(pageHelperProps); return interceptor; } }
3>示例
Mapper:import com.github.pagehelper.Page; import test.PageRespInfo; /** * 分页的响应类 * Created by Administrator on 2017/6/7. */ public class TestResponse extends PageRespInfo { public TestResponse(Page pageInfo) { super(pageInfo); } }
Page<Test> selectTest (@Param("id")String id);
Service:
Page<Test> testList = testMapper.selectTest(id); return new TestResponse(testList);
附加>通用分页类
import java.util.ArrayList; import java.util.List; /** * 分页基类 * Created by Administrator on 2017/6/7. */ public class PageRespInfo<T>{ @ApiModelProperty(value = "页码,从1开始", required = true) private int pageNum; @ApiModelProperty(value = "页面大小", required = true) private int pageSize; @ApiModelProperty(value = "起始行", required = true) private int startRow; @ApiModelProperty(value = "末行", required = true) private int endRow; @ApiModelProperty(value = "总页数", required = true) private int pages; @ApiModelProperty(value = "总行数", required = true) private long total; @ApiModelProperty(value = "内容列表", required = true) private List<T> list; public PageRespInfo(Page<T> pageInfo) { this.pageNum = pageInfo.getPageNum(); this.pageSize = pageInfo.getPageSize(); this.startRow = pageInfo.getStartRow(); this.endRow = pageInfo.getEndRow(); this.pages = pageInfo.getPages(); this.total = pageInfo.getTotal(); list = new ArrayList<>(); for (T t : pageInfo) { list.add(t); } } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getStartRow() { return startRow; } public void setStartRow(int startRow) { this.startRow = startRow; } public int getEndRow() { return endRow; } public void setEndRow(int endRow) { this.endRow = endRow; } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }