Maven
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-join-boot-starterartifactId>
<version>1.4.5version>
dependency>
或者clone代码到本地执行 mvn install, 再引入以上依赖
注意: mybatis plus version 3.3.0+
class test {
@Resource
private UserMapper userMapper;
@Test
void testResultMap() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
.selectAll(UserDO.class)
//一对多查询
.selectCollection(AddressDO.class, UesrDTO::getAddressList)
//一对一查询
//.selectAssociation(AddressDO.class, UesrDTO::getAddress)
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
.eq(UserDO::getId, 1)
.like(UserAddressDO::getTel, "1")
.gt(UserDO::getId, 5);
//关联查询
List<UserDTO> dtoList = userMapper.selectJoinList(UserDTO.class, wrapper);
//分页查询 (这个方法需要启用 mybatis plus 分页插件,或者用PageHelper调用上面那个方法)
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
}
}
等效于ResultMap
<resultMap id="xxxxxxxx" type="com.github.yulichang.join.dto.UserDTO">
<result property="id" column="id"/>
<result property="name" column="name"/>
<collection property="addressList" javaType="java.util.List"
ofType="com.github.yulichang.join.entity.UserAddressDO">
<id property="id" column="mpj_id"/>
<result property="address" column="address"/>
<result property="userId" column="user_id"/>
collection>
resultMap>
MPJLambdaWrapper其他功能
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
MPJLambdaWrapper wrapper = new MPJLambdaWrapper<UserDO>()
.selectAll(UserDO.class)//查询user表全部字段
.select(UserAddressDO::getTel)//查询user_address tel 字段
.selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)//别名 t.address AS userAddress
.select(AreaDO::getProvince, AreaDO::getCity)
.leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId)
.leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId)
.eq(UserDO::getId, 1)
.like(UserAddressDO::getTel, "1")
.gt(UserDO::getId, 5);
//连表查询
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
//分页查询 (这个方法需要启用 mybatis plus 分页插件,或者用PageHelper调用上面那个方法)
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
}
}
对应sql
SELECT
t.id, t.name, t.sex, t.head_img,
t1.tel, t1.address AS userAddress,
t2.province, t2.city
FROM
user t
LEFT JOIN user_address t1 ON t1.user_id = t.id
LEFT JOIN area t2 ON t2.id = t1.area_id
WHERE (
t.id = ?
AND t1.tel LIKE ?
AND t.id > ?)
说明:
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
MPJQueryWrapper wrapper = new MPJQueryWrapper<UserDO>()
.selectAll(UserDO.class)
.select("addr.tel", "addr.address", "a.province")
.leftJoin("user_address addr on t.id = addr.user_id")
.rightJoin("area a on addr.area_id = a.id")
.like("addr.tel", "1")
.le("a.province", "1");
//列表查询
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
//分页查询 (需要启用 mybatis plus 分页插件)
List<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, wrapper);
}
}
对应sql
SELECT
t.id,
t.name,
t.sex,
t.head_img,
addr.tel,
addr.address,
a.province
FROM
user t
LEFT JOIN user_address addr on t.id = addr.user_id
RIGHT JOIN area a on addr.area_id = a.id
WHERE (
addr.tel LIKE ?
AND a.province <= ?)
说明:
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class,
new MPJQueryWrapper<UserDO>()
.selectAll(UserDO.class)
.select("addr.tel", "addr.address")
//行列转换
.select("CASE t.sex WHEN '男' THEN '1' ELSE '0' END AS sex")
//求和函数
.select("sum(a.province) AS province")
//自定义数据集
.leftJoin("(select * from user_address) addr on t.id = addr.user_id")
.rightJoin("area a on addr.area_id = a.id")
.like("addr.tel", "1")
.le("a.province", "1")
.orderByDesc("addr.id"));
}
}
对应sql
SELECT
t.id,
t.name,
t.sex,
t.head_img,
addr.tel,
addr.address,
CASE t.sex WHEN '男' THEN '1' ELSE '0' END AS sex,
sum(a.province) AS province
FROM
user t
LEFT JOIN (select * from user_address) addr on t.id = addr.user_id
RIGHT JOIN area a on addr.area_id = a.id
WHERE (
addr.tel LIKE ?
AND a.province <= ?)
ORDER BY
addr.id DESC