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 ...
随机推荐
- Python Ethical Hacking - TROJANS Analysis(5)
Spoofing File Extention - A trick. Use the Kali Linux Program - Characters 1. Open the program. 2. F ...
- 使用truncate ,截断有外键约束的父表
此时有两种方法,解决1.删除外键约束,删除该表,在重建外键约束--查询外键约束select TABLE_NAME,CONSTRAINT_NAME,CONSTRAINT_TYPE,R_CONSTRAIN ...
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...
- 程序员肺被切掉一块还得去加班... 再谈“工作996,生病ICU”
如题,为什么要说再谈“工作996,生病ICU”,因为996问题早已不是一个新问题,在我最近刚出版的新书<SOD框架“企业级”应用数据架构实战>写作期间,爆发了一次程序员“起义”,出现了一个 ...
- 循序渐进nginx(二):反向代理、负载均衡、缓存服务、静态资源访问
目录 反向代理 使用 1.创建代理目标服务端: 2.配置nginx反向代理目标服务端: 3.测试使用: 负载均衡 使用 1.准备服务端 2.修改nginx配置 3.测试 负载均衡策略 负载均衡的额外参 ...
- Vue脚手架创建项目出现 (Failed to download repo vuejs-templates/webpack: Response code 404)
搭建好(脚手架2.X版本)环境像往常一样使用vue init webpack xxxx 创建项目可以是没多久就开始报错了 报错结果就是:vue-cli · Failed to download rep ...
- eclipse IDE usage of my own and tutorials link list
设置 字符集 Eclipse 修改字符集 默认情况下 Eclipse 字符集为 GBK,但现在很多项目采用的是 UTF-8,这是我们就需要设置我们的 Eclipse 开发环境字符集为 UTF-8, 设 ...
- CentOS6下yum搭建LNMP环境
1.关闭防火墙[root@CentOS ~]# chkconfig iptables off 2.关闭selinuxvi /etc/sysconfig/selinux //将SELINUX=enfor ...
- 第二章 Java基础知识(下)
2.1.分支结构(if.switch) 2.1.1.if语句 格式一: if (关系表达式) { 语句体; } 流程一: ①首先计算关系表达式的值 ②如果关系表达式的值为true就执行语句体 ③如果关 ...
- 牛客练习赛63 牛牛的斐波那契字符串 矩阵乘法 KMP
LINK:牛牛的斐波那契字符串 虽然sb的事实没有改变 但是 也不会改变. 赛时 看了E和F题 都不咋会写 所以弃疗了. 中午又看了一遍F 发现很水 差分了一下就过了. 这是下午和古队长讨论+看题解的 ...