springboot_data_access_demo基于rapid,根据自定义模版生成的基于mybatis+mysql的数据库访问示例项目。简单配置数据库信息,配置不同的生成策略生成可以直接运行访问数据库的项目,吸取了mybatis generator的动态条件优势,同时又稍有扩展。可以生成简单易懂的sql,支持大部分单表操作,一般情况下不需要自己动手写sql。模板可以根据自己需求做相应的修改(github地址)。

1、自动生成支持的方法有:

public interface BaseDaoMapper<T, E> {
/**
* 根据 key 查询
* @param key 查询的id
* @return
*/
T getByPrimaryKey(E key); /**
* 根据 keyList 查询
* @param keyList 查询id的集合
* @return
*/
List<T> getByPrimaryKeyList(@Param("keyList") List<E> keyList); /**
* 根据条件查询
* @param example 查询条件
* @return
*/
T getSingleByPredicate(BaseExample example); /**
* 根据条件查询所有
* @param example 查询条件
* @return
*/
List<T> getAllByPredicate(BaseExample example); /**
* 根据条件查询
* @param example 查询条件
* @return
*/
int queryCount(BaseExample example); /**
* 分页查询(配合pageHelper)
* @param example 查询条件
* @return
*/
List<T> getListByPage(BaseExample example); /**
* 根据key更新除了key以外的其他字段
* @param record
* @return
*/
int updateByPrimaryKey(@Param("record") T record); /**
* 按条件更新
* @param record 需要更新的字段
* @param example 需要满足的条件
* @return
*/
int updateByPredicate(@Param("record") T record, @Param("example")BaseExample example); /**
* 新增记录
* @param entity 需要新增的entity
* @return
*/
int insert(T entity); /**
* 批量新增
* @param list 批量新增的entity
* @return
*/
int batchInsert(@Param("list") List<T> list);
}

生成的mapper.xml示例

2、自己扩展

public interface UserinfoMapper extends BaseDaoMapper<UserinfoEntity, java.lang.Integer> {

}

在对应的Mapper里可以自己扩展,然后只需要在对应的 UserinfoExtendMapper.xml 里写自定义的sql 即可。

3、生成结构

生成的项目结构:(com-xxx-demo 可以通过生成器自己配置)

  • mr-xxx-demo-common
  • mr-xxx-demo-dao
  • mr-xxx-demo-model
  • mr-xxx-demo-server
  • mr-xxx-demo-service

目前仅支持单个数据库的配置,后续考虑同时支持多个。此项目是以名为Test的数据库生成的示例

  xxxExample支持指定查询列、查询条件、排序字段生成动态sql

示例:

    public void test() {
UserinfoEntity entity1 = new UserinfoEntity();
entity1.setSex(1);
entity1.setUsername("zhangsan"); UserinfoEntity entity2 = new UserinfoEntity();
entity2.setSex(2);
entity2.setUsername("lisi"); BaseExample example = UserinfoExample.builder()
//指定查询列为 id,username
.includeSelectFieldClause(UserinfoExample.builderSelectFieldCriteria().id().username())
//指定查询条件为 id in (5,6)
.addCriteria(UserinfoExample.builderCriteria().andIdIn(Lists.newArrayList(5, 6)))
//指定 order by id asc
.orderByClause(UserinfoExample.buildOrderByCriteria().orderByIdAsc())
.build(); BaseExample updateExample = UserinfoExample.builder()
.addCriteria(UserinfoExample.builderCriteria().andIdIn(Lists.newArrayList(5, 6)))
.build(); userinfoMapper.updateByPredicate(entity1, updateExample); userinfoMapper.getSingleByPredicate(example); userinfoMapper.getByPrimaryKey(1); userinfoMapper.getByPrimaryKeyList(Lists.newArrayList(1, 2, 3)); userinfoMapper.batchInsert(Lists.newArrayList(entity1, entity2)); userinfoMapper.getAllByPredicate(example); userinfoMapper.getListByPage(example); userinfoMapper.queryCount(example); entity1.setId(5);
entity1.setUsername("lisi - 22222");
userinfoMapper.updateByPrimaryKey(entity1); userinfoMapper.insert(entity2);
}

springboot+mybatis项目自动生成的更多相关文章

  1. Springboot mybatis generate 自动生成实体类和Mapper

    https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...

  2. SpringBoot+Mybatis+MySql 自动生成代码 自动分页

    一.配置文件 <!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <arti ...

  3. Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库

    1. Springboot mybatis 介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数获取 ...

  4. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  5. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  6. SpringBoot中mybatis的自动生成

    1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...

  7. MyBatis代码自动生成(利用命令)

    这几天在学习springmvc,需要用到mybatis,所以研究了一下mybatis自动代码生成,当然也可以手动敲,但是那样效率非常的慢,并且出错率也是很高的,利用MyBatis生成器自动生成实体类. ...

  8. MyBatis代码自动生成

    MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实 ...

  9. java实现的一个maven多模块项目自动生成工具

    平时在做spring mvc web新项目时,都需要自己去搭建spring mvc的项目框架,包括基本pom 依赖引入,基本配置文件(web.xml,spring-mvc.xml,数据库配置文件等等) ...

随机推荐

  1. 说说SPI协议

    SPI,是英语Serial Peripheral Interface 的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管 ...

  2. php学习十一:组合

    我们在类当中我往往会用到一些对象,此时的继承就无法满足我们的需求,这个时候我们需要用到组合.继承如果是is..a的关系,那么组合就是has...a的关系,直接在本类里面声明即可,不过声明的是一个对象 ...

  3. js里面函数的内部属性

    1.arguments用來存放传输参数的集合,可以被调用多次,每次数組都不一样,增强了函数的强壮性 实例: function calc() { var sum = 0; /*参数为一个时候*/ if ...

  4. CALayer的position,anchorPoint属性 与UIView的frame 属性

    彻底理解CALayer的position,anchorPoint属性 与UIView的frame 属性 一.position,anchorPoint两者都是CALayer的属性,都是CGPoint点 ...

  5. ubuntu android studio kvm

    Android studio 启动模拟器失败: Cannot launch AVD in emulator.Output:emulator: ERROR: x86 emulation currentl ...

  6. [黑金原创教程] FPGA那些事儿《数学篇》- CORDIC 算法

    简介 一本为完善<设计篇>的书,教你CORDIC算法以及定点数等,内容请看目录. 贴士 这本教程难度略高,请先用<时序篇>垫底. 目录 Experiment 01:认识CORD ...

  7. Lucene中最简单的索引和搜索示例

    package com.jiaoyiping.lucene; import org.apache.lucene.analysis.standard.StandardAnalyzer; import o ...

  8. 自定义事件类EventManager (TS中...args的使用例子)

    一个自定义事件类 初衷是使用Egret的事件有两点比较麻烦 1   在事件处理函数时,需要从e中获取data hander(e:egret.Event){ let data = e.data; } 2 ...

  9. 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流

    [BZOJ1834][ZJOI2010]network 网络扩容 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不 ...

  10. Ant教程

    安装ant,去http://ant.apache.org下载 配置环境变量(前提是配置了java环境变量) ANT_HOME G:\Software\ant1.9.7 //ant根目录 在PATH后添 ...