springboot+mybatis项目自动生成
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项目自动生成的更多相关文章
- Springboot mybatis generate 自动生成实体类和Mapper
https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...
- SpringBoot+Mybatis+MySql 自动生成代码 自动分页
一.配置文件 <!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <arti ...
- Springboot 系列(十一)使用 Mybatis(自动生成插件) 访问数据库
1. Springboot mybatis 介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数获取 ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- SpringBoot 添加mybatis generator 自动生成代码插件
自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...
- SpringBoot中mybatis的自动生成
1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...
- MyBatis代码自动生成(利用命令)
这几天在学习springmvc,需要用到mybatis,所以研究了一下mybatis自动代码生成,当然也可以手动敲,但是那样效率非常的慢,并且出错率也是很高的,利用MyBatis生成器自动生成实体类. ...
- MyBatis代码自动生成
MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实 ...
- java实现的一个maven多模块项目自动生成工具
平时在做spring mvc web新项目时,都需要自己去搭建spring mvc的项目框架,包括基本pom 依赖引入,基本配置文件(web.xml,spring-mvc.xml,数据库配置文件等等) ...
随机推荐
- JS 操作iframe
很多人一直都有个想法,要是可以随心所欲的操作iframe就好了.这样静态页面也就有了相当于后台动态页面php,jsp,asp中include,require实现统一多页面布局的能力. 通过Javasc ...
- JSON.parse()和JSON.stringfy()
JSON.parse()从字符串中解析出JSON对象: var data = '{"a":1,"b":2}'; JSON.parse(data); JSON.s ...
- 微信红包随机生成算法(PHP版)
/** * 求一个数的平方 * @param $n */ function sqr($n){ return $n*$n; } /** * 生产min和max之间的随机数,但是概率不是平均的,从min到 ...
- Delphi的打开文件对话框-TOpenDialog
1.TOpenDialog组件的典型用法 “打开”对话框是用TOpenDialog组件实现的,TOpenDialog组件是非可视组件. Filter属性用于设置文件过滤器,让对话框只列出特定类型的文件 ...
- PHP一句话木马小总结与SQL语句写一句话木马
一.基础类的一句话--功能仅限于验证漏洞了,实际中太容易被查出出来: <?php @eval($_GET["code"])?> <?php @system($_P ...
- wamp安装和基础配置
一 下载地址 二 安装 三 修改默认网站目录 四 修改数据库密码 一 下载地址 wamp百度软件中心 wamp官方下载地址 二 安装 windows环境下wampserver的配置教程——超级详细 w ...
- 企业服务的3种模式:On-Premise、SaaS、Mixed,该选哪种?--创业邦
B轮融资二三事 我们从9月份开始启动B轮融资,与这些颇具洞察力的投资人聊天,是非常有挑战的事.他们的很多观点充满智慧,能帮你突破思考局限,受益良多.当然,整个过程虽然有趣但也不轻松,毕竟你的目的是完成 ...
- 手机联系人信息获取(contacts) ---- HTML5+
模块:contacts Contacts模块管理系统通讯录,用于可对系统通讯录进行增.删.改.查等操作.通过plus.contacts获取系统通讯录管理对象. 对象:联系人对象(属性:电话,地址等)针 ...
- 如何通过命令在Ubuntu中安装PyCharm
对于Ubuntu 16.10和Ubuntu 17.04,通过Ctrl + Alt + T打开终端,或通过从应用启动器搜索“terminal”,打开后,执行以下步骤: 安装: 1.通过命令添加PPA存储 ...
- pta 习题集5-17 哥尼斯堡的“七桥问题”
哥尼斯堡是位于普累格河上的一座城市,它包含两个岛屿及连接它们的七座桥,如下图所示. 可否走过这样的七座桥,而且每桥只走过一次?瑞士数学家欧拉(Leonhard Euler,1707-1783)最终解决 ...