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,数据库配置文件等等) ...
随机推荐
- thinkjs之页面跳转-同步异步
对于刚入手thinkjs项目的新手来说,时常会犯的一个错误就是“混用”各种代码逻辑,比如:我们经常在做后台管理系统的时候用到的登录框, , 其实它原本是有一个路由专门存放自己的代码逻辑,而在点击提交按 ...
- Ubuntu远程登录服务器--ssh的安装和配置
ssh是一种安全协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全. 安装ssh sudo apt-get update sudo apt-get install openssh-serve ...
- element.style{}
有时在写css样式,并调试时,会出现很不可思议的现象,比如:我们定义了一个<div class=”aaa”></div>,在css中定义样式,.aaa{width:500px; ...
- [USB] Windows USB/DVD Download Tool
此工具为微软官方U盘启动盘制作工具 Windows USB/DVD Download Tool 说明:https://www.microsoft.com/en-us/download/windows- ...
- linux awk时间计算脚本
在linux如果计划时间是个麻烦事, 用awk脚本如下 BEGIN {FS=":";OFS=":"} {total_seconds=total_seconds+ ...
- MUI事件管理
模块:事件管理 http://dev.dcloud.net.cn/mui/event/ 事件绑定: 除了可以使用addEventListener()方法监听某个特定元素上的事件外, 也可以使用.on( ...
- C语言模拟ATM机界面
虽然是满屏的printf.printf.printf.printf......尴尬 但是一个小项目做下来还是能学习到很多的,有很多小的问题,不是亲自来敲一遍代码,是不会发现的.他的框架,每一个小函数功 ...
- Oracle数据库类型date
date日期类型 说明 oracle dd-mm-yyyy create table test1(birthday date); timestamp(n) 说明:邮戳的时间类型 于date的区别,邮戳 ...
- 集成tomcat插件到eclipse
Eclipse中Tomcat的配置及简单例子 环境: 系统: Windows 7 64位专业版 Eclipse: Eclipse Java EE IDE for Web Developers. ...
- EOS token 代币兑换的资料
eos token 兑换价格预估查询: https://eosscan.io/ https://steemit.com/eos/@sandwich/how-to-check-which-eos-p ...