1. Mapper interface annotation
MyBatis is a popular Java data persistence layer framework that allows the use of simple XML or annotations to configure and map native information to map interfaces and Java's POJOs (Plain Old Java Objects) into records in the database.
In MyBatis, the Mapper interface is used to define methods to interact with the database, while annotations such as @Select, @Insert, @Update, and @Delete are used to declare SQL statements directly on the interface method.
Examples of common annotations
1. @Select
Used to perform SQL query operations.
import ; public interface UserMapper { @Select("SELECT * FROM users WHERE id = #{id}") User getUserById(int id); }
2. @Insert
Used to perform SQL insertion operations.
import ; public interface UserMapper { @Insert("INSERT INTO users(name, email) VALUES(#{name}, #{email})") void insertUser(User user); }
3. @Update
Used to perform SQL update operations.
import ; public interface UserMapper { @Update("UPDATE users SET name=#{name}, email=#{email} WHERE id=#{id}") void updateUser(User user); }
4. @Delete
Used to perform SQL deletion operations.
import ; public interface UserMapper { @Delete("DELETE FROM users WHERE id=#{id}") void deleteUserById(int id); }
5. @Results and @Result
Properties used to map columns of the result set to the object. Although they are not used directly for SQL operations, they are usually used in @Select annotations to define mappings for result sets. Here is an example using the DTO type.
import ; import ; import ; public interface UserMapper { @Select("SELECT id, name, email FROM users WHERE id = #{id}") @Results({ @Result(property = "userId", column = "id"), @Result(property = "userName", column = "name"), @Result(property = "userEmail", column = "email") }) UserDTO getUserByIdWithDTO(int id); } // Define a DTO classpublic class UserDTO { private int userId; private String userName; private String userEmail; // getters and setters }
In this example,getUserByIdWithDTO
The method not only performs query operations, but also uses@Results
and@Result
Annotation to map the columns of the result set to the properties of the UserDTO object. Note that the attribute names of the DTO class (such as userId, userName and userEmail) may be different from the column names of the database table. Here,@Result
Annotatedproperty
andcolumn
Attributes to specify mapping relationships.
2. Steps to use the MyBatis Mapper interface
- Define Mapper interface: Define methods in the interface and use annotations to configure SQL statements.
- Configure MyBatis: Register the Mapper interface (or use the Java configuration class) in the MyBatis configuration file.
- Use SqlSessionFactory: Get SqlSession through SqlSessionFactory, and then use SqlSession to get an instance of the Mapper interface.
- Calling the Mapper method: Perform database operations through an instance call method of the Mapper interface.
Sample code
import ; import ; public class MyBatisExample { public static void main(String[] args) { SqlSessionFactory sqlSessionFactory = (); try (SqlSession session = ()) { UserMapper userMapper = (); UserDTO userDTO = (1); (userDTO); } } }
In this example, MyBatisUtil is a tool class that gets an instance of SqlSessionFactory. This tool class needs to be implemented according to the MyBatis configuration. In this way, MyBatis annotations can be used to simplify the configuration of database operations and to encapsulate query results using DTO classes.
This is the article about the Mapper annotation of MyBatis Practical Battle. This is all about this article. For more related contents of MyBatis Practical Battle. Please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!