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
原文地址:https://www.cnblogs.com/dreamroute/p/13477148.html