spring-data-mybatis-mini
spring-data-mybatis-mini
github地址
gitee地址
博客
等同于spring data jdbc + mybatis 动态sql能力
大道至简
抛弃繁琐的xml 只使用mybatis模版引擎即动态sql能力 sql写在markdown文件里
更容易书写和阅读 sql能统一管理查看底层基于springJdbc 而不是mybatis 更直接纯粹
提供单表增删改(没有删除) 批量更新插入等基础方法 支持分页 多数据源 读写分离
mybatis最大优点就是sql模版引擎
我也有且仅有使用这部分功能(对于使用过mybatis的无学习成本) 但底层使用springJDBC
更简单直接简化mybatis动态sql写法(可混用-写法还是mybatis那套) 比如
[@and id in idList] 等于
<if test="null!=idList and idList.size>0"> and id in <foreach
collection="idList" index="index" item="item" open="(" separator=","
close=")">#{item}</foreach></if>
== why not spring data jdbc,jpa,hibernate,mybaits,mybatis-plus等
- 基于spring data jdbc理念但扩展使用mybatis动态sql能力 对于复杂点查询支持更好
- 相比jpa 底层使用hibernate(当然也能sql) 只有sql 基于spring jdbc
无jpa根据方法名(复杂点需要你学习思考,名字老长,不透明) 简单没有黑魔法 学习成本低
sql写在markdown里,纯jdbc更易于调优 - 相比mybatis 没有cache,复杂join映射实体,无resultType,resultMap配置
扩展单表CRUD 只用他的动态sql能力的模版引擎和sql放到文件管理思想 去繁就简
取其优点抛弃鸡肋功能 - 相比mybatis-plus等扩展mybatis框架 他们做的越来越像hibernate,jpa
搞Criteria那套 基本脱离mybatis优点 - 查询只提供一个选择 就是sql写在markdown文件里 不会提供类似hibernate Criteria
多种选择说是灵活但项目多种有多种实现写法 你会有打人的冲动 - 缓存可以用SpringCache等上层方案
- 查询只能映射单一实体(VO,DO,DTO均可) 但现在推荐减少JOIN 推荐代码里join 后期会尝试写新的组件sqlHelper方式简化
== Getting Started
- 提供单表增删改(没有物理删除) 批量更新插入等基础方法
- 抛弃繁琐的xml 所有sql 写在markdown文件里 便于书写和阅读
默认位置sql包下repository接口名.md @ConfigLocation 可自定义位置 - 自定义更新 update/save/insert/delete 开头方法是更新操作
- 支持分页 分页参数必须是第一个参数
- 对于 " > “,” < “,” >= “,” <= “,” <> "无需转义(两边需有空格 我会自动替换转义)
- 提供if判断和in查询简写方式(偷懒 >-<)
- 注解属于spring data jpa 体系的
- 支持sql片段 [@sql XX] XX markdown文件XX名的sql片段
- 查询返回实体 不需要必须是DO 如果没特殊规范
也可直接返回VO层实体(抛弃繁琐的DO->DTO->VO 偷懒轻喷) - 支持批量更新插入(jdbc链接参数需加入rewriteBatchedStatements=true&allowMultiQueries=true)
- 分页某些特性支持mysql,oracle 主支持mysql
- 使用简单 约定大于配置 默认配置基本都满足
- 支持LocalDateTime LocalTime jdk8更方便的时间类型
== 其他特性 无特殊需要可不用关心
- 分页 可自定义同名+Count的sql 优化分页
- 支持读写分离 根据业务逻辑添加@ReadDataSource在方法名上 默认配置多数据源随机取
可自定义 - 多数源支持但在微服务化潮流里尽量保证同一数据源
== 使用步骤基本同jpa,spring data jdbc
- 添加依赖
- @EnableMybatisMini
- extends BaseRepository<UserBaseDO, Long> 或 extends
BaseQueryRepository(只查询) - 使用例子demo项目spring-data-mybatis-mini-demo
Here is a quick teaser of an application using Spring Data
Repositories in Java:
=== Maven configuration
Add the Maven dependency:
<!-- spring boot 2.x 是使用版本2.3.5 低版本比如1.5.x 使用版本1.9.3 -->
<dependency><groupId>com.vonchange.common</groupId><artifactId>spring-data-mybatis-mini</artifactId><version>2.3.5</version>
</dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-commons</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.15</version>
</dependency>
import org.springframework.data.mybatis.mini.jdbc.repository.query.ConfigLocation;
import org.springframework.data.mybatis.mini.jdbc.repository.support.BaseRepository;
import org.springframework.data.repository.query.Param;public interface UserBaseRepository extends BaseRepository<UserBaseDO, Long> {@ReadDataSourceList<UserBaseDO> findList(@Param("userName") String userName,@Param("createTime") Date createTime);Page<UserBaseDO> findList(Pageable pageable, @Param("userName") String userName,@Param("createTime") Date createTime);String findUserName(@Param("userName") String userName);List<UserBaseVO> findListByIds(@Param("userName") String userName,@Param("createTime") Date createTime,@Param("idList")List<Long> idList);int updateIsDelete(@Param("isDelete") Integer isDelete,@Param("id") Long id);}
默认sql 包下同名吧UserBaseRepository.md 识别```开头结尾的 – 定义的同名方法
参见UserBaseRepository.md
实体类 定义ID 和TABLE 名
import javax.persistence.Id;
import javax.persistence.Table;@Data
@Table(name = "user_base")
public class UserBaseDO {@Idprivate Long id;private String userName;private String firstPhone;}
@Service
public class MyService {@Resourceprivate final UserBaseRepository userBaseRepository;public void doWork() {List<UserBaseDO> userBaseDOList = userBaseRepository.findList("test",new Date());}
}//添加 EnableMybatisMini 注解
@EnableMybatisMini
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
偷懒简化 if test 和in查询 识别 {@开头
[@and id in idList] 等于
<if test="null!=idList and idList.size>0"> and id in <foreach
collection="idList" index="index" item="item" open="(" separator=","
close=")">#{item}</foreach></if>
[@and user_name <> userName] 等于
<if test="null!=userName and ''!=userName"> and user_name <>
#{userName} </if>
in 查询List实体下的属性 [@and id in userList:id]
like
[@and user_name like userName] 等于 and user_name like CONCAT('%',?,'%')
[@and user_name like userName%] 等于 and user_name like CONCAT(?,'%')
[@and user_name like userName%] 等于 and user_name like CONCAT('%','test')
- 其他非4个分隔
[@and id in #{idList:in} and user_name like #{userName:like}]
等于
<if test="@com.vonchange.mybatis.tpl.MyOgnl@isNotEmpty(idList) and @com.vonchange.mybatis.tpl.MyOgnl@isNotEmpty(userName) "> and id in <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">#{item}</foreach> and user_name like CONCAT('%',#{userName},'%') </if>[@AND content -> '$.account' = #{bean.account}]等于<if test="null!=bean.account and ''!=bean.account">AND content -> '$.account' = #{bean.account}</if>
- [@sql XX] XX markdown文件XX名的sql片段
相关注解
@ColumnNot 非字段注解
InsertIfNull UpdateIfNull 插入或者更新为空时默认值 可使用函数
UpdateNotNull updateAllField方法NULL值忽略
ReadDataSource 指定某个方法读数据源 默认配置多数据源随机取
//自定义 读库数据源 不自定义默认所有你设置的数据源@Beanpublic ReadDataSources initReadDataSources(){return new ReadDataSources() {@Overridepublic DataSource[] allReadDataSources() {return new DataSource[]{mainDataSource(),mainDataSource(),readDataSource()};}};}
批量更新插入
jdbc链接参数需加入rewriteBatchedStatements=true&allowMultiQueries=true
提供insertBatch(默认第一行不为NULL的字段) 可在markdown里自定义sql
无需关心List对象大小经测试简单数据插入1万耗时1s以内
自定义实现(建议使用 更透明)
@BatchUpdate(size = 5000)int batchInsert(List<UserBaseDO> list);
只需定义单条insert 语句
-- batchInsert
insert into user_base(`user_name`,`mobile_phone`,create_time) values
(#{userName},#{mobilePhone},#{createTime})
大数据量流式读取
使用场景: 不用编写复杂分包逻辑,表数据大小,可关联表查 可直接 select * from 整个表
不用关心内存爆调 流的方式读取使用例子
定义方法
void findBigData(@Param("")AbstractPageWork<UserBaseDO> abstractPageWork,@Param("userName") String userName);
定义sql
-- findBigData
select * from user_base
<where>
[@and user_name like userName]
</where>
使用demo
AbstractPageWork<UserBaseDO> abstractPageWork = new AbstractPageWork<UserBaseDO>() {@Overrideprotected void doPage(List<UserBaseDO> pageContentList, int pageNum, Map<String, Object> extData) {pageContentList.forEach(userBaseDO -> {log.info("{}",userBaseDO.toString());});}@Overrideprotected int getPageSize() {return 500;}};userBaseRepository.findBigData(abstractPageWork,"三");log.info("{} {} {}",abstractPageWork.getSize(),abstractPageWork.getTotalPages(),abstractPageWork.getTotalElements());
spring-data-mybatis-mini相关推荐
- Spring Data JPA 与 MyBatis 对比,你喜欢用哪个?
来源:jianshu.com/p/3927c2b6acc0 概述 Spring Data JPA是Spring Data的子模块.使用Spring Data,使得基于"repositorie ...
- 聊一聊:MyBatis和Spring Data JPA的选择问题
从个人开发角度来说,Spring Data JPA更好用,是因为开发起来更快. 但从团队角度,我们希望更好的维护性,spring data jpa就差一些,或者说对后期人的要求更高. 很容易出现这种情 ...
- spring data jpa和mybatis的不同
使用spring data JPA和mybatis都是用来访问数据库,但他们的分工不同: 1.spring data jpa默认使用hibernate作为ORM实现,是spring 提供的一套jpa接 ...
- Spring data Jpa,Mybatis,读写锁,@Lock 使用
Spring data jpa 支持注解式的读写锁(悲观锁),实际上这个东西硬编码也简单,但是基于Jpa 命名方式定义的Sql,只能用注解添加支持读写锁了, 不了解读写锁的可以点这里 mysql读写锁 ...
- jpa、mybatis、hibernate、spring data jpa区别
在软件开发中,jdbc作为操作数据库的最基本api,它提供了操作数据库的方法.常见的数据库都实现了jdbc,具体的实现都是由数据库厂家来实现.但是jdbc不是一个orm框架(对象关系映射,简单理解为将 ...
- Fenix — 比 MyBatis 更加强大的 Spring Data JPA 扩展库
Fenix(菲尼克斯)是一个比 MyBatis 更加强大,为解决复杂.动态 SQL (JPQL) 而生的 Spring Data JPA 扩展库,目的是辅助开发者更方便.快捷的书写复杂.动态且易于维护 ...
- 468、Java框架122 -【Spring + SpringMVC + MyBatis - JSON】 2021.01.27
目录 0.本知识点效果 1.jquery.min.js 2.json中文问题 3.CategoryController 4.submit.html 5.getOne.html 6.getMany.ht ...
- [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
使用SSM(spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- Spring+SpringMVC+MyBatis整合教程
2019独角兽企业重金招聘Python工程师标准>>> 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- Spring Boot 应用系列 1 -- Spring Boot 2 整合Spring Data JPA和Druid,双数据源
最近Team开始尝试使用Spring Boot + Spring Data JPA作为数据层的解决方案,在网上逛了几圈之后发现大家并不待见JPA,理由是(1)MyBatis简单直观够用,(2)以Hib ...
最新文章
- 一文图解机器学习的基本算法!
- 欢迎访问github地址,并指出项目中的缺陷和BUG
- o2 atom(HP rw68xx)系列手机将可以使用Android。
- VB6 二维数组去重实现
- Codeforces | CF1029F 【Multicolored Markers】
- org.springframework.web.servlet.view.ContentNegotiatingViewResolver
- 计算机课程用到的软件,在电脑上录课用什么软件好?好用的录课软件推荐
- 安卓apk反编译教程
- 计算机桌面比例怎么调,如何调整计算机显示器的比例
- Macbook PS快捷键
- MATLAB寻找峰值函数
- win10企业版2016长期服务版本认证过期怎么办?
- 常用的logo设计技巧
- 《C#零基础入门之百识百例》(三十五)方法返回 -- 自守数
- Nginx 实战-负载均衡
- b460m迫击炮黑苹果bios
- 基才HDLC协议的SDH传输系统板间通信的设计
- 很有意思的思维方式:直线思维和曲线思维
- HKROnline SyncNavigator 注册机(非破解)下载地址,官方网站
- BMC 历史漏洞汇总
热门文章
- php jSignature,jSignature手写签名
- css101路由密码,192.168.0.101登陆官网登录入口
- 第二十章 树莓派 3/4 安装 FreeBSD
- 一、各种WAV文件头格式
- 看到一道题没懂为什么?
- ATTCK实战系列(二)
- 席慕容 一棵开花的树
- oracle dbms_workload_repository,AWR dbms_workload_repository使用
- RHCSA之路----20、创建逻辑卷
- 8进制和16进制 前导#