阅读量:0
mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的联表查询能力一直被大家所诟病。一旦遇到left join或right join的左右连接,你还是得老老实实的打开xml文件,手写上一大段的sql语句。
偶然碰到了这么一款叫做mybatis-plus-join的工具(后面就简称mpj了),使用了一下,不得不说真香!彻底将我从xml地狱中解放了出来,终于可以以类似mybatis-plus中QueryWrapper的方式来进行联表查询了,话不多说,我们下面开始体验。
插件文档 https://mybatisplusjoin.com
插件Github仓库 https://github.com/yulichang/mybatis-plus-join
一、添加依赖
在pom中添加 mybatis plus join依赖
<!-- mpj 依赖 --> <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-plus-join-boot-starter</artifactId> <version>1.4.5</version> </dependency> <!-- mp 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency>
二、创建实体
添加两个数据库实体类 User 和 Address 和结果类 UserDTO
这里用lombok简单代码
@Data @ToString @TableName("area") public class User { @TableId private Long id; private String name; private Integer age; private String email; }
@Data @ToString @TableName("address") public class Address { @TableId private Long id; private Long userId; private String city; private String address; }
@Data @ToString public class UserDTO{ private Long id; private String name; private Integer age; private String email; //address关联表中的两个字段 private String city; private String address; //地址列表 用于接下来的一对多映射查询 private List<Address> addressList; //地址 用于接下来的一对一映射查询 private Address address; }
三、创建mapper
添加mapper并且继承MPJBaseMapper
@Mapper public interface UserMapper extends MPJBaseMapper<User> { }
@Mapper public interface AddressMapper extends MPJBaseMapper<Address> { }
四、连表查询测试
实体和mapper都建好了就可以直接用了~~
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查询user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List<UserDTO> userList = userMapper.selectJoinList(UserDTO.class, wrapper); userList.forEach(System.out::println); } }
sql打印
SELECT t.id,t.name,t.age,t.email,t2.city,t2.address FROM user t LEFT JOIN address t1 ON t1.user_id = t.id
控制台输出
User(id=1, name=Jone, age=18, email=test1@baomidou.com,city=北京,address=人民广场) User(id=2, name=Jack, age=20, email=test2@baomidou.com,city=上海,address=人民广场) User(id=3, name=Tom, age=28, email=test3@baomidou.com,city=广州,address=人民广场) User(id=4, name=Sandy, age=21, email=test4@baomidou.com,city=上海,address=人民广场) User(id=5, name=Billie, age=24, email=test5@baomidou.com,city=北京,address=人民广场)
连表分页也是很常用的功能,MPJ也支持,调用selectJoinPage()就可以了
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查询user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); Page<UserDTO> page= userMapper.selectJoinPage(new Page(1,10), UserDTO.class, wrapper); } }
sql打印可以看到多了分页方言
SELECT t.id,t.name,t.age,t.email,t2.city,t2.address FROM user t LEFT JOIN address t1 ON t1.user_id = t.id LIMIT ?
小结:
通过以上几个简单的步骤,我们就实现了 User 表的连表功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成MyBatis-Plus-Join非常的简单,只需要引入 starter 工程即可。
但 MyBatis-Plus-Join 的强大远不止这些功能,
可以查阅插件文档 https://mybatisplusjoin.com/
接下来测试一对多和一对一映射查询
五、一对多、一对一映射
一对多 selectCollection
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查询user表全部字段 .selectCollection(Address::getCity, UserDTO::getAddressList) .leftJoin(Address.class, Address::getUserId, User::getId); List<UserDTO> userList = userMapper.selectJoinList(UserDTO.class, wrapper); } }
一对一 selectAssociation
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查询user表全部字段 .selectAssociation(Address::getCity, UserDTO::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List<UserDTO> userList = userMapper.selectJoinList(UserDTO.class, wrapper); } }
六、总结
通过以上几个简单的步骤,我们就实现了 User 表的连表功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成MyBatis-Plus-Join非常的简单,只需要引入 starter 工程即可。
但 MyBatis-Plus-Join 的强大远不止这些功能,想要详细了解 MyBatis-Plus-Join 的强大功能?
可以查阅插件文档 https://mybatisplusjoin.com/