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. lua知识点整理

    1. lua全局环境和局部环境 local cf = loadstring(" local i=0 i=i+1 print(i) ") --从后面两个输出我们可以看出,生成的函数的 ...

  2. web基础---->java邮件的发送

    这里记录一下关于java邮件发送代码的编写.你在我身边也好,在天边也罢,想到世界的角落有一个你,觉得整个世界也变得温柔安定了. java邮件的发送 一.直接贴出代码,如下: package com.c ...

  3. 简单的网络爬虫程序(Web Crawlers)

    程序比较简单,但是能体现基本原理. package com.wxisme.webcrawlers; import java.io.*; import java.net.*; /** * Web Cra ...

  4. windows环境下最简单的nginx + tomcat负载均衡配置示例

    后端是两台tomcat服务器,我们简称为node1 和node2,访问地址分别是 http://192.168.1.2:8080 和 http://192.168.1.4:8080 前端使用nginx ...

  5. fluentValidation集成到autofac

    废话不说直接上代码 // 首先实现ValidatorFactory public class DependencyResolverValidatorFactory : ValidatorFactory ...

  6. Android Activity 半透明效果(Translucent)

    本文转自:http://norety.javaeye.com/blog/648725 今天试着做activity半透明的效果,做出来之后才发现想复杂了!很简单的几句就可以实现,不多说了,贴代码! 1. ...

  7. mysql存储过程基础示例

    转自:http://database.51cto.com/art/201608/516661.htm http://www.cnblogs.com/mark-chan/p/5384139.html D ...

  8. 微信小程序 --- 用户登录

    整体逻辑:点击用户中心,如果如果整个页面没有

  9. postgresql 表空间创建、删除

    表空间:字面上理解就是表存储的物理空间,其实包括数据库的表.索引.序列等. 可以将表空间创建在服务器的不同分区,这样做的好处有: 一.如果初始化集群所在分区已经用光,可以方便的其他分区上创建表空间已达 ...

  10. 170731、Nginx初探

    一. 概念 Nginx——Ngine X,是一款自由的.开源的.高性能HTTP服务器和反向代理服务器:也是一个IMAP.POP3.SMTP代理服务器:也就是说Nginx本身就可以托管网站(类似于Tom ...