MyBatis—操作数据库(四)

简介: MyBatis—操作数据库(四)

🔎类中的属性与数据库的字段名不一致


数据表中的数据

查询数据

/**
* 查询全部信息
* @author bibubibu
* @date 2023/7/2
*/
List<UserInfo> getAll();
<select id="getAll" resultType="com.example.demo.entity.UserInfo">
    select * from userinfo
</select>

分析结果发现类中的属性名与数据库的字段名不一致的均未被赋值

解决方法🍭

  1. resultMap
  2. 重命名

使用较多的是重命名

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 拼接

官方对于动态 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>

对应关系🍂

🔎🌸🌸🌸完结撒花🌸🌸🌸


相关文章
|
JavaScript 关系型数据库 MySQL
❤Nodejs 第六章(操作本地数据库前置知识优化)
【4月更文挑战第6天】本文介绍了Node.js操作本地数据库的前置配置和优化,包括处理接口跨域的CORS中间件,以及解析请求数据的body-parser、cookie-parser和multer。还讲解了与MySQL数据库交互的两种方式:`createPool`(适用于高并发,通过连接池管理连接)和`createConnection`(适用于低负载)。
21 0
|
2月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
16 0
|
1天前
|
XML Java 数据库连接
Springboot整合mybatisPlus操作数据库
MyBatis-Plus是MyBatis的增强工具,简化开发、提高效率。它提供官网文档,便于集成到SpringBoot项目中。集成步骤包括添加mybatis-plus-boot-starter和数据库驱动依赖,配置数据源,扫描Mapper类包。Mapper接口继承BaseMapper即可使用基本的CRUD操作。示例代码展示了Service层的增删改查实现。MyBatisPlus还支持逻辑删除、自动填充等功能,同时可与Mybatis XML配合使用,通过调整配置指定XML映射文件位置。
|
1天前
|
关系型数据库 MySQL Go
数据库的事务操作
数据库的事务操作
|
1天前
|
关系型数据库 MySQL Go
数据库的事务操作 | 青训营
数据库的事务操作 | 青训营
|
6天前
|
算法 数据库
Mybatis-Plus实现常规增删改操作
Mybatis-Plus实现常规增删改操作
|
9天前
|
SQL Java 数据库连接
Javaweb之Mybatis的基础操作之新增和更新操作的详细解析
Javaweb之Mybatis的基础操作之新增和更新操作的详细解析
10 0
|
22天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
1月前
|
数据库
MybatisPlus属性字段为数据库关键字
MybatisPlus属性字段为数据库关键字
14 0
|
2月前
|
缓存 NoSQL 数据库
[Redis]——数据一致性,先操作数据库,还是先更新缓存?
[Redis]——数据一致性,先操作数据库,还是先更新缓存?