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

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>
    @Test
    void 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 articleinfo
        where id in
        <foreach collection="idList" item="aid" open="(" close=")" separator=",">
            #{aid}
        </foreach>
    </delete>
@Transactional
    @Test
    void 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. 帮助在打包发现一些问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值