MyBatis-Pro,新一代的MyBatis增強框架

地址

框架功能

  • 內置提供基礎CRUD方法
  • 提供根據方法名自進行單表查詢(包括查詢、統計、刪除等)

接入方法

Spring Boot
    <dependency>
        <groupId>com.github.dreamroute</groupId>
        <artifactId>mybatis-pro-boot-starter</artifactId>
        <version>latest version</version>
    </dependency>
Spring MVC

內置方法

1. 你的Mapper接口繼承com.github.dream.mybatis.pro.sdk.Mapper接口
2. 在啓動類上使用@MapperScan註解指明你的Mapper接口路徑
3. 此時你的接口就擁有了Mapper接口的所有通用方法,如下:
    T selectById(ID id);                       // 根據主鍵id查詢單個對象
    List<T> selectByIds(List<ID> ids);         // 根據主鍵id集合查詢多個對象
    List<T> selectAll();                       // 查詢全部

    int insert(T entity);                      // 新增
    int insertExcludeNull(T entity);           // 新增,值爲null的屬性不進行保存,使用數據庫默認值
    int insertList(List<T> entityList);        // 批量新增

    int updateById(T entity);                 // 根據主鍵id修改
    int updateByIdExcludeNull(T entity);      // 根據主鍵id修改,值爲null的屬性不進行修改

    int deleteById(ID id);                    // 根據id刪除
    int deleteByIds(List<ID> ids);            // 根據id列表進行刪除

實體對象註解

@Data
@Table(name = "smart_user")
public class User {

    @Id
    private Long id;
    private String name;
    private String password;
    private Long version;
    @Transient
    private Integer gender;
    @Column(name = "phone_no")
    private String phoneNo;
}

說明:
  • @com.github.dreamroute.mybatis.pro.core.annotations.Table:name屬性表示表名
  • @com.github.dreamroute.mybatis.pro.core.annotations.Id:標記的字段表示主鍵(默認爲自增,可根據@Id的屬性type屬性修改主鍵策略
  • @com.github.dreamroute.mybatis.pro.core.annotations.Transient:表示此字段無序持久化到數據庫
  • @com.github.dreamroute.mybatis.pro.core.annotations.Column:實體屬性與數據列的映射關係(默認:駝峯屬性自動轉成下劃線風格)

靈魂功能

1、Mapper接口的方法名根據特定的書寫規則進行查詢,用戶無需編寫sql語句

2、方法名以findBy、countBy、existBy、deleteBy開頭,屬性首字母大寫,多個屬性使用And或者Or連接

比如:

public interface UserMapper extends Mapper<User, Long> {

    // select * from xx where name = #{name} and password = #{password}
    User findByNameAndPassword(String name, String password);

    // select count(*) c from xx where name = #{name}
    int countByName(String name);

    // select * from xx where name = #{name} and password like '%#{password}%'
    List<User> findByNameAndPasswordLike(String name, String password);

    // delete from xx where name = #{name} and version = #{version}
    int deleteByNameAndVersion(String name, Long version);

}

全部功能

一個方法可以有多個and或者or拼接多個條件,

如:findByNameLikeOrPasswordIsNotNullAndVersion(String name, String password, version)

效果:where name like '%#{name}%' or password is not null and version #{version}

關鍵字 示例 效果
and findByNameAndPassword(String name, String password) where name = #{name} and #{password}
or findByNameOrPassword(String name, String password) where name = #{name} or #{password}
count countByName(String name) select count(*) c from xx where name = #{name}
exist existByName(String name) 查詢結果大於等於1,那麼返回true,否則返回false
delete deleteByName(String name) delete from x where name = #{name}
Between findByAge(Integer start, Integer end ) where age between #{start} and #{end}
LT(LessThan) findByAgeLT(Integer age) where age < #{age}
LTE(LessThanEqual) findByAgeLTE(Integer age) where age <= #{age}
GT(GreaterThan) findByAgeGT(Integer age) where age > #{age}
GTE(GreaterThanEqual) findByAgeLTE(Integer age) where age >= #{age}
IsNull findByNameIsNull where name is null
IsNotNull findByNameIsNotNull where name is not null
IsBlank findByNameIsBlank where name is null or name = ''
IsNotBlank findByNameIsNotBlank where name is not null and name != ''
Like findByNameAndPasswordLike(String name, String password) where name = #{name} and password like '%#{password}%'
NotLike findByNameNotLike(String name) where name not like '%#{name}%'
StartWith findByNameStartWith(String name) where name like '#{name}%'
EndWith findByNameEndWith(String name) where name like '%#{name}'
Not findByNameNot(String name) where name <> #{name}
In findByNameIn(List<String> names) where name in ('A', 'B', 'C')
NotIn findByNameNotIn(List<String> names) where name not in ('A', 'B', 'C')
OrderBy findByNameOrderById(String name) where name = #{name} order by id
Desc findByNameOrderByIdDesc(String name) where name = #{name} order by id desc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章