mybatis-plus入门CRUD/日志/自动填充/乐观锁

入门

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-common</artifactId>
    <version>2.9.2</version>
</dependency>

application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver

实体类

User.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
    @TableId(type = IdType.AUTO)//自增,注意数据库也需要设置自增
    private int id;
    @ApiModelProperty(value = "姓名",name = "name",dataType = "String",required = true)//解释,名字,类型,是否可为空
    private String name;
    private String pwd;
}

dao层Mapper接口

UserMapper.java

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {


}

启动类下添加扫描器

@SpringBootApplication
@MapperScan("com.shuo.dao")
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
}

测试类

@SpringBootTest
class MybatisPlusApplicationTests {

    @Autowired
    UserMapper userMapper;

        //查
    @Test
    void contextLoads() {
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user.toString());
        }
    }
        //增
    @Test
    public void testInsert(){
        /*User user = new User(12, "小胖", "123456");*/
        User user=new User();
        user.setName("冉冉");
        user.setPwd("789456");
        userMapper.insert(user);
        //contextLoads();

    }
//改
    @Test
    public void updatetest(){
        contextLoads();
        User user = new User();
        user.setId(8);
        user.setName("李明");
        user.setPwd("789798");
        userMapper.updateById(user);
        contextLoads();
    }
}

日志自带的

application.yml

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

自动填充

添加插入的时间,改变的时间

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
    @TableId(type = IdType.AUTO)//自增
    private int id;
    @ApiModelProperty(value = "姓名",name = "name",dataType = "String",required = true)//解释,名字,类型,是否可为空
    private String name;
    private String pwd;

   // @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

handler

MyMetaObjectHandler.java

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("start insert fill.....");
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill.....");
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}

乐观锁

实体类新增字段version

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
    @TableId(type = IdType.AUTO)//自增
    private int id;
    @ApiModelProperty(value = "姓名",name = "name",dataType = "String",required = true)//解释,名字,类型,是否可为空
    private String name;
    private String pwd;

   // @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;


    @TableField(value="version")
    @Version
    private Integer version;
}

编写config配置文件

MybatisPlusConfig.java

@EnableTransactionManagement
@Configuration
@MapperScan("com.shuo.dao")将扫描器可转移到此
public class MybatisPlusConfig {
    @Bean
    public OptimisticLockerInterceptor OptimisticLockerInnerInterceptor() {
        return new OptimisticLockerInterceptor();//名字容易出错
    }
}

乐观锁的测试

//测试乐观锁
@Test
public void testOptimisticLocker(){
    User user=userMapper.selectById(1L);
    user.setName("小白菜");
    user.setPwd("cike567");
    System.out.println(user);
    userMapper.updateById(user);
}
//乐观锁失败   多线程下失败情况
@Test
public void testOptimisticLocker2(){
    User user=userMapper.selectById(1L);
    user.setName("小土豆");
    user.setPwd("cike567");
    System.out.println(user);
    User user1 = userMapper.selectById(1L);
    user1.setName("小豆芽");
    userMapper.updateById(user1);
    userMapper.updateById(user);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值