增加流读方法及其说明
<strong>[</strong>github地址<strong>](</strong><a href="https://github.com/VonChange/spring-data-mybatis-mini"><strong>https://github.com/VonChange/spring-data-mybatis-mini</strong></a><strong>) </strong><strong>[</strong>gitee地址<strong>](</strong><a href="https://gitee.com/vonchange/spring-data-mybatis-mini"><strong>https://gitee.com/vonchange/spring-data-mybatis-mini</strong></a><strong>)</strong> <strong>[</strong>blog<strong>](</strong><a href="http://www.vonchange.com/doc/mini.html">http://www.vonchange.com/doc/mini.html</a><strong>)</strong>
>> 具体更新说明文档
批量更新插入
jdbc链接参数需加入rewriteBatchedStatements=true&allowMultiQueries=true
提供insertBatch(默认第一行不为NULL的字段) 可在markdown里自定义sql 无需关心List对象大小
经测试简单数据插入1万耗时1s以内(mysql5.7)
自定义实现(建议使用 更透明)
@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>() {
@Override
protected void doPage(List<UserBaseDO> pageContentList, int pageNum, Map<String, Object> extData) {
pageContentList.forEach(userBaseDO -> {
log.info("{}",userBaseDO.toString());
});
}
@Override
protected int getPageSize() {
return 500;
}
};
userBaseRepository.findBigData(abstractPageWork,"三");
log.info("{} {} {}",abstractPageWork.getSize(),abstractPageWork.getTotalPages(),abstractPageWork.g
etTotalElements());
>>>>>>>>>>
<strong>**</strong><strong>等同于spring data jdbc + mybatis 动态sql能力</strong><strong>** </strong> <strong>**</strong><strong>大道至简 麻雀虽小 五脏俱全</strong><strong>** </strong> <strong>1. </strong>抛弃繁琐的xml 只使用mybatis模版引擎即动态sql能力 sql写在markdown文件里 更容易书写和阅读 sql能统一管理查看 <strong>2. </strong>底层基于springJdbc 而不是mybatis 更直接纯粹 <strong>3. </strong>提供单表增删改(没有删除) 批量更新插入等基础方法 支持分页 多数据源 读写分离 <strong>4. </strong>mybatis最大优点就是sql模版引擎 我也有且仅有使用这部分功能(对于使用过mybatis的无学习成本) 但底层使用springJDBC 更简单直接 <strong>5. </strong>简化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>
<strong> </strong><img alt="" src="https://gitee.com/vonchange/spring-data-mybatis-mini/raw/master/mini.png" /> == why not spring data jdbc,jpa,hibernate,mybaits,mybatis-plus等 <strong>1. </strong>基于spring data jdbc理念但扩展使用mybatis动态sql能力 对于复杂点查询支持更好 <strong>2. </strong>相比jpa 底层使用hibernate(当然也能sql) 只有sql 基于spring jdbc 无jpa根据方法名(复杂点需要你学习思考,名字老长,不透明) 简单没有黑魔法 学习成本低 sql写在markdown里,纯jdbc更易于调优 <strong>3. </strong>比价mybatis 没有cache,复杂join映射实体,无resultType,resultMap配置 扩展单表CRUD 只用他的动态sql能力的模版引擎和sql放到文件管理思想 去繁就简 取其优点抛弃鸡肋功能 <strong>4. </strong>相比mybatis-plus等扩展mybatis框架 他们做的越来越像hibernate,jpa 搞Criteria那套 基本脱离mybatis优点 <strong>5. </strong>查询只提供一个选择 就是sql写在markdown文件里 不会提供类似hibernate Criteria 多种选择说是灵活但项目多种有多种实现写法 你会有打人的冲动 <strong>6. </strong>缓存可以用SpringCache等上层方案 <strong>7. </strong>查询只能映射单一实体(VO,DO,DTO均可 支持继承) 但现在推荐减少JOIN 推荐代码里join 后期会尝试写新的组件sqlHelper方式简化
== Getting Started
== 其他特性 无特殊需要可不用关心
== 使用步骤基本同jpa,spring data jdbc
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.4 低版本比如1.5.x 使用版本1.9.2 -->
<dependency>
<groupId>com.vonchange.common</groupId>
<artifactId>spring-data-mybatis-mini</artifactId>
<version>2.3.4</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> {
@ReadDataSource
List<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 {
@Id
private Long id;
private String userName;
private String firstPhone;
}
@Service
public class MyService {
@Resource
private 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>