mapper接口中常见的增删改查

前言

  相信大家在使用mybatis写mapper接口的时候,最常用且简单的方法就是增删改查了。我也是刚开始做项目,在本篇文章中,我将根据自己在vhr微人力项目中的mapper接口方法为实例,记录一下接口中常用的增删改查方法

1.insert

  1.1 insert的使用

    1.1.1 接口方法

    int insert(Nation record);

    1.1.2 SQL实现

  <insert id="insert" parameterType="com.ku.vhr.model.Nation">
        insert into vhr_project.nation
        (id, name) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
    </insert>
  1. SQL语句中的插入操作,我们使用<insert></insert>,id的名称必须于mapper中的插入方法名保持一致,不然会报错;
  2. parameterType是指你传入的参数的参数类型,在mapper接口中的insert方法中我传入的是Nation实体类对象,因此我的插入参数类型为我定义的Nation类
  3. 在数据库中插入数据语句格式:insert into `数据库名.表名` (`字段名1`, `字段名2`, ...) values (#{属性名1,jdbcType="", #{属性名2,jdbcType=""}, ...})
  4. 其中jdbcType的作用是防止在插入过程中因属性名为空而报错,在编写SQL语句的时候最好加上
  5. jdbcType的常用类型
复制代码
JDBC Type            Java Type
CHAR                String
VARCHAR                String
LONGVARCHAR            String
NUMERIC                java.math.BigDecimal
DECIMAL                java.math.BigDecimal
BIT                    boolean
BOOLEAN                boolean
TINYINT                byte
SMALLINT            short
INTEGER                int
BIGINT                long
REAL                float
FLOAT                double
DOUBLE                double
BINARY                byte[]
VARBINARY            byte[]
LONGVARBINARY                byte[]
DATE                java.sql.Date
TIME                java.sql.Time
TIMESTAMP            java.sql.Timestamp
CLOB                Clob
BLOB                Blob
ARRAY                Array
DISTINCT            mapping of underlying type
STRUCT                Struct
REF                            Ref
DATALINK            java.net.URL[color=red][/color]
jdbcType常用类型
复制代码

  1.2 insert测试

    1.2.1 测试代码

复制代码
 @Test
    public void insert(){
//        System.out.println(nationMapper);
        Nation nation = new Nation();
        nation.setId(55);
        nation.setName("珞巴族");
        int rows = nationMapper.insert(nation);
        System.out.println(rows);
    }
insertTest
复制代码

    1.2.2 测试结果

2.insertSelective

  2.1 insertSelective的使用

    2.1.1 接口方法

int insertSelective(Nation record);

    2.1.2 SQL实现

复制代码
<insert id="insertSelective" parameterType="com.ku.vhr.model.Nation">
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR}
            </if>
        </trim>
    </insert>
insertSelective
复制代码
  1. 在insertSelective方法中,是允许选择性插入的,这是insertSelective方法与insert方法的本质区别,且一般执行default,insert不执行default
  2. 在insertSelective方法中,标签<trim></trim>可以用于增删改以及条件判断,在该方法中的作用就是判断数据库字段以及属性是否为空,如果为空,插入报错
  3. prefix 是指在字段前面添加什么,suffix是指在字段插入之后加上什么,suffixOverrides是指最后去掉什么
  4. SQL插入语句格式为insert into `数据库名.表名` (`字段名1`, `字段名2`, ...) values (#{属性名1,jdbcType="", #{属性名2,jdbcType=""}, ...}),
  5. 从into开始就是用<trim></trim>标签来执行,所以第一个<trim></trim>标签prefix指"(",suffix指最后一个字段之后的")",suffixOverrides指去掉最后的","
  6. 第二个<trim></trim>标签prefix指"values (",suffix指最后一个字段之后的")",suffixOverrides指去掉最后的","
  7. <if test= "t条件判断"></if>标签,主要是用于条件判断;主要注意的是,条件中的变量必须是实体类的属性

  2.2 insertSelective测试

    2.2.1 测试代码

复制代码
    @Test
    public void insertSelective(){
        Nation nation = new Nation();
        nation.setId(56);
        nation.setName("基诺族");
        int rows = nationMapper.insertSelective(nation);
        System.out.println(rows);
    }
insertSelectiveTest
复制代码

    2.2.2 测试结果

 

3.deleteById

  3.1 deleteById的使用

    3.1.1 接口方法

int deleteById(Integer id);

    3.1.2 SQL实现

<delete id="deleteById" paramentType="java.lang.Integer">
    delete from vhr_project.nation
    where id = #{id,jdbcType=Integer}
</delete>
  1. 根据id删除的SQL语句格式为:delete from `数据名.表名` where id = #{id,jdbcType=Integer}

  3.2 deleteById测试

    3.2.1 测试方法

复制代码
@Test
    public void deleteById(){
        int rows = nationMapper.deleteById(57);
        System.out.println(rows);
    }
deleteByIdTest
复制代码

4.updateById

  4.1 updateById的使用

    4.1.1 接口方法

int updateById(Nation record);

    4.1.2 SQL实现

复制代码
<update id="updateById" parameterType="com.ku.vhr.model.Nation">
        update vhr_project.nation
        set name = #{name,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>
updateById
复制代码
  1. 修改的SQL语句格式:update `数据库名.表名` set `字段1` = #{属性1,jdbcType=""}, `字段2` = #{属性2,jdbcType=""},...
  2. 在updateById方法中,如果修改字段为空,那么数据库中会更新为null

  4.2 updateById测试

    4.2.1 测试方法

复制代码
@Test
    public void updateById(){
        Nation nation = new Nation();
        nation.setId(55);
        nation.setName("什么族");
        int rows = nationMapper.updateById(nation);
        System.out.println(rows);
    }
updateByIdTest
复制代码

    4.2.2 测试结果

 

5.updateByIdSelective

  5.1 updateByIdSelective的使用

    5.1.1 接口方法

int updateByIdSelective(Nation record);

    5.1.2 SQL实现

复制代码
    <update id="updateByIdSelective" parameterType="com.ku.vhr.model.Nation">
        update vhr_project.nation
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR}
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
updateByIdSelective
复制代码
  1. 在updateByIdSelective的SQL中使用<set></set>类似于<trim></trim>用于写条件判断,但是<set></set>只能用于修改SQL
  2. 在updateByIdSelective方法中,我们可以选择性地对字段进行修改,如果为空就忽略更新

5.2 updateByIdSelective测试

    5.2.1 测试方法

复制代码
    @Test
    public void updateByIdSelective(){
        Nation nation = new Nation();
        nation.setId(55);
        nation.setName("珞巴族");
        int rows = nationMapper.updateById(nation);
        System.out.println(rows);
    }
updateByIdSelectiveTest
复制代码

    5.2.2 测试结果

 

6.selectById

  6.1 selectById的使用

    6.1.1 接口方法

Nation selectById();

    6.1.2 SQL实现

复制代码
<select>
    select <include ref = "Base_Column_List"/>
   from `vhr_project.nation`
   where id = #{id,jdbcType=INTEGER}
</select>
selectById
复制代码
  1. 根据id查询数据的SQL语句格式:select * from `数据库名.表名` where `字段主键` = #{属性id,jdbcType=INTEGER}
  2. <include=ref="别名"/>是与<sql id = "别名">字段1, 字段2, 字段3, 字段4, 字段5</sql>一起联用的,
  3. <sql></sql>是用来提取重用的SQL片段,以此定义,多次使用,非常方便
  4. sql id = "别名"></sql>中的id是<include ref=""/>中的ref的值

  6.2 selectById测试

    6.2.1 测试方法

复制代码
@Test
    public void selectById(){
        Nation nation = nationMapper.selectById(2);
        System.out.println(nation);
    }
selectByIdTest
复制代码

    6.2.2 测试结果

 

7.getAll

  7.1 getAll的使用

    7.1.1 接口方法

List<Nation> getAllNations();

    7.1.2 SQL实现

<select id="getAllNations" resultMap="BaseResultMap">
        select * from vhr_project.nation
    </select>
getAll
  1. 获取各类的全部信息SQL格式:select * from `数据库名.表名`

  7.2 getAll测试

    7.2.1 测试方法

复制代码
@Test
    public void getAllNations(){
        List<Nation> allNations = nationMapper.getAllNations();
        for (Nation nation : allNations) {
            System.out.println(nation);
        }
    }
getAllTest
复制代码

    7.2.2 测试结果

 

8.参考链接

https://blog.csdn.net/jiankunking/article/details/52403300?ops_request_misc=&request_id=&biz_id=102&utm_term=SQL%E6%A0%87%E7%AD%BE%E6%98%AF%E7%94%A8%E6%9D%A5%E6%8A%BD%E5%8F%96%E5%8F%AF%E9%87%8D%E7%94%A8%E7%9A%84SQL%E7%89%87%E6%AE%B5%EF%BC%8C%E5%8D%95%E7%8B%AC%E5%AE%9A%E4%B9%89%EF%BC%8C%E6%96%B9%E4%BE%BF%E5%A4%9A%E6%AC%A1%E5%BA%94&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-52403300.142^v67^js_top,201^v4^add_ask,213^v2^t3_esquery_v2&spm=1018.2226.3001.4187

https://blog.csdn.net/good_good_xiu/article/details/122690774?ops_request_misc=&request_id=&biz_id=102&utm_term=prefix%EF%BC%9A%E6%8B%BC%E6%8E%A5%E5%90%8E%E7%BB%ADsql%E6%97%B6%E9%9C%80%E8%A6%81%E5%8A%A0%E4%B8%8A%E7%9A%84%E5%89%8D%E7%BC%80%20suffix%EF%BC%9A&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-122690774.142^v67^js_top,201^v4^add_ask,213^v2^t3_esquery_v2&spm=1018.2226.3001.4187

 

9.如果我写的有什么问题,请大家帮我指正!我会持续修改的

posted @   求知律己  阅读(1002)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示