Mybatis 动态SQL的插入操作

需求 : 根据用户的输入情况进行插入

动态SQL:根据需求动态拼接SQL

用户往表中插入数据,有的数据可能不想插入,比如不想让别人知道自己的性别,性别就为空

insert into userinfo(username,password,age,gender,phone) values(?,?,?,?,?);
insert into userinfo(username,password,age,gender) values(?,?,?,?);
insert into userinfo(username,password,age,phone) values(?,?,?,?);

接下来看看 mybatis 注解的方式该如何实现动态SQL,新建了一个 userInfo2Mapper 接口

<if>标签里面的意思是 : 如果gener不为null,那就输出if标签的内容

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfo2Mapper {
    @Insert("<script>" +
            " insert into userinfo(username,password,age," +
            "<if test='gender!=null'>gender,</if>" +
            "phone) " +
            "value(#{username},#{password},#{age}," +
            "<if test='gender!=null'>#{gender},</if>" +
            "#{phone})" +
            "</script>"
    )
    Integer insert(UserInfo userInfo);
}

 然后老样子,Generate,test,勾选 insert ,然后补充代码,我们先每个数据都插入内容

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class UserInfo2MapperTest {
    @Autowired
    private UserInfo2Mapper userInfo2Mapper;
    @Test
    void insert() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("kiki");
        userInfo.setPassword("555www");
        userInfo.setAge(23);
        userInfo.setGender(1);
        userInfo.setPhone("177966");
        userInfo2Mapper.insert(userInfo);
    }
}

插入成功 

数据库中也能成功找到刚刚插入的数据 

接下来我们要测试性别为空的情况,把 test代码里面的 userinfo.setGender 给去掉, 再次运行

 数据库也能找到,说明性别为空也插入成功了

上面是注解的方式,接下来我们看看 XML 的方式该如何实现 

在resources 中创建 Userinfo2XMLMapper.xml 文件

然后在 userInfo2Mapper 接口 中声明这个方法

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfo2Mapper {

    Integer insertByXML(UserInfo userInfo);
}

将 Userinfo2XMLMapper.xml 文件中的 namespace 进行修改,改为 userInfo2Mapper 接口中的第一行 package 的内容再加上接口名

<?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">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfo2Mapper">

</mapper>

然后补充代码

<?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">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfo2Mapper">
    <insert id="insertByXML">
        insert into userinfo(username,password,age,
        <if test="gender!=null">
            gender,
        </if>
        phone)
        values(#{username},#{password},#{age},
        <if test="gender!=null">
            #{gender},
        </if>
        #{phone})
    </insert>
</mapper>

再回到接口,然后Generate,test,勾选insertByXML,ok,先测试每个数据都插入的情况

package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class UserInfo2MapperTest {
    @Autowired
    private UserInfo2Mapper userInfo2Mapper;

    @Test
    void insertByXML() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("io");
        userInfo.setPassword("555www");
        userInfo.setAge(23);
        userInfo.setGender(1);
        userInfo.setPhone("177966");
        userInfo2Mapper.insertByXML(userInfo);
    }
}

成功插入

再把 userinfo.setGender 给去掉,再次运行

 

 没毛病

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值