Mybatis常用操作

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>示例

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);
    }
}
Mapper: 

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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值