pull/4/head
zhouyu 9 months ago
parent 4e668b56a5
commit 13343e57fa

@ -1,9 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?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" >
<!-- 此 XML 文件是 MyBatis 的映射文件Mapper用于定义与数据库交互的 SQL 语句以及数据库结果与 Java 对象之间的映射关系。
namespace 属性指定了该映射文件对应的接口全限定名(这里是 com.njupt.swg.dao.UserMapperMyBatis 通过这个命名空间将 XML 中的 SQL 操作与对应的接口方法关联起来,
使得在代码中调用接口方法时MyBatis 能准确找到对应的 SQL 语句去执行数据库操作。 -->
<mapper namespace="com.njupt.swg.dao.UserMapper" >
<!-- resultMap 元素用于定义从数据库查询结果到 Java 对象的映射规则,它的 id 属性(这里是 "BaseResultMap")是该映射关系的唯一标识符,
在其他 SQL 语句配置中可以通过这个 id 来引用此映射关系。type 属性指定了映射的目标 Java 对象类型,即 com.njupt.swg.entity.User 类,
表示将数据库查询结果按照下面定义的规则映射到这个 User 类的对象实例上。 -->
<resultMap id="BaseResultMap" type="com.njupt.swg.entity.User" >
<!-- constructor 元素用于指定使用构造函数来实例化目标 Java 对象,在构造函数中通过 idArg 和 arg 元素来定义参数与数据库列的映射关系。 -->
<constructor >
<!-- idArg 元素用于定义主键列与 Java 对象构造函数参数的映射关系。这里将数据库表中的 "id" 列映射到 Java 中的 Integer 类型的构造函数参数上,
同时通过 jdbcType 属性指定了该列在数据库中的 JDBC 类型为 INTEGER确保数据类型的正确转换。 -->
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<!-- arg 元素用于定义普通列与 Java 对象构造函数参数的映射关系。以下依次将数据库表中的各列与 User 类的相应构造函数参数进行映射,
明确了各列对应的 Java 类型以及在数据库中的 JDBC 类型,例如将 "username" 列映射到 String 类型的参数等,方便在查询结果返回时创建对应的 Java 对象。 -->
<arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="email" jdbcType="VARCHAR" javaType="java.lang.String" />
@ -15,158 +27,190 @@
<arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
</constructor>
</resultMap>
<!-- sql 元素用于定义可复用的 SQL 片段,通过给它一个唯一的 id这里是 "Base_Column_List"),方便在其他 SQL 语句中通过 <include> 标签引用这个片段,
避免重复编写相同的列选择语句,提高代码的复用性和可维护性。这里定义的是查询时常用的列名列表,对应数据库表中的各列。 -->
<sql id="Base_Column_List" >
id, username, password, email, phone, question, answer, role, create_time, update_time
</sql>
<!-- select 元素用于定义一个查询 SQL 语句id 属性("selectByPrimaryKey")是该查询语句的唯一标识,与对应的接口方法名相对应,
resultMap 属性指定了使用前面定义的 "BaseResultMap" 来映射查询结果到 Java 对象parameterType 属性指定了传入参数的类型为 Java 的 Integer 类型,
表示这个查询是根据主键(通常是一个整数类型的 id 值)来查询对应的记录,并按照定义的映射规则将结果转换为 User 对象返回。 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
select
<!-- 通过 <include> 标签引用前面定义的 "Base_Column_List" SQL 片段,这样就将常用的列选择语句包含进来,避免重复编写,使得 SQL 语句更简洁易维护。 -->
<include refid="Base_Column_List" />
from mmall_user
where id = #{id,jdbcType=INTEGER}
</select>
<!-- delete 元素用于定义一个删除 SQL 语句id 属性("deleteByPrimaryKey"标识该删除语句parameterType 属性指定传入参数类型为 Integer
表示此 SQL 语句用于根据主键(传入的整数类型的 id 值来删除数据库表mmall_user中的对应记录。 -->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from mmall_user
where id = #{id,jdbcType=INTEGER}
</delete>
<!-- insert 元素用于定义一个插入 SQL 语句id 属性("insert"是该插入语句的标识parameterType 属性指定传入参数的类型为 com.njupt.swg.entity.User 类,
意味着要插入的数据来源于一个 User 类型的对象,此 SQL 语句将 User 对象中的各属性值插入到数据库表mmall_user的对应列中
其中对于时间相关的列create_time 和 update_time使用了数据库函数 now() 来获取当前时间插入,确保插入的记录包含正确的时间戳信息。 -->
<insert id="insert" parameterType="com.njupt.swg.entity.User" >
insert into mmall_user (id, username, password,
email, phone, question,
answer, role, create_time,
update_time)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR},
#{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, now(),
now())
insert into mmall_user (id, username, password,
email, phone, question,
answer, role, create_time,
update_time)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR},
#{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, now(),
now())
</insert>
<!-- insertSelective 元素同样用于定义插入 SQL 语句,但与上面的 "insert" 不同,它采用了动态 SQL 的方式,更具灵活性。
通过 <trim><if> 标签组合,根据传入的 User 对象中属性值是否为 null 来动态决定要插入哪些列及其对应的值,避免插入 null 值到数据库中,
例如只有当 User 对象中的某个属性(如 id、username 等)不为 null 时,才会将对应的列及其值添加到插入语句中,使得插入操作更加智能和符合实际业务需求。 -->
<insert id="insertSelective" parameterType="com.njupt.swg.entity.User" >
insert into mmall_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
<if test="id!= null" >
id,
</if>
<if test="username != null" >
<if test="username!= null" >
username,
</if>
<if test="password != null" >
<if test="password!= null" >
password,
</if>
<if test="email != null" >
<if test="email!= null" >
email,
</if>
<if test="phone != null" >
<if test="phone!= null" >
phone,
</if>
<if test="question != null" >
<if test="question!= null" >
question,
</if>
<if test="answer != null" >
<if test="answer!= null" >
answer,
</if>
<if test="role != null" >
<if test="role!= null" >
role,
</if>
<if test="createTime != null" >
<if test="createTime!= null" >
create_time,
</if>
<if test="updateTime != null" >
<if test="updateTime!= null" >
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
<if test="id!= null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
<if test="username!= null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
<if test="password!= null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="email != null" >
<if test="email!= null" >
#{email,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
<if test="phone!= null" >
#{phone,jdbcType=VARCHAR},
</if>
<if test="question != null" >
<if test="question!= null" >
#{question,jdbcType=VARCHAR},
</if>
<if test="answer != null" >
<if test="answer!= null" >
#{answer,jdbcType=VARCHAR},
</if>
<if test="role != null" >
<if test="role!= null" >
#{role,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
<if test="createTime!= null" >
now(),
</if>
<if test="updateTime != null" >
<if test="updateTime!= null" >
now(),
</if>
</trim>
</insert>
<!-- updateByPrimaryKeySelective 元素用于定义一个根据主键选择性更新的 SQL 语句,通过动态 SQL<set> 和 <if> 标签组合)来实现,
根据传入的 User 对象中各属性值是否为 null 来动态决定要更新哪些列的值,只更新那些不为 null 的属性对应的列,避免将非必要的字段更新为 null
从而更精准地对数据库表中的记录进行部分更新操作,提高数据更新的灵活性和准确性,更新操作是基于主键(通过 where 子句指定 id 值)来定位要更新的记录。 -->
<update id="updateByPrimaryKeySelective" parameterType="com.njupt.swg.entity.User" >
update mmall_user
<set >
<if test="username != null" >
<if test="username!= null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
<if test="password!= null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="email != null" >
<if test="email!= null" >
email = #{email,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
<if test="phone!= null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="question != null" >
<if test="question!= null" >
question = #{question,jdbcType=VARCHAR},
</if>
<if test="answer != null" >
<if test="answer!= null" >
answer = #{answer,jdbcType=VARCHAR},
</if>
<if test="role != null" >
<if test="role!= null" >
role = #{role,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
<if test="createTime!= null" >
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null" >
<if test="updateTime!= null" >
update_time = now(),
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<!-- updateByPrimaryKey 元素用于定义一个根据主键更新的 SQL 语句,它会将传入的 User 对象中的所有属性值更新到数据库表中对应主键的记录上,
无论属性值是否为 null都会进行更新操作相较于 updateByPrimaryKeySelective 更加简单直接,适用于需要完整更新记录所有字段的场景,
更新操作同样是基于主键(通过 where 子句指定 id 值)来定位要更新的记录。 -->
<update id="updateByPrimaryKey" parameterType="com.njupt.swg.entity.User" >
update mmall_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
question = #{question,jdbcType=VARCHAR},
answer = #{answer,jdbcType=VARCHAR},
role = #{role,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
question = #{question,jdbcType=VARCHAR},
answer = #{answer,jdbcType=VARCHAR},
role = #{role,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
where id = #{id,jdbcType=INTEGER}
</update>
<!-- select 元素用于定义一个查询 SQL 语句根据传入的用户名parameterType="string"来查询数据库表mmall_user中同名的记录数量
通过 count(1) 函数统计满足条件的记录数结果类型resultType为 int表示返回的是一个整数类型的记录数量用于判断用户名在表中是否存在等业务场景。 -->
<select id="selectByUsername" resultType="int" parameterType="string" >
select
count(1)
count(1)
from mmall_user
where username = #{username}
</select>
<!-- 与上面的 selectByUsername 类似,此 select 元素用于根据传入的邮箱地址parameterType="string"来查询数据库表mmall_user中同邮箱的记录数量
同样通过 count(1) 函数统计记录数,结果类型为 int用于判断邮箱在表中是否存在等相关业务逻辑判断场景。 -->
<select id="selectByEmail" resultType="int" parameterType="string" >
select
count(1)
count(1)
from mmall_user
where email = #{email}
</select>
<!-- select 元素用于根据用户名和密码(通过 #{username} 和 #{password} 传入参数来查询数据库表mmall_user中的记录
并使用前面定义的 "BaseResultMap" 来映射查询结果到 User 对象,适用于用户登录验证等场景,通过传入的用户名和密码查找对应的用户记录信息。 -->
<select id="selectByUsernameAndPasswd" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
@ -175,6 +219,8 @@
and password = #{password}
</select>
<!-- select 元素用于根据用户名parameterType="string"来查询数据库表mmall_user中的记录
并通过 "BaseResultMap" 映射查询结果到 User 对象,用于根据用户名获取对应的用户详细信息等业务场景。 -->
<select id="getUserByUsername" resultMap="BaseResultMap" parameterType="string" >
select
<include refid="Base_Column_List"/>
@ -182,7 +228,8 @@
where username = #{username}
</select>
<!-- select 元素用于根据用户名、问题和答案(通过 #{username}、#{question} 和 #{answer} 传入参数来查询数据库表mmall_user中的记录
同样使用 "BaseResultMap" 映射查询结果到 User 对象,常用于找回密码等业务场景中,根据用户输入的相关信息查找对应的用户记录。 -->
<select id="getUserByUsernameQuestionAnswer" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"/>
@ -192,11 +239,13 @@
and answer = #{answer}
</select>
<!-- select 元素用于根据传入的邮箱地址email以及排除指定的用户 iduserId来查询数据库表mmall_user中满足条件的记录数量
通过 count(1) 函数统计记录数,结果类型为 int常用于验证邮箱是否被其他用户使用除了当前指定用户外等业务逻辑场景。 -->
<select id="checkEmailValid" resultType="int">
select
count(1)
count(1)
from mmall_user
where email = #{email}
and id != #{userId}
and id!= #{userId}
</select>
</mapper>
Loading…
Cancel
Save