Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
ldp-docs
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
doc
ldp-docs
Commits
0c7c6238
Commit
0c7c6238
authored
Dec 31, 2020
by
马超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc: 完善HqlWhereHelper文档
parent
cd921366
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
237 additions
and
1 deletion
+237
-1
Hibernate条件查询工具类使用.md
开发文档/Hibernate条件查询工具类使用.md
+237
-1
No files found.
开发文档/Hibernate条件查询工具类使用.md
View file @
0c7c6238
...
...
@@ -31,7 +31,39 @@ LinkedList<Condition> conditions = HqlWhereHelper.getInstance().and("key","value
**HqlWhereHelper **
条件组合的方法列表如下,所有的方法都可以重复调用并随意组合,第一个方法也可以随意调用,不会多生成一个and或者or:
```
java
/**
/**
* 获取工具类实例
*
* @return
*/
public
static
HqlWhereHelper
getInstance
();
/**
* 通过条件列表实例化,新增条件会添加到此列表之后,适用于已有condition列表的情况下使用
*
* @param conditionList 条件列表
* @return
*/
public
static
HqlWhereHelper
getInstance
(
LinkedList
<
Condition
>
conditionList
);
/**
* 通过hql实例化,后续条件直接拼接到hql语句后
*
* @param hql
* @return
*/
public
static
HqlWhereHelper
getInstance
(
String
hql
);
/**
* 通过hql、条件列表实例化
*
* @param hql hql语句
* @param conditionList 条件列表
* @return
*/
public
static
HqlWhereHelper
getInstance
(
String
hql
,
LinkedList
<
Condition
>
conditionList
);
/**
* 使用and拼接map中的参数,sql参数过滤方式为filterType
*
* @param paramMap 参数map
...
...
@@ -125,6 +157,44 @@ LinkedList<Condition> conditions = HqlWhereHelper.getInstance().and("key","value
* @return
*/
public
HqlWhereHelper
endGroup
();
/**
* join一对一关联对象,生成默认别名(下划线加上类名首字母大写)
*
* @param field 关联对象在主表中的字段名
* @param mappingClazz 关联对象class
* @return
*/
public
HqlWhereHelper
join
(
String
field
,
Class
<?>
mappingClazz
);
/**
* join一对多关联对象,生成默认别名(下划线加上类名首字母大写)
*
* @param field 关联对象在主表中的字段名
* @param mappingClazz 关联对象class
* @return
*/
public
HqlWhereHelper
joinFetch
(
String
field
,
Class
<?>
mappingClazz
);
/**
* 一对一关联对象
*
* @param field 关联对象在主表中的字段名
* @param mappingClazz 关联对象class
* @param aliasName 查询别名
* @return
*/
public
HqlWhereHelper
join
(
String
field
,
Class
<?>
mappingClazz
,
String
aliasName
);
/**
* 一对多关联对象
*
* @param field 关联对象在主表中的字段名
* @param mappingClazz 关联对象class
* @param aliasName 查询别名
* @return
*/
public
HqlWhereHelper
joinFetch
(
String
field
,
Class
<?>
mappingClazz
,
String
aliasName
);
```
组合条件示例:
...
...
@@ -182,5 +252,171 @@ WHERE key1=value1 AND (key2>=10 AND key2<=50) OR key3 like 'value3'
## 级联查询
当实体类中有相关关联,并且需要用关联对象中的某个字段来做查询条件,可以使用join(一对一),joinFetch(一对多)将关联对象映射添加进来,并直接使用关联对象别名来区分查询字段。
下面使用城市City、地区Area来举例,城市为主控方,维护着多个区域类,可以通过地区名称来过滤城市。
City类
```
java
@Table
(
name
=
"ldp_city"
)
@Getter
@Setter
@Entity
@NoArgsConstructor
public
class
City
implements
Serializable
{
public
City
(
String
id
)
{
this
.
id
=
id
;
}
/**
* 主键ID
*/
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
,
generator
=
"custom-uuid"
)
@GenericGenerator
(
name
=
"custom-uuid"
,
strategy
=
CustomUUIDGenerator
.
STRATEGY_UUID
)
private
String
id
;
/**
* 城市名称
*/
@Column
(
name
=
"name"
)
private
String
name
;
/**
* 创建者ID
*/
@Column
(
name
=
"create_id"
)
@AutoComputed
(
command
=
ComputedCommand
.
USERID
)
private
String
createId
;
/**
* 创建时间
*/
@Column
(
name
=
"create_time"
)
@AutoComputed
(
command
=
ComputedCommand
.
DATETIME
)
private
Date
createTime
;
/**
* 更新者ID
*/
@Column
(
name
=
"update_id"
)
@AutoComputed
(
command
=
ComputedCommand
.
USERID
)
private
String
updateId
;
/**
* 更新时间
*/
@Column
(
name
=
"update_time"
)
@AutoComputed
(
command
=
ComputedCommand
.
DATETIME
)
private
Date
updateTime
;
/**
* 双向,一对多关联
*/
@JsonIgnoreProperties
(
"city"
)
@OneToMany
(
mappedBy
=
"city"
,
fetch
=
FetchType
.
EAGER
)
private
Set
<
LdpArea
>
areas
=
new
HashSet
<>();
}
```
Area类
```
java
@Table
(
name
=
"ldp_area"
)
@Getter
@Setter
@Entity
@NoArgsConstructor
public
class
Area
implements
Serializable
{
public
Area
(
String
id
)
{
this
.
id
=
id
;
}
/**
* 主键ID
*/
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
,
generator
=
"custom-uuid"
)
@GenericGenerator
(
name
=
"custom-uuid"
,
strategy
=
CustomUUIDGenerator
.
STRATEGY_UUID
)
private
String
id
;
/**
* 地区名称
*/
@Column
(
name
=
"name"
)
private
String
name
;
/**
* 创建者ID
*/
@Column
(
name
=
"create_id"
)
@AutoComputed
(
command
=
ComputedCommand
.
USERID
)
private
String
createId
;
/**
* 创建时间
*/
@Column
(
name
=
"create_time"
)
@AutoComputed
(
command
=
ComputedCommand
.
DATETIME
)
private
Date
createTime
;
/**
* 更新者ID
*/
@Column
(
name
=
"update_id"
)
@AutoComputed
(
command
=
ComputedCommand
.
USERID
)
private
String
updateId
;
/**
* 更新时间
*/
@Column
(
name
=
"update_time"
)
@AutoComputed
(
command
=
ComputedCommand
.
DATETIME
)
private
Date
updateTime
;
/**
* 双向,多对一关联
*/
@JsonIgnoreProperties
(
"areas"
)
@ManyToOne
(
fetch
=
FetchType
.
EAGER
)
@JoinColumn
(
name
=
"c_id"
)
private
City
city
;
}
```
通过城市的name进行查询,构造查询ConditionList
```
java
HqlWhereHelper
conditions
=
HqlWhereHelper
.
getInstance
()
.
and
(
"name"
,
"上海"
)
.
buildConditions
();
```
通过地区名称的name进行查询,构造查询ConditionList,没有传别名,默认使用下划线+类名首字母小写(_area)
```
java
HqlWhereHelper
conditions
=
HqlWhereHelper
.
getInstance
()
.
joinFetch
(
"areas"
,
Area
.
class
)
.
and
(
"_area.name"
,
"浦东新区"
);
```
也可以自定义的别名,通过别名进行查询
```
java
HqlWhereHelper
conditions
=
HqlWhereHelper
.
getInstance
()
.
joinFetch
(
"areas"
,
Area
.
class
,
"a"
)
.
and
(
"a.name"
,
"浦东新区"
);
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment