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 |
MyBatis-Pro,新一代的MyBatis增强框架的更多相关文章
- Spring+Mybatis+Mysql搭建分布式数据库访问框架
一.前言 用Java开发企业应用软件, 经常会采用Spring+MyBatis+Mysql搭建数据库框架.如果数据量很大,一个MYSQL库存储数据访问效率很低,往往会采用分库存储管理的方式.本文讲述如 ...
- MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
目录(?)[-] 一MyBatis简介与配置MyBatisSpringMySql MyBatis简介 MyBatisSpringMySql简单配置 搭建Spring环境 建立MySql数据库 搭建My ...
- 【转】MyBatis学习总结(七)——Mybatis缓存
[转]MyBatis学习总结(七)——Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualC ...
- 【转】MyBatis学习总结(一)——MyBatis快速入门
[转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC ...
- mybatis系列笔记(1)---mybatis入门
mybatis入门 MyBatis是什么? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了goog ...
- Mybatis配置信息浅析 MyBatis简介(二)
官方文档入门篇中有明确说明 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的. SqlSessionFactory 的实例可以通过 SqlSessionF ...
- springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
百度百科: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBat ...
随机推荐
- 大一寒假我在一个oj网站只刷了这些题从此入门了绝大部分通用算法
如果你想入门算法,那么我这篇文章也许可以帮到你. 先说点题外话.这是在一个不冷不热的寒假,照理来说寒假应该很冷,但这个寒假是真的舒服.这样舒服的寒假学习似乎是一件不可能的事情,所以我继续我的游戏生涯, ...
- 利用Data vault对数据仓库建模
简介 国内关于Data Vault的信息很少,所以决定写点什么,纯粹都是自己在这个行业10多年的摸爬滚打.不过为了效率,尽量做到简短,直接上干货.对于各个细节大家有不同的理解欢迎来讨论. 数据仓库建模 ...
- Asp.net Core 3.1基于AspectCore实现AOP,实现事务、缓存拦截器
最近想给我的框架加一种功能,就是比如给一个方法加一个事务的特性Attribute,那这个方法就会启用事务处理.给一个方法加一个缓存特性,那这个方法就会进行缓存. 这个也是网上说的面向切面编程AOP. ...
- Android多线程--AsyncTask
常见的多线程方法有: 继承Thread类 实现Runnable接口 Handler AsyncTask HandlerThread 1.定义 一个Android已经封装好的轻量级异步类 属于抽象类,即 ...
- C语言中的 “>>”与“<<”
1. ">>" int x = 16; printf("%d\n", x >> 1); 先将x转成二进制 10000, 不读最后一位, ...
- Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...
- python 连接 mysql 的三种驱动
连接 mysql 驱动 mysq1-client python2,3都能直接使用 对myaq1安装有要求,必须指定位置存在 配置文件 python-mysql python3 不支持 pymysql ...
- PHP strripos() 函数
实例 查找 "php" 在字符串中最后一次出现的位置: <?php高佣联盟 www.cgewang.comecho strripos("I love php, I ...
- luogu P4516 [JSOI2018]潜入行动
LINK:潜入行动 初看题感觉很不可做 但是树形dp的状态过于明显. 容易设\(f_{x,j,l,r}\)表示x为根子树内放了j个设备且子树内都被覆盖l表示x是否被覆盖r表示x是否放设备的方案数. 初 ...
- luogu 3188 [HNOI2007]梦幻岛宝珠
LINK:梦幻岛宝珠 时隔多日 我再次挑战这道题.还是以失败告终. 我觉得这一道背包真的有点难度 这是一个数量较少 但是价值和体积较大的背包. 通常的01背包 要不就是体积小 要么是价值小 但这道题给 ...