🔎类中的属性与数据库的字段名不一致
数据表中的数据
查询数据
/** * 查询全部信息 * @author bibubibu * @date 2023/7/2 */ List<UserInfo> getAll();
<select id="getAll" resultType="com.example.demo.entity.UserInfo"> select * from userinfo </select>
分析结果发现类中的属性名与数据库的字段名不一致的均未被赋值
解决方法🍭
- resultMap
- 重命名
使用较多的是重命名
resultMap
使用resultMap
将数据库中的字段与类中的属性映射
使用resultMap
🍂
<select id="getAll" resultMap="bibubibu"> select * from userinfo </select>
问题🍂
并非所有的属性与字段名称都不一致, 需要全部进行映射么?
答
是的, 仅修改不一致的字段在单表查询时可能不会问题, 但多表查询时可能会出现 null
测试结果🍂
重命名
重命名, 更为简便的映射属性与字段
<select id="getAll" resultType="com.example.demo.entity.UserInfo"> select id, username as name, password as pwd, photo, createtime, updatetime, state from userinfo; </select>
测试结果🍂
🔎多表查询
多表查询的实现 → 联表查询(left join / right join / inner join) + xxxVO
举个栗子🌰
查询文章的作者(username)
此处查询的数据表为 userinfo(用户) 表,articleinfo(文章) 表
由于 articleinfo(文章) 表只有 uid(用户 id), 因此需要进行多表联查
下面为具体步骤
articleinfo 表与 userinfo 表🍂
articleinfo 表中的 uid 对应 userinfo 表中的 id
根据 articleinfo 表创建 ArticleInfo 实体类🍂
多表联查时返回数据需要包含文章作者(username)
因此扩展 articleinfo(文章) 表的实体类
扩展的实体类放入 objectview 包下, 继承 Articleinfo 类
在 Interface 中定义方法🍂
ArticleInfoVO getById(@Param("id") Integer id);
在 xml 中实现方法🍂
<select id="getById" resultType="com.example.demo.entity.viewobject.ArticleInfoVO"> select a.*, u.username from articleinfo as a left join userinfo as u on a.uid = u.id where a.id = #{id} </select>
单元测试验证效果🍂
发现只有 username 字段, 其他字段消失了
Debug 分析错误🍂
发现查询的结果是包含 username + 其他字段的
排查错误🍂
发现是 Lombok 重写 toString() 方法时的 BUG
重写 toString()🍂
单元测试验证效果🍂
另一种解决方法🍂
取消继承, 重写全部属性
注意事项🍂
实体类建议实现 Serializable 接口
目的是为了完成该类的序列化与反序列化操作
🔎动态 SQL 的使用
动态 SQL 是 MyBatis 的强⼤特性之⼀,能够完成不同条件下不同的 SQL 拼接
if 标签
在数据表中
一些字段必须填写(必填字段)
一些字段可以填写, 也可以不填写(非必填字段)
如果在编写 SQL 语句中有不确定的字段(非必填字段), 就需要使用 if 标签进行判断
语法🍂
<if test=""> </if>
举个栗子🌰
SQL 语句
insert into userinfo(username, password, photo) values('Lily', '6666', 'Lily.png');
xml 中实现
<insert id="add2"> insert into userinfo(username, password <if test="photo != null"> , photo </if> ) values(#{username}, #{password} <if test="photo != null"> , #{photo} </if> ) </insert>
trim 标签
如果所有字段都是非必填字段, 通常使用 trim 标签 + if 标签
trim 标签的属性🍂
- prefix, 表示整个代码块以 prefix 的值作为前缀
- suffix, 表示整个代码块以 suffix 的值作为后缀
- prefixOverrides, 表示整个代码块要去除的前缀
- suffixOverrides, 表示整个代码块要去除的后缀
语法🍂
<trim prefix="" suffix="" prefixOverrides="" suffixOverrides=""> </trim>
举个栗子🌰
SQL 语句
insert into userinfo(username, password, photo) values('Judy', 'Judy_pwd', 'Judy.png'); • 1
xml 中实现
<insert id="add3"> insert into userinfo <trim prefix="(" suffix=")" suffixOverrides=","> <if test="username != null"> username, </if> <if test="password != null"> password, </if> <if test="photo != null"> photo </if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> <if test="username != null"> #{username}, </if> <if test="password != null"> #{password}, </if> <if test="photo != null"> #{photo} </if> </trim> </insert>
where 标签
where 标签通常搭配 if 标签一起使用
where 标签的特点🍂
- where 标签会删除最前面的 and 关键字
- 如果 where 标签中没有内容(即 if 属性中的参数全部为 null), 就不会生成相对应的 where 语句
语法🍂
<where> </where>
举个栗子🌰
SQL 语句
select * from userinfo where username = 'Tom' and password = 'Tom_pwd';
xml 中实现
<select id="getByCondition" resultType="com.example.demo.entity.UserInfo"> select * from userinfo <where> <if test="username != null"> username = #{username} </if> <if test="password != null"> and password = #{password} </if> </where> </select>
利用 trim 标签实现 where 标签🍂
<select id="getByCondition" resultType="com.example.demo.entity.UserInfo"> select * from userinfo <trim prefix="where" prefixOverrides="and"> <if test="username != null"> username = #{username} </if> <if test="password != null"> and password = #{password} </if> </trim> </select>
set 标签
set 标签通常搭配 if 标签一起使用
set 标签的特点🍂
- set 标签会删除最后面的
,
语法🍂
<set> </set>
举个栗子🌰
SQL 语句
update userinfo set username = 'Jackson', password = 'Jackson_pwd', photo = 'Jackson.png' where id = 9;
xml 中实现
<update id="update"> update userinfo <set> <if test="username != null"> username = #{username}, </if> <if test="password != null"> password = #{password}, </if> <if test="photo != null"> photo = #{photo}, </if> </set> where id = #{id} </update>
利用 trim 标签实现 where 标签🍂
<update id="update"> update userinfo <trim prefix="set" suffixOverrides=","> <if test="username != null"> username = #{username}, </if> <if test="password != null"> password = #{password}, </if> <if test="photo != null"> photo = #{photo}, </if> </trim> where id = #{id} </update>
foreach 标签
foreach 标签通常用于遍历集合
foreach 标签常用属性🍂
- collection, 绑定方法中的参数, 如 List, Set, Map 或数组对象
- item, 遍历时的每一个对象
- open, 代码块开头的字符串
- close, 代码块结束的字符串
- separator, 每次遍历之间间隔的字符串
语法🍂
<foreach collection="" open="" close="" item="" separator=""> </foreach>
举个栗子🌰
SQL 语句
delete from userinfo where id in(1, 2, 3);
xml 中实现
<delete id="dels"> delete from userinfo where id in <foreach collection="ids" open="(" close=")" item="val" separator=","> #{val} </foreach> </delete>
对应关系🍂
🔎🌸🌸🌸完结撒花🌸🌸🌸