Although MyBatis Plus (MP for short) provides rich CRUD operation methods, in some scenarios, we may need to execute some complex SQL statements, and at this time we need to use the custom SQL function of MyBatis Plus. MyBatis Plus allows us to define custom SQL methods in the Mapper interface and write specific SQL statements in XML files or annotations. This article will explain in detail how to use custom SQL in MyBatis Plus.
1. Define methods in the Mapper interface
First, define methods that require custom SQL in the Mapper interface. For example, we want to query the number of users older than a certain value.
public interface UserMapper extends BaseMapper<User> { int countUsersOlderThan(int age); }
2. Write SQL statements in XML files
Write specific SQL statements in the XML configuration file of MyBatis. Usually, these XML files are stored insrc/main/resources
In the directorymapper
in folder.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/"> <mapper namespace=""> <select resultType="int"> SELECT COUNT(*) FROM user WHERE age > #{age} </select> </mapper>
In the above XML file,<mapper>
Tagsnamespace
The attribute specifies the corresponding Mapper interface.<select>
Tagsid
The attribute corresponds to the method name in the Mapper interface.resultType
The attribute specifies the return value type.#{age}
is the parameter placeholder for MyBatis.
3. Write SQL statements in annotations
In addition to writing SQL statements in XML files, you can also use annotations to write SQL statements directly on the Mapper interface methods.
public interface UserMapper extends BaseMapper<User> { @Select("SELECT COUNT(*) FROM user WHERE age > #{age}") int countUsersOlderThan(int age); }
In the above code,@Select
Annotations are used to specify SQL statements.#{age}
is the parameter placeholder for MyBatis.
4. Use custom SQL methods
After defining custom SQL methods, you can call these methods in business logic.
@Service public class UserService { @Autowired private UserMapper userMapper; public int getUserCountOlderThan(int age) { return (age); } }
In the above code,UserService
Class passed@Autowired
Annotation injectionUserMapper
and call customcountUsersOlderThan
method.
5. Use in combination with the condition constructor
Sometimes, we want to use MyBatis Plus's condition constructor in custom SQL to dynamically generate SQL conditions. Although conditional constructors are mainly used to simplify CRUD operations, they can also be used in custom SQL.
public interface UserMapper extends BaseMapper<User> { @Select("SELECT COUNT(*) FROM user ${}") int countUsersByWrapper(@Param("ew") Wrapper<User> wrapper); }
In the above code,${}
Used to insert SQL fragments generated by the condition constructor.
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ("age", 25); int count = (queryWrapper);
In the above code, we useQueryWrapper
A condition older than 25 is built and passed to a custom SQL method.
6. Summary
MyBatis Plus provides flexible custom SQL capabilities, allowing developers to execute complex SQL statements when needed. Custom SQL requirements can be easily implemented by defining methods in the Mapper interface and writing specific SQL statements in XML files or annotations. In addition, combined with the use of a conditional constructor, the flexibility and maintainability of SQL can be further improved.
This is all about this article about MyBatisPlus custom SQL. For more related content about MyBatisPlus custom SQL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!