mybatis-plus是对mybatis的扩展,非常的好用,不管对单表还是多表都提供了分页的功能,后续也有分页的例子,而且针对单表的增删改查只需要继承BaseMapper,再根据QueryWrapper和UpdateWrapper就可以简单的对单表进行操作了,最后有一个简单的图解供参考。
多表的分页只需要在Mapper里的方法中传入Page这个对象就可以自动分页了,mybatis-plus是基于mybatis,也要引入对应的包。本项目是基于gradle构建的。 用到的jar包,如果是springboot项目:
// https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.1.0'
// https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.2.0'
如果单独引包:
// https://mvnrepository.com/artifact/org.mybatis/mybatis
compile group: 'org.mybatis', name: 'mybatis', version: '3.5.2'
// https://mvnrepository.com/artifact/com.baomidou/mybatis-plus
compile group: 'com.baomidou', name: 'mybatis-plus', version: '3.2.0'
package org.sjframework.learn.mybatis.plus;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.sjframework.learn.mybatis.UserDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* mybatis-plus
*
* @author 719383495@qq.com |719383495qq@gmail.com |gfu
* @date 2019/10/18
*/
@Mapper
public interface UserDaoPlus extends BaseMapper<UserDO> {
}
package org.sjframework.learn.mybatis;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
/**
* user dao
*
* @author 719383495@qq.com |719383495qq@gmail.com |gfu
* @date 2019/10/18
*/
@TableName("t_user")
public class UserDO {
private String userId;
private String leader;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLeader() {
return leader;
}
public void setLeader(String leader) {
this.leader = leader;
}
@Override
public String toString() {
return "UserDO{" +
"userId='" + userId + '\'' +
", leader='" + leader + '\'' +
'}';
}
}
package org.sjframework.learn.mybatis;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sjframework.learn.mybatis.plus.UserDaoPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* mybatis test
*
* @author 123456789@qq.com |123456789@gmail.com |gfu
* @date 2019/10/17
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusTest {
@Autowired
UserDaoPlus userDaoPlus;
@Test
public void loadContext() {
Page<UserDO> page = new Page<>(2, 100);
QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
// 分页
IPage<UserDO> mapIPage = userDaoPlus.selectPage(page, wrapper);
System.out.println(mapIPage.getRecords());
List<UserDO> records = mapIPage.getRecords();
records.forEach(i -> System.out.println(i));
// 筛选
wrapper.eq("user_id", "U00003");
List<UserDO> res = userDaoPlus.selectList(wrapper);
System.out.println(res);
}
}