在pom中添加 mybatis plus join依赖
com.github.yulichang mybatis-plus-join-boot-starter 1.4.5 com.baomidou mybatis-plus-boot-starter 3.5.3.1
添加两个数据库实体类 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 addressList; //地址 用于接下来的一对一映射查询 private Address address; }
添加mapper并且继承MPJBaseMapper
@Mapper public interface UserMapper extends MPJBaseMapper{ }
@Mapper public interface AddressMapper extends MPJBaseMapper { }
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapperwrapper = new MPJLambdaWrapper () .selectAll(User.class)//查询user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List userList = userMapper.selectJoinList(UserDTO.class, wrapper); userList.forEach(System.out::println); } }
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=人民广场)
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapperwrapper = new MPJLambdaWrapper () .selectAll(User.class)//查询user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); Page page= userMapper.selectJoinPage(new Page(1,10), UserDTO.class, wrapper); } }
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() { MPJLambdaWrapperwrapper = new MPJLambdaWrapper () .selectAll(User.class)//查询user表全部字段 .selectCollection(Address::getCity, UserDTO::getAddressList) .leftJoin(Address.class, Address::getUserId, User::getId); List userList = userMapper.selectJoinList(UserDTO.class, wrapper); } }
一对一 selectAssociation
@SpringBootTest public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapperwrapper = new MPJLambdaWrapper () .selectAll(User.class)//查询user表全部字段 .selectAssociation(Address::getCity, UserDTO::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List userList = userMapper.selectJoinList(UserDTO.class, wrapper); } }
通过以上几个简单的步骤,我们就实现了 User 表的连表功能,甚至连 XML 文件都不用编写!
从以上步骤中,我们可以看到集成MyBatis-Plus-Join非常的简单,只需要引入 starter 工程即可。
但 MyBatis-Plus-Join 的强大远不止这些功能,想要详细了解 MyBatis-Plus-Join 的强大功能?
可以查阅插件文档 https://mybatisplusjoin.com/