MybatisPlus multi-table join query

As an enhanced tool of mybatis, mybatis-plus greatly simplifies the database operation in development, but its joint table query ability has been criticized by everyone for a long time. Once you encounter left join or right join, you still have to honestly open the xml file and write a large paragraph of sql statement by hand.

I came across such a tool called mybatis-plus-join by chance (hereinafter referred to as mpj), and after using it, I have to say it is really delicious! It completely liberated me from xml hell, and finally I can perform joint table query in a way similar to QueryWrapper in mybatis-plus. Without further ado, let's start to experience it below.

Plug-in documentation https://mybatisplusjoin.com

Plug-in Github repository https://github.com/yulichang/mybatis-plus-join

1. Add dependencies

Add mybatis plus join dependency in pom

<!-- 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>

2. Create entities

Add two database entity classes User and Address and the result class UserDTO
here with lombok simple code

@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;
}

3. Create mapper

Add mapper and inherit MPJBaseMapper

@Mapper
public interface UserMapper extends MPJBaseMapper<User> {
    
    

}
@Mapper
public interface AddressMapper extends MPJBaseMapper<Address> {
    
    

}

4. Linked table query test

After the entity and mapper are built, they can be used directly~~
@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 print
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
console output
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=人民广场)
Even table paging is also a very common function, and MPJ also supports it, just call 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);
    }
}
You can see more pagination dialects in sql printing
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 ?

Summary:
Through the above few simple steps, we have realized the table linking function of the User table, and even the XML file does not need to be written!
From the above steps, we can see that integrating MyBatis-Plus-Join is very simple, just need to import the starter project.
But the power of MyBatis-Plus-Join is far more than these functions,
you can refer to the plug-in documentation https://mybatisplusjoin.com/
Next test one-to-many and one-to-one mapping queries

Five, one-to-many, one-to-one mapping

One-to-many 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);
    }
}

One-to-one 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);
    }
}

6. Summary

Through the above few simple steps, we have realized the table linking function of the User table, and we don't even need to write XML files!
From the above steps, we can see that integrating MyBatis-Plus-Join is very simple, just need to import the starter project.
But the power of MyBatis-Plus-Join is far more than these functions. Want to learn more about the powerful functions of MyBatis-Plus-Join?
You can check the plugin documentation https://mybatisplusjoin.com/

Guess you like

Origin blog.csdn.net/bjdiys/article/details/130230405