You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 地址表数据映射文件 - 将Java方法映射为SQL语句 -->
<mapper namespace="com.example.mapper.AddressMapper">
<!-- 定义基础字段列表,避免重复编写 -->
<sql id="Base_Column_List">
id,user_id,username,useraddress,phone
</sql>
<!-- 查询所有地址(支持条件查询) -->
<select id="selectAll" resultType="com.example.entity.Address">
select address.*
from address
<where>
<!-- 动态条件:根据传入参数决定是否添加查询条件 -->
<if test="id != null"> and id= #{id}</if>
<if test="userId != null"> and user_id = #{userId}</if>
<if test="username != null"> and username = #{username}</if>
<if test="useraddress != null"> and useraddress = #{useraddress}</if>
<if test="phone != null"> and phone = #{phone}</if>
</where>
</select>
<!-- 根据ID查询单个地址 -->
<select id="selectById" resultType="com.example.entity.Address">
select
<include refid="Base_Column_List" /> <!-- 引用上面定义的字段列表 -->
from address
where id = #{id}
</select>
<!-- 根据ID删除地址 -->
<delete id="deleteById">
delete from address
where id = #{id}
</delete>
<!-- 新增地址(动态插入非空字段) -->
<insert id="insert" parameterType="com.example.entity.Address" useGeneratedKeys="true">
insert into address
<trim prefix="(" suffix=")" suffixOverrides=",">
<!-- 只插入非空的字段 -->
<if test="id != null">id,</if>
<if test="userId != null">user_id,</if>
<if test="username != null">username,</if>
<if test="useraddress != null">useraddress,</if>
<if test="phone != null">phone,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<!-- 对应的值 -->
<if test="id != null">#{id},</if>
<if test="userId != null">#{userId},</if>
<if test="username != null">#{username},</if>
<if test="useraddress != null">#{useraddress},</if>
<if test="phone != null">#{phone},</if>
</trim>
</insert>
<!-- 根据ID修改地址信息动态更新非空字段 -->
<update id="updateById" parameterType="com.example.entity.Address">
update address
<set>
<!-- 只更新非空的字段 -->
<if test="userId != null">
user_id = #{userId},
</if>
<if test="username != null">
username = #{username},
</if>
<if test="useraddress != null">
useraddress = #{useraddress},
</if>
<if test="phone != null">
phone = #{phone},
</if>
</set>
where id = #{id}
</update>
</mapper>