当前位置: 首页 > news >正文

MyBatis操作数据库常见用法总结2

文章目录

      • 1.动态SQL使用
        • 什么是动态sql
        • 为什么用动态sql
        • 标签拼接
        • 标签拼接
        • 标签拼接
        • 标签拼接
        • 标签拼接
      • 补充1:resultType和resultMap
      • 补充2:后端开发中单元测试工具使用(Junit框架)

1.动态SQL使用

以insert标签为例

什么是动态sql

是mybatis的特性之一,能在xml里边写逻辑判断(if else for循环)

为什么用动态sql

对于非必传参数,如果传了就做xx处理,如果没传,就给默认值。
常用的就5个,掌握这些即可。

标签拼接

作用:判断。例如,注册分为必填项和非必填项,如果有值就拼接上去,如果没值就不拼接。
使用:test里边判断的key是属性,不是数据库中字段,还有{}里边的

int addUser(UserEntity user);
<insert id="addUser2">insert into userinfo(username,password<if test="photo!=null and photo!=''">,photo</if>)values(#{username},#{password}<if test="photo!=null and photo!=''">,#{photo}</if>)
</insert>
@Test
void addUser2(){String username="liliu";String password="123456";UserEntity user=new UserEntity();user.setUsername(username);user.setPassword(password);int result=userMapper.addUser2(user);System.out.println(result);
}

标签拼接

作用:去除前置空格和结尾空格
使用:有四个属性
prefix:表示整个语句块以prefix值作为前缀
suffix:表示整个语句块,以suffix的值作为后缀
prefixOverrides:表示整个语句块要去除的前缀
suffixOverrides:表示整个语句块要去除的后缀

表示语句必须以(开始,)结束,去除最后的,

<insert id="addUser3">insert into userinfo<trim prefix="(" suffix=")" suffixOverrides=","><if test="username!=null and username!=''">username,</if><if test="img!=null and img!=''">photo,</if><if test="pwd!=null and pwd!=''">password,</if></trim>values<trim prefix="(" suffix=")" suffixOverrides=","><if test="username!=null and username!=''">#{username},</if><if test="img!=null and img!=''">#{img},</if><if test="pwd!=null and pwd!=''">#{pwd},</if></trim></insert>
@Transactional
@Test
void addUser3() {String username = "liliu";String password = "123456";UserEntity user = new UserEntity();user.setUsername(username);user.setPwd(password);
//        user.setImg("cat.png");int result = userMapper.addUser3(user);System.out.println("添加:" + result);}

标签拼接

作用:条件查询的时候进行使用
场景:进行列表页的查询,多个非必传参数的处理,解决方案之where标签
用法:会自动去除and前缀,不会自动去除and后缀,有条件自动加上where条件,没有则不加

List<ArticleInfoVO> getListByIdOrTitle(@Param("id") Integer id,@Param("title") String title);
<select id="getListByIdOrTitle" resultType="com.example.demo.entity.vo.ArticleInfoVO">select * from articleinfo<where><if test="id!=null and id>0">id=#{id}</if><if test="title!=null and title!=''">and title like concat('%',#{title},'%')</if></where></select>
    @Testvoid getListByIdOrTitle() {List<ArticleInfoVO> list = articleMapper.getListByIdOrTitle(1, null);System.out.println(list.size());}

标签拼接

作用:进行就修改的时候,去掉最后的逗号,有修改内容就生成set,否则不加
场景:例如修改用户昵称

int updateById(User user);
<update id="updateById" parameterType="org.example.model.User">update user<set><if test="username != null">username=#{username},</if><if test="password != null">password=#{password},</if><if test="nickname != null">nickname=#{nickname},</if><if test="sex != null">sex=#{sex},</if><if test="birthday != null">birthday=#{birthday},</if><if test="head != null">head=#{head},</if><if test="createTime != null">create_time=#{createTime},</if></set>where id=#{id}
</update>

可以在controller层进行控制,必须要有对应的参数不为空,既然来了就必须要改

标签拼接

作用:对集合进行遍历
用法:属性
collection:绑定方法参数的集合
item:遍历时开始的字符串
open:语句块开始的字符串
close:语句块结束的字符串
separator:每次遍历之间间隔的字符串
加载需要循环的部分上边
场景:批量删除指定id的文章

// 根据文章id集合批量删除文章int delByIdList(List<Integer> idList);
<delete id="delByIdList"><!--        where id in(1,2..)-->delete from articleinfowhere id in<foreach collection="idList" item="aid" open="(" close=")" separator=",">#{aid}</foreach></delete>
@Transactional@Testvoid delByIdList() {List<Integer> idList = new ArrayList<>();idList.add(1);idList.add(2);idList.add(3);int result = articleMapper.delByIdList(idList);System.out.println("删除条数:" + result);}

补充1:resultType和resultMap

使用场景:数据库表名和实体类中的名字不同,框架不能再自动映射。
需要手动写,自己做映射。映射表和实体类,字段名和属性。(前边是实体类后边是数据库表)
在这里插入图片描述
对应的标签resultType改成resultMap
一个xml中可以有多个resultMap

补充2:后端开发中单元测试工具使用(Junit框架)

在使用之前需要在springboot项目中添加junit框架
但是一般来说,springboot项目加载后,这个依赖自动就拉去了。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wOS2oVOo-1691473708864)(F:\typora插图\image-20230515164236370.png)]

  1. 在需要单元测试的类中,右键Test(接口中),Junit5(springboot内置的5)
  2. 添加单元测试代码
    • 在测试类上边加上@SpringBootTest。将对象交给Spring管理,是在springboot项目中进行测试的
    • 在测试方法上加上@Transactional,不污染数据库。默认还是污染,所以切记在增删改操作测试时加上此注解,可以回滚事务,不污染数据库。他可以修饰类,也可以修饰方法
    • @Transactional实现原理,方法执行前开启事务,结束后回滚事务 roll back。
    • 注入属性,调用对应测试的方法传输响应数据进行测试
    • 另外,添加测试代码和新建测试类区别在于会有一个追加的提示框,确认即可

具体使用方法在上边的例子中演示。
开发中只是看一个结果,具体测试会交给测试人员

特点说明:

  1. 简单直观快速测试某功能是否正确
  2. 跳过用户校验直接测试代码。可以每次只执行一个方法
  3. 可以在不污染数据库的前提下测试功能
    过用户校验直接测试代码。可以每次只执行一个方法
  4. 可以在不污染数据库的前提下测试功能
  5. 帮助在打包发现一些问题
http://www.lryc.cn/news/118206.html

相关文章:

  • 基于SpringBoot+LayUI的宿舍管理系统 001
  • C语言笔记7
  • Centos更换网卡名称为eth0
  • 【Express.js】软件测试
  • TCP三次握手、四次握手过程,以及原因分析
  • OceanBase X Flink 基于原生分布式数据库构建实时计算解决方案
  • 600V EasyPIM™ IGBT模块FB30R06W1E3、FB20R06W1E3B11、FB20R06W1E3降低了系统成本和损耗,可满足高能效要求。
  • form 表单恢复初始数据
  • MySQL—索引
  • Android图形-合成与显示-概论
  • Swift 5 数组如何获取集合的索引和对应的元素值
  • 计算 Nginx 日志的PV和UV
  • Spring中常用的注解
  • Plugin 插件
  • Structure needs cleaning fsimage文件系统损坏修复
  • MATLAB|信号处理的Simulink搭建与研究
  • LinuxC编程——线程
  • 使用fetch调用fastapi接口(post)的实例
  • 探索规律:Python地图数据可视化艺术
  • Django-------自定义命令
  • 【Linux】在浏览器输入网址后发生了什么事情?
  • 推荐两本书《JavaRoadmap》、《JustCC》
  • 使用基于jvm-sandbox的对三层嵌套类型的改造
  • [HDLBits] Mt2015 q4b
  • C++:堆排序
  • Grafana Prometheus 通过JMX监控kafka
  • vue项目切换页面白屏不显示解决方案
  • Goland报错 : Try to open it externally to fix format problem
  • Python-OpenCV中的图像处理-几何变换
  • 前端JavaScript入门-day08-正则表达式