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. .Net内存溢出 System.OutOfMemoryException

    内存溢出常见的情况和处理方式: http://outofmemory.cn/c/dotNet-outOfMemoryException MSDN中关于processModel的文档 https://m ...

  2. poj_3662 最小化第k大的值

    题目大意 有N个节点以及连接的P个无向边,现在要通过这P条边从1号节点连接到N号节点.若无法连接成功,则返回-1:若能够连接成功,那么其中用到了L条边,这L条边中有K条边可以免费,L-K条边不能免费, ...

  3. HDU 4462Scaring the Birds(枚举所有状态)

    Scaring the Birds Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. adb shell dumpsys的用法

    一. 打印出设备中所有service的信息 zh@zh:~$adb shell dumpsys -l (字母l)zh@zh:~$adb shell service list 二.查询到运行的syste ...

  5. mysql数据类型及存储过程

    转自:http://www.cnblogs.com/mark-chan/p/5384139.html 存储过程简介 SQL语句需要先编译然后执行,而存储过程(Stored Procedure)是一组为 ...

  6. 最小生成树的变形(次小生成树hdu4081)

    hdu4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  7. 获取鼠标经过位置的X、Y坐标

    利用JavaScript获取鼠标经过位置的X.Y坐标方法. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  8. Java中的文件和流相关知识

    1. File File类可以使用文件路径字符串来创建File实例,该文件路径可以是绝对路径或相对路径 File类的list()方法中可以接收一个FilenameFilter参数,通过该参数可以只列出 ...

  9. 用了快1年的MacBook Pro遇到的硬件问题

    去年11月7日买的MacBook Pro,到目前快1年了,遇到了3个硬件问题(之前用了5年的Thinkpad在5年内未出现任何硬件问题): 1. 有一次MacBook放在背包中,背包拎在手上落在地上, ...

  10. Feature extraction using convolution

    http://ufldl.stanford.edu/wiki/index.php/Feature_extraction_using_convolution http://ufldl.stanford. ...