Mybatis-plus-join目前(2021-05-08)发布在Gitee上,直接将其克隆/下载到本地即可:
下载地址: Mybatis-plus-join源码地址.
注意:需要 Mybatis-plus version >= 3.4.0
2.打包成功后,jar包存在的目录,找到jar包并复制到自己的项目中:
3.将打出来的jar包引入工程模块中,步骤如图(如果跳出版本问题,则点击更新即可):
在pom.xml中引入该依赖:
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-joinartifactId>
<version>1.1.6version>
dependency>
123456
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
List list = userMapper.selectJoinList(UserDTO.class,
new MPJLambdaWrapper()
.selectAll(UserDO.class)
.select(UserAddressDO::getTel)
.selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)
.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));
}
}
对应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() {
IPage iPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class,
new MPJLambdaWrapper()
.selectAll(UserDO.class)
.select(UserAddressDO::getTel)
.selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)
.select(AreaDO::getProvince, AreaDO::getCity)
.leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId)
.leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId));
}
}
对应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
LIMIT ?,?
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
List list = userMapper.selectJoinList(UserDTO.class,
new MPJQueryWrapper()
.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"));
}
}
对应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() {
IPage page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
new MPJQueryWrapper()
.selectAll(UserDO.class)
.select("addr.tel", "addr.address")
.select("a.province")
.leftJoin("user_address addr on t.id = addr.user_id")
.rightJoin("area a on addr.area_id = a.id"));
}
}
对应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
LIMIT ?,?
class test {
@Resource
private UserMapper userMapper;
void testJoin() {
List list = userMapper.selectJoinList(UserDTO.class,
new MPJQueryWrapper()
.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
链接: mybatis-plus-join更多使用方法.