The mapper.xml file is a mapping file of the mapper interface class, which is used to store database SQL operation statements
1. Insert data:
1) Insert data into a known table:
<insert id="addCategory" useGeneratedKeys="true" keyProperty="id">
INSERT INTO category (id,name,label,logo_type)
VALUES (#{id},#{name},#{label},#{logoType});
</insert>
2) Insert data into the dynamic table:
<insert id="insertExhibitImgUrl" useGeneratedKeys="true" keyColumn="id">
INSERT INTO ${tableName} (imgurl)
VALUES (#{imgurl});
</insert>
3) Notes:
2. Delete data:
1) Delete an item of matching data:
<delete id="deleteExhibitImgUrl">
DELETE FROM ${tableName} WHERE id=#{id};
</delete>
2) Delete the records in the entire table:
<delete id="deleteExhibitContent">
DELETE FROM ${tableName};
</delete>
3. Update data and tables
1) Update a certain data in the table according to the condition:
<update id="updateBannerUrlById">
UPDATE banner
<set>
<if test="imgUrl !=null">
img_url=#{imgUrl}
</if>
</set>
WHERE banner_id=#{bannerId};
</update>
2) create table
<update id="createExhibitImgTable" useGeneratedKeys="true" keyProperty="id">
CREATE table ${tableName}
(id int NOT NULL AUTO_INCREMENT,imgurl varchar(255),PRIMARY KEY (id));
</update>
4. Query data
1) Count the number of data items in the table
<select id="count" resultType="int">
SELECT count(*) FROM banner;
</select>
2. Query qualified data
<select id="getBannerById" resultMap="GetBannersMap">
SELECT <include refid="BannersQueryFields"></include>
FROM banner
WHERE banner_id=#{bannerId}
</select>
<resultMap id="GetBannersMap" type="com.example.demo.pojo.vo.BannerVO">
<id column="banner_id" property="bannerId"></id>
<result column="img_url" property="imgUrl"></result>
<result column="a_href" property="abnHref"></result>
<result column="is_use" property="isUse"></result>
<result column="need_href" property="needHref"></result>
<result column="need_imgurl" property="needImgUrl"></result>
<result column="add_datetime" property="addDatetime"></result>
</resultMap>
<sql id="BannersQueryFields">
banner_id,img_url,a_href,is_use,need_href,need_imgurl,add_datetime
</sql>
5. Notes
1) Use ${} and #{} as two variable modifiers: except for database and table names, use #, and use $ modifier for table name variables.
2) The tag id attribute value corresponds to the mapper interface method name
3) <set></set> tags are used to store updated content
4) <if></if> conditional statement, the value passed in by test
5) When creating a table, set the primary key auto-increment: (id int NOT NULL AUTO_INCREMENT, imgurl varchar(255), PRIMARY KEY (id)); not null cannot be less, note that the PRIMARY KEY (id) should be placed in the entire brackets, in An error will be reported outside, and the AUTO_INCREMENT setting will increase automatically