地址
框架功能
- 内置提供基础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:实体属性与数据列的映射关系(默认:驼峰属性自动转成下划线风格)
灵魂功能
比如:
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);
}