Coder Social home page Coder Social logo

multipleselect's Introduction

广告:中后台低代码开发平台

文档地址,歡迎交流

multipleselect

java mybatis 实现简单多表通用查询

简介

实现项目中比较基本的多表通用查询。

​实现简单的实体类操作多表, 首先你的项目是使用了mybatis-plus 才可以使用。

不做任何更改,也不会对项目产生任何影响,与手写XML 功能一样。

通过解析实体,调用通用的XML来实现多表查询, 提供一个设计多表查询的思路,复杂的Sql嵌套等目前并不支持。

目前支持:

left join方式,能关联的两张表的实体中关联字段名称必须一样,数据库字段可以不一样可以通@TableField注解来解决,right join 换个位置喽 其它方式还没有)

where 基本查询条件, sql函数等

分页查询,order排序,group by

可以用来三两句搞定一些简单关联查询业务,解决不需要写的代码

设计说明

  • 如何关联表?

    找第一张表注解为 TableId (mybatis-plus 注解)的属性名, 到每二张表找同样的属性名, 如果没找到,反过来找,如果还没找到,挨个属性找。以此类推,实现关联的前提条件是 主从表的实体关联列名必须是一样的

    // user 表
    @TableId
    private Integer userId
    // address 表
    @TableId
    private Integer addressId
    private Integer userId
    //那么自动条件为  user.user_id = address.user_id
    //也可以是
    @TableId(value="id")
    private Integer userId
    // address 表
    @TableId(value="id")
    private Integer addressId
    @TableField(value="test_user_id")
    private Integer userId
    //目前只有left join
    //那么自动条件为  user.id = address.test_user_id
    //如果符合这设计条件,你就往里扔就完事了

使用说明

  1. 将 com.freedomen.multipselect 包放到你的项目中
  2. 使 com.freedomen.multipselect.mapper里的xml 要被扫描到,或手动配置
  3. com.freedomen.multipselect.service也要被发现
//引入service
@Autowired
private MultipleService multipleService;
//表关联, 关联用户表和地址表,查找 用户表的所有字段和地址表的所有字段
MultipleSelect multipleSelect = MultipleSelect.newInstance("${1}", new User(), new Address());

multipleSelect
	.where("${0}")
    .like("userName", "张三");

multipleService.mulSelect(multipleSelect);

查找字段

//MultipleSelect.newInstance 的第一个参数是所要查找的字段
//${0} 或 ${user} 表是第一张表的所有字段  ${0}.userName或${user}.userName表示userName字段, 默认第一张表的字段全部都返回的。 ${}中间的参数可以是后面实体的下标,也可以是表名 如user、user_address
//字段中有@TableField(exist=false)注解也是被跳过的
//下面是要订单表的所有信息 和用户的姓名与号码 和地址

MultipleSelect.newInstance("${1}.userName,${1}.userPhone,${2}", new Orders(), new User(), new Address());

查找条件

  • eq: =
  • notEq: !=
  • like: LIKE (前置已经加了 '%')
  • between: between
  • and: 改变接下来的连接方式为 AND练级(默认)
  • andOnce: 改变接下来一个的连接方式为 AND
  • or: 改变接下来的连接方式为 OR
  • orOnce 改变接下来一个的连接方式为 OR
  • division:括号 ,不支持括号嵌套括号
  • in: IN
  • notIn: NOT IN
  • notLike: NOT LIKE
  • isNull: IS NULL
  • isNotNull: IS NOT NULL
  • sql: 简易自定义带sql代码片段
  • ...
//实例好 查找实体后可以操作实体

//注意: 如何实体内属性有值  将会以 eq方式and连接做为where 条件
/*
可以关联的必要条件
Orders:
	@TableId //或者数据库字段为其它@TableId(value="id")
	private Long ordersId;
	private Long userId;
	...
User: 
	@TableId
	private Long userId;
	...
Address:
	@TableId
	private Long addressId;
	private Long userId;
	...
*/
MultipleSelect multipleSelect = MultipleSelect.newInstance("${1}.userName,${1}.userPhone,${2}", new Orders(), new User(), new Address());

multipleSelect
	.where("${0}") //哪张表
	.eq("ordersId", 1) //并且 订单id = 1
	.like("ordersName", "cmcc") //并且 订单名称 like ''%cmcc'
	.or() //改变后续操作关系为 OR, 默认为AND
    .notEq("orderSno", "123"); //或者 orderSno 不等于 '123'
    
multipleSelect
	.where("${1}") //哪张表接着用户表 默认and连接  可以 .or()改为 OR
	.in("userId", [1, 2, 3]); // 并且userId in [1, 2, 3]
    
multipleSelect
	.where("${2}")
    .or()
	.like("adressDetails", "江苏"); //或者  地址 like '江苏'

multipleService.mulSelect(multipleSelect); //查询

//括号
multipleSelect.where("${0}")
    .eq("componyId", 1)
    .division()
    .like("userName", "abcd")
    .or()
    .like("userPhone", "abcd");
// 部分sql: compony_id = 1 and (user_name = 'abcd' or user_phone = 'abcd')    

排序

//MultipleSelect.setOrderBy(...columns)
MultipleSelect.setOrderBy("${0}.createTime", "${1}.ordersName desc", "${2}.userId asc", ...)

分组

//分组一般都要结合聚集函数使用,可以使用的:AVG, COUNT, MAX, MIN, SUM
/**统计用户订单总额*/ 
// 聚集函数使用 函数名:${表名/下标}.属性名; 不可以重命名哦, 下面的sum 字段仍然是 price
MultipleSelect.newInstance("${1}, sum:${0}.price", new Orders(), new User());
//(...columns)
MultipleSelect.setGoupBy("${0}.userId", ...);

SQL 方法使用

//如  查找创建日期为 2019年10月 的订单;
//两个问号对应两个参数, 其中使用的仍然是实体的Filed名,不是数据表的字段名
multipleSelect.where("${orders}").sql("year(createTime)=? and month(createTime)=?", new Object[]{2019, 10});

分页

//MultipleSelect.setPage(pageNo, pageSize);
MultipleSelect.setPage(1, 15); //第一页 每页 15条

multipleService.mulSelect返回结果

//MultipleResult
/*	原型
	private List<Map<String, Object>> data; //结果数据
	private Integer pageNo; 	//如果设置了分页 会有
	private Integer pageSize; 	//如果设置了分页 会有
	private Integer total;		//如果设置了分页 会有
*/

逻辑删除

//默认是读取  mybatis-plus 的 TableLogic 注解 0 未删除,
//如果不是用 0 表示未删除, 可以修改 MultipleSelect 的 setCustomWhere 方法中的下面这段中的 0 
 
if (logic != null)
    sb.append(" AND ")
    .append(te.getNickName())
    .append(".")
    .append(logic)
    .append(" = ")
    .append("0");

multipleselect's People

Contributors

yangaijun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

multipleselect's Issues

生成sql报错

image
image
image
怎么久生成了这样的sql了 你的mybatis-plus什么版本
image

只改了一个这个com.baomidou.mybatisplus.annotations的区别

提议

多表连接连接字段自己定义

Autowired已经不知道有没有空更新一下。

required a bean of type 'com.freedomen.multipleselect.service.MultipleService' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

包怎么找?

你说导入包到项目中,但是你的包怎么查不到呀?

通过注解@TableId(value = "id")指定的字段名无效

// user 表
@TableId(value = "id")
private Integer userId

// address 表
@TableId(value = "id")
private Integer addressId
private Integer userId

测试结果:user.user_id、address.address_id

正确结果应该是user.id、address.id

总记录数与返回字段问题

1.有没有只查询总记录的入口
2.设置参数
-2.1 根据字段去重如:
count(distinct hs_id)
-2.2 目前有没有别的方法来设置字段,活用的那种
MultipleSelect m2 = MultipleSelect.newInstance("${1}.hsId",
new Abb(), new Bcc());
m2.setColumns("abb.hs_Id");
这种方法设置参数都是写死的 abb.hs_id
能否提供一种便捷的参数类型设置类似${1}.hsId

Please☺

括号有问题

multipleSelect.where("${1}")
.division()
.eq("userName", "张三")
.eq("userPhone", "123")
.or()
.division()
.eq("userName", "李四")
.eq("userPhone", "456");

sql: WHERE ( user_name = "张三" AND user_phone = "123" ) OR ( OR user_name = "李四" OR user_phone = "456" )

两个问题:
1.OR ( OR user_name = "李四"
多个括号的时候,第二个括号开始会多出来一个OR

我把WhereCustomSegment.java 中的第42行
if (segmentSql.get(segmentSql.size() - 1).indexOf("(") == -1
|| (segmentSql.get(segmentSql.size() - 1).indexOf("(") != -1 && segmentSql.get(segmentSql.size() - 1).indexOf(")") != -1))
改成if (segmentSql.get(segmentSql.size() - 1).indexOf("(") == -1)就好了

2.user_name = "李四" OR user_phone = "456"
这里我实际想要的是user_name = "李四" AND user_phone = "456"
发现
.or()
.division()
.eq("userName", "李四")
.and()
.eq("userPhone", "456");
可以达到预期效果,但感觉怪怪的

另外,条件构造器可以再完善下吗,比如isNull、groupBy、having

表自连

`select distinct meuproductcategory.serial_version_u_i_d serialVersionUID,meuproductcategory.id id,meuproductcategory.id parentId,meuproductcategory.name name,meuproductcategory.level level,meuproductcategory.product_count productCount,meuproductcategory.product_unit productUnit,meuproductcategory.nav_status navStatus,meuproductcategory.show_status showStatus,meuproductcategory.sort sort,meuproductcategory.icon icon,meuproductcategory.keywords keywords,meuproductcategory.description description,meuproductcategory.serial_version_u_i_d serialVersionUID,meuproductcategory.id id,meuproductcategory.id parentId,meuproductcategory.name name,meuproductcategory.level level,meuproductcategory.product_count productCount,meuproductcategory.product_unit productUnit,meuproductcategory.nav_status navStatus,meuproductcategory.show_status showStatus,meuproductcategory.sort sort,meuproductcategory.icon icon,meuproductcategory.keywords keywords,meuproductcategory.description description from meu_product_category as meuproductcategory left join meu_product_category as meuproductcategory on meuproductcategory.id = meuproductcategory.id

Cause: java.sql.SQLSyntaxErrorException: Not unique table/alias: 'meuproductcategory'

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Not unique table/alias: 'meuproductcategory'
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:93) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]`

想要查询的是这中的是不是不行啊
select * from meu_product_category as c1 left join meu_product_category as c2 on c1.id = c2.parent_id

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.