Giter Site home page Giter Site logo

mybatis-plus's Introduction

mybatis-plus

mybatis-plus是什么

是否已经厌倦了mybatis-generator? 每当表结构需要变更时候就要进行mapper.xml重新生成, 如果存在自定义当sql的话那就更加痛苦了。 mybatis-plus就是用来代替mybatis-generator的一种方案,实现了通用的insert/update 语句生成功能, 降低表结构变更带来的影响。

quickstart

  1. 导入依赖
<dependency>
  <groupId>com.majian.mybatis</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>
  1. 使用
  • 在数据类中添加对应注解
    • @Table映射表名
    • @Column映射列名,不建议使用(未对select语句进行处理)。不使用时默认字段名转为下划线。
    • @Transient 表示该字段不需要持久化,生成insert,update语句时忽略该字段。
    • @Version 表示乐观锁版本号,生成update语句时会 进行 set version_column=version_column+1 ... where version_column =#{versionField}的 处理
    • @Id表示主键, update时会以此定位数据。
 @Table("user")
 public class User {
      @Id
      private Integer id;
      private Integer age;
      @Column("real_name")
      private Integer realName;
      @Version
      private Integer version;
      @Transient
      private Date updateTime;
  }
  • 继承通用mapper, 可使用 insert/update 方法
  public interface UserMapper extends CommonMapper<User> {

  }
 
  public class UserDao {
      private UserMapper userMapper;
 
      public int insert(User user) {
          return userMapper.insert(user);
      }
 
  }
  • 默认主键生成方式为@Options(useGeneratedKeys = true, keyProperty="id"),可通过@Overeride覆盖默认的主键生成策略,此时需要加上@InsertProvider(type = CommonProvider.class, method = "genInsertSQL")
public interface UserMapper extends CommonMapper<User> {
    @InsertProvider(type = CommonProvider.class, method = "genInsertSQL")
    @Options(useGeneratedKeys = true, keyProperty="xx")
    @Override
    int insert(T record);
}
      

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.