地址
框架功能
- 內建提供基礎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 |