How to associate other tables with querywrapper in Java?
In Java, using QueryWrapper to associate with other tables can be achieved by using Lambda expressions or directly writing SQL fragments. Here are examples of both methods.
- Use Lambda expressions to join other tables.
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getDepartmentId, 1);
List<User> userList = userMapper.selectList(queryWrapper);
- Join other tables using SQL fragments.
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("user.department_id = department.id");
List<User> userList = userMapper.selectList(queryWrapper);
It is important to avoid SQL injection attacks when using SQL fragments by ensuring that the input parameters are secure. Additionally, using Lambda expressions provides more type safety, so it is recommended to use Lambda expressions to associate other tables whenever possible.