mybatis-plus系统化学习之更新-AR-主键-service

1.背景

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

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("主键id=" + 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("主键id=" + 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

b.建立实体对象

package com.ldp.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 12/07 11:57
 * @description
 */
@Data
@Accessors(chain = true)
public class Product extends Model<Product> {
    private Integer id;
    @TableField(value = "`name`")
    private String name;
    private String productNo;
    @TableField(value = "`describe`")
    private String describe;
    private Date createTime;
    private Date updateTime;
}
View Code

c.建立mapper接口

package com.ldp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ldp.entity.Product;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 11/06 8:54
 * @description
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {

}
View Code

d.使用

package com.ldp.demo01;

import com.ldp.entity.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
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>
 * 一般不使用这个方式
 * </p>
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test07AR {
    /**
     * 增加
     * ==>  Preparing: INSERT INTO product ( name, product_no ) VALUES ( ?, ? )
     * ==> Parameters: 苹果(String), P001(String)
     * <==    Updates: 1
     */
    @Test
    public void test01() {
        Product product = new Product().setName("苹果").setProductNo("P001");
        product.insert();
    }

    /**
     * 删除
     * <p>
     * ==>  Preparing: DELETE FROM product WHERE id=?
     * ==> Parameters: 1(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test02() {
        Product product = new Product().setId(1);
        product.deleteById();
    }

    /**
     * 修改
     * <p>
     * ==>  Preparing: UPDATE product SET name=? WHERE id=?
     * ==> Parameters: 苹果-修改(String), 2(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test03() {
        Product product = new Product().setId(2).setName("苹果-修改");
        product.updateById();
    }

    /**
     * 查询
     * ==>  Preparing: SELECT id,`name`,product_no,`describe`,create_time,update_time FROM product WHERE id=?
     * ==> Parameters: 2(Integer)
     */
    @Test
    public void test04() {
        Product product = new Product().setId(2);
        Product selectProduct = product.selectById();
        System.out.println("selectProduct=" + selectProduct);
    }
}
View Code

5.service

a.建立ISysUserService接口

package com.ldp.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.ldp.entity.SysUser;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 12/07 4:31
 * @description
 */
public interface ISysUserService  extends IService<SysUser> {
}

b.建立SysUserServiceImpl实现

package com.ldp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldp.entity.SysUser;
import com.ldp.mapper.SysUserMapper;
import com.ldp.service.ISysUserService;
import org.springframework.stereotype.Service;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 12/07 4:31
 * @description
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
}

c.使用

package com.ldp.demo01;

import com.ldp.entity.SysUser;
import com.ldp.service.ISysUserService;
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>
 * 配置:
 * https://baomidou.com/config/
 * </p>
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test10Service {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 增加
     * ==>  Preparing: INSERT INTO sys_user ( name, we_chat ) VALUES ( ?, ? )
     * ==> Parameters: 李东平4(String), 851298348(String)
     * <==    Updates: 1
     */
    @Test
    public void test01() {
        SysUser sysUser = new SysUser().setName("李东平4").setWeChat("851298348");
        boolean save = sysUserService.save(sysUser);
        System.out.println("save:" + save);
        System.out.println("主键id=" + sysUser.getId());
    }

    /**
     * 删除
     * ==>  Preparing: DELETE FROM sys_user WHERE id=?
     * ==> Parameters: 27(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test02() {
        boolean remove = sysUserService.removeById(27);
        System.out.println("remove:" + remove);
    }

    /**
     * 修改
     * ==>  Preparing: UPDATE sys_user SET name=? WHERE id=?
     * ==> Parameters: 李东平-修改3(String), 26(Integer)
     * <==    Updates: 1
     */
    @Test
    public void test03() {
        SysUser sysUser = new SysUser().setId(26).setName("李东平-修改3");
        boolean update = sysUserService.updateById(sysUser);
        System.out.println("update:" + update);
    }

    /**
     * 查询
     * ==>  Preparing: SELECT id,version,age,gender,name,parent_id,position,account,we_chat,password,status,type,create_time,update_time FROM sys_user WHERE id=?
     * ==> Parameters: 26(Integer)
     */
    @Test
    public void test04() {
        SysUser user = sysUserService.getById(26);
        System.out.println("user:" + user);
    }

}
View Code

mybatis-plus系统化学习教程:https://www.cnblogs.com/newAndHui/p/14141950.html

完美!

posted @ 2020-12-07 19:08  李东平|一线码农  阅读(366)  评论(0编辑  收藏  举报