In Mybatis, the Mapper XML file is the core configuration file used to define the mapping relationship between SQL statements and Java methods. Through these files, developers can map tables in the database to Java objects to achieve persistent data operations. This article will introduce in detail the relevant knowledge of Mybatis mapping files, including its structure, labels, and how to write and use them.
1. Overview of Mybatis mapping files
Mybatis is a Java persistence layer framework that provides a simple and easy way to access and operate databases. In Mybatis, map files (Mapper XML) play a crucial role, which defines the mapping relationship between SQL statements and Java methods.
2. The structure of the mapping file
Mapper XML files are usually used to define SQL statements and operations that interact with databases. Its basic structure is as follows:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/"> <mapper namespace=""> <!-- TagStandardVO getStandardById(Long id);--> <select resultMap="StandardResultMap"> SELECT id,name,parent_id,enable,sort FROM content_tag WHERE id=#{id} </select> <resultMap type=""> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="parent_id" property="parentId"></result> <result column="enable" property="enable"></result> <result column="sort" property="sort"></result> </resultMap> </mapper>
Detailed explanation of Mapper XML file
The mapper element is the root element of the Mapper XML file. It has a namespace attribute that specifies the corresponding Mapper interface or namespace.
<mapper namespace=""> </mapper>
Define SQL statements and corresponding operations:
: Used to perform query operations.
: Used to perform insertion operations.
: Used to perform update operations.
: Used to perform a delete operation.
Each tag has the following main properties:
id: The unique identifier of the SQL statement. You can use this id to call the corresponding SQL statement in Java code.
parameterType: The parameter type of the SQL statement, which specifies the parameter type of the incoming SQL statement.
resultType or resultMap: If it is a query operation, you can specify the type of return result through resultType, or use resultMap to customize the result mapping rules.
Query operation
<select resultType=""> SELECT , , c.img_url, , , categoryName, c.view_count, c.comment_count, c.create_time FROM t_content c JOIN t_category cat ON c.category_id = WHERE = #{type} AND c.create_by = #{id} </select>
In this example, the tag defines a query operation, with id being selectUserById, the return result type is User, and the SQL statement is SELECT * FROM users WHERE id = #{id}. #{id} is a placeholder that represents a dynamically passed parameter.
Insert operation
<insert > INSERT INTO t_content VALUES (NULL, #{title}, #{imgUrl}, #{videoUrl}, #{content}, #{type}, 0, 0, #{createBy}, #{createTime}, null, null, #{brief}, #{categoryId}) </insert>
In this example, the tag defines an insert operation, with id as insertUser, parameter type as User, and SQL statement INSERT INTO users (username, password) VALUES (#{username}, #{password}). #{username} and #{password} represent properties in the User object, respectively.
Update operation
<update parameterType="User"> UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id} </update>
In this example, the tag defines an update operation, the id is updateUser, the parameter type is User, and the SQL statement is UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}. Here #{id}, #{username} and #{password} are all properties of the User object.
Delete operation
<delete > DELETE FROM t_content WHERE id = #{id} </delete>
In this example, the tag defines a delete operation, id is deleteUser, and the SQL statement is DELETE FROM users WHERE id = #{id}, where #{id} is a dynamically passed parameter.
Dynamic SQL
MyBatis supports the use of tags such as , , , and , to build dynamic SQL statements, and generate SQL fragments dynamically according to conditions, improving the flexibility and reusability of SQL statements.
<update > UPDATE t_content <set> <if test="title!=null">title=#{title},</if> <if test="imgUrl!=null">img_url=#{imgUrl},</if> <if test="brief!=null">brief=#{brief},</if> <if test="videoUrl!=null">video_url=#{videoUrl},</if> <if test="type!=null">type=#{type},</if> <if test="categoryId!=null">category_id=#{categoryId},</if> <if test="viewCount!=null">view_count=#{viewCount},</if> <if test="commentCount!=null">comment_count=#{commentCount},</if> <if test="updateBy!=null">update_by=#{updateBy},</if> <if test="updateTime!=null">update_time=#{updateTime},</if> <if test="content!=null">content=#{content}</if> </set> WHERE id=#{id} </update>
Result Mapping
In addition to the simple resultType property, you can also use tags to customize complex result mapping relationships to map query results in the database to the properties of Java objects.
<select resultMap="StandardResultMap"> SELECT id,name,parent_id,enable,sort FROM content_tag WHERE id=#{id} </select> <resultMap type=""> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="parent_id" property="parentId"></result> <result column="enable" property="enable"></result> <result column="sort" property="sort"></result> </resultMap>
This is the article about the detailed explanation of Mybatis mapping file - the file. For more related Mybatis file content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!