本节要么很简单,要么不重要,知道就可以了.

2.更新

package com.ldp.demo01;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ldp.entity.SysUser;
import com.ldp.mapper.SysUserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 11/06 10:27
 * @description <p>
 *
 * </p>
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test06Update {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据 ID 修改(建议使用这个方式修改)
     * 需求:
     * 将id为20用户的年龄改为102
     * <p>
     * ==>  Preparing: UPDATE sys_user SET age=? WHERE id=?
     * ==> Parameters: 102(Integer), 20(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test1() {
        int rows = sysUserMapper.updateById(new SysUser().setId(20).setAge(102));
        System.out.println("受影响行数:" + rows);
    }

    /**
     * 根据 whereEntity 条件,更新记录
     * 需求:
     * 将id为20并且年龄为102的 用户的年龄改为100
     * <p>
     * ==>  Preparing: UPDATE sys_user SET age=? WHERE (id = ? AND age = ?)
     * ==> Parameters: 100(Integer), 20(String), 102(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test2() {
        // 作为更新的条件
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", "20").eq("age", 102);

        // 修改的字段
        SysUser sysUser = new SysUser().setAge(100);

        int rows = sysUserMapper.update(sysUser, updateWrapper);
        System.out.println("受影响行数:" + rows);
    }

    /**
     * 根据 whereEntity 条件,更新记录(简写)
     * 需求:
     * 将id为20并且年龄为100的 用户的年龄改为200
     * <p>
     * ==>  Preparing: UPDATE sys_user SET age=? WHERE (id = ? AND age = ?)
     * ==> Parameters: 200(Integer), 20(String), 100(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test3() {
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        // 作为更新的条件
        updateWrapper.eq("id", "20").eq("age", 100)
                // 修改的字段
                .set("age", 200);

        int rows = sysUserMapper.update(null, updateWrapper);
        System.out.println("受影响行数:" + rows);
    }
}

3.主键

1.实体配置

@Data
@Accessors(chain = true)
//@KeySequence(value = "SEQ_ORACLE_STRING_KEY", clazz = String.class) // oracle数据库使用序列生成主键
public class SysUser {

    @TableId(type = IdType.AUTO)
    //@TableId(value = "ID", type = IdType.INPUT) // oracle数据库使用序列生成主键
    private Integer id;
}

2.测试代码

package com.ldp.demo01;

import com.ldp.entity.SysUser;
import com.ldp.mapper.SysUserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 12/07 12:23
 * @description <p>
 * 主键策略
 * </p>
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test08Sequence {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * mysql数据库自动生成id
     * ==>  Preparing: INSERT INTO sys_user ( age, name ) VALUES ( ?, ? )
     * ==> Parameters: 18(Integer), 李东平(String)
     * <==    Updates: 1
     */
    @Test
    public void test01() {
        SysUser sysUser = new SysUser().setName("李东平").setAge(18);
        int rows = sysUserMapper.insert(sysUser);
        System.out.println("受影响行数:" + rows);
        System.out.println("主键> sysUser.getId());
    }
    /**
     * oracle数据库 一般使用序列生成(受环境影响这里不演示这是给出代码)
     * 1.对象上加序列@KeySequence(value = "SEQ_ORACLE_STRING_KEY", clazz = Integer.class)
     * 2.id上加id生成策略  @TableId(value = "ID", type = IdType.INPUT)
     * ==>  Preparing: INSERT INTO sys_user ( age, name ) VALUES ( ?, ? )
     * ==> Parameters: 18(Integer), 李东平(String)
     * <==    Updates: 1
     */
    @Test
    public void test02() {
        SysUser sysUser = new SysUser().setName("李东平2").setAge(19);
        int rows = sysUserMapper.insert(sysUser);
        System.out.println("受影响行数:" + rows);
        System.out.println("主键> sysUser.getId());
    }
}

4.AR

实际生产中一般不这样使用

a.建表product

CREATE TABLE `product` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL COMMENT '产品名称',
  `price` double(10,2) DEFAULT NULL COMMENT '卖价',
  `product_no` varchar(32) DEFAULT NULL COMMENT '产品编号',
  `describe` varchar(255) DEFAULT NULL COMMENT '产品描述',
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2021-07-25
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-31
  • 2021-11-22
  • 2021-09-13
  • 2021-09-09
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案