Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
ZhangKuan
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
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
张宽
ZhangKuan
Commits
edb870e0
Commit
edb870e0
authored
Mar 16, 2018
by
22管33
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构OR查询
parent
8b3d8351
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
74 deletions
+33
-74
README.md
README.md
+2
-10
Util.java
domain/src/main/java/com/haomostudio/Util/Util.java
+29
-40
MybatisExampleHelper.java
...eekTruthBackend/service/HmUtils/MybatisExampleHelper.java
+2
-24
No files found.
README.md
View file @
edb870e0
...
@@ -41,16 +41,8 @@ PS:项目根目录下面的脚本作用clean_mybatis_generated.sh和clean_mybati
...
@@ -41,16 +41,8 @@ PS:项目根目录下面的脚本作用clean_mybatis_generated.sh和clean_mybati
1.
将下面的代码拷贝到生成
**xxxExample.java**
文件中,拷贝的位置为抽象类
**GeneratedCriteria**
中,如下图所示。
1.
将下面的代码拷贝到生成
**xxxExample.java**
文件中,拷贝的位置为抽象类
**GeneratedCriteria**
中,如下图所示。
```
```
public Criteria filedsValueOr(List<String> fileds, String value, String column){
public Criteria fieldsValuesOr( List<String> fields,List<String> values, String column){
addCriterion(Util.dealDataBaseOR(fileds,value,column));
addCriterion(Util.dealFieldsValuesDataBaseOR(fields,values,column));
return (Criteria) this;
}
public Criteria filedValuesOr(List<String> fileds, String value, String column){
addCriterion(Util.dealValuesDataBaseOR(fileds,value,column));
return (Criteria) this;
}
public Criteria filedsValuesOr( List<String> fileds,List<String> values, String column){
addCriterion(Util.dealFieldsValuesDataBaseOR(fileds,values,column));
return (Criteria) this;
return (Criteria) this;
}
}
...
...
domain/src/main/java/com/haomostudio/Util/Util.java
View file @
edb870e0
...
@@ -10,53 +10,42 @@ import java.util.List;
...
@@ -10,53 +10,42 @@ import java.util.List;
*/
*/
public
class
Util
{
public
class
Util
{
public
static
String
deal
DataBaseOR
(
List
<
String
>
fileds
,
String
value
,
String
column
){
public
static
String
deal
FieldsValuesDataBaseOR
(
List
<
String
>
fields
,
List
<
String
>
values
,
String
column
){
StringBuffer
stringBuffer
=
new
StringBuffer
();
StringBuffer
stringBuffer
=
new
StringBuffer
();
stringBuffer
.
append
(
"( "
);
stringBuffer
.
append
(
"( "
);
fileds
.
stream
().
forEach
(
filed
->{
if
(
fields
==
null
||
values
==
null
||
(
fields
!=
null
&&
fields
.
size
()
==
0
)
||
(
values
!=
null
&&
fields
.
size
()
==
0
)
){
return
"1=1"
;
}
if
(
fields
.
size
()
>
1
&&
values
.
size
()
>
1
){
int
i
=
0
;
for
(
String
value:
values
){
if
(
column
.
contains
(
"like"
)){
if
(
column
.
contains
(
"like"
)){
stringBuffer
.
append
(
filed
+
" like '%"
+
value
+
"%' or "
);
stringBuffer
.
append
(
fields
.
get
(
i
)
+
" like '%"
+
value
+
"%' or "
);
}
else
{
}
else
{
stringBuffer
.
append
(
filed
+
" = '"
+
value
+
"' or "
);
stringBuffer
.
append
(
fields
.
get
(
i
)
+
" = '"
+
value
+
"' or "
);
}
}
i
++;
});
stringBuffer
.
delete
(
stringBuffer
.
length
()-
4
,
stringBuffer
.
length
()-
1
);
stringBuffer
.
append
(
") "
);
return
stringBuffer
.
toString
();
}
}
}
else
{
public
static
String
dealValuesDataBaseOR
(
List
<
String
>
values
,
String
filed
,
String
column
){
boolean
fieldsOrValue
=
fields
.
size
()
==
1
;
StringBuffer
stringBuffer
=
new
StringBuffer
();
List
<
String
>
lists
=
fieldsOrValue
?
values:
fields
;
stringBuffer
.
append
(
"( "
);
lists
.
stream
().
forEach
(
list
->{
values
.
stream
().
forEach
(
value
->{
if
(
column
.
contains
(
"like"
)){
if
(
column
.
contains
(
"like"
)){
stringBuffer
.
append
(
filed
+
" like '%"
+
value
+
"%'
or "
);
stringBuffer
.
append
((
fieldsOrValue
?
fields
.
get
(
0
):
list
)+
" like '%"
+(
fieldsOrValue
?
list:
values
.
get
(
0
))+
"%'
or "
);
}
else
{
}
else
{
stringBuffer
.
append
(
filed
+
" = '"
+
value
+
"'
or "
);
stringBuffer
.
append
((
fieldsOrValue
?
fields
.
get
(
0
):
list
)+
" = '"
+(
fieldsOrValue
?
list:
values
.
get
(
0
))+
"'
or "
);
}
}
});
});
stringBuffer
.
delete
(
stringBuffer
.
length
()-
4
,
stringBuffer
.
length
()-
1
);
stringBuffer
.
append
(
") "
);
return
stringBuffer
.
toString
();
}
public
static
String
dealFieldsValuesDataBaseOR
(
List
<
String
>
fileds
,
List
<
String
>
values
,
String
column
){
StringBuffer
stringBuffer
=
new
StringBuffer
();
stringBuffer
.
append
(
"( "
);
int
i
=
0
;
for
(
String
value:
values
){
if
(
column
.
contains
(
"like"
)){
stringBuffer
.
append
(
fileds
.
get
(
i
)+
" like '%"
+
value
+
"%' or "
);
}
else
{
stringBuffer
.
append
(
fileds
.
get
(
i
)+
" = '"
+
value
+
"' or "
);
}
i
++;
}
}
stringBuffer
.
delete
(
stringBuffer
.
length
()-
4
,
stringBuffer
.
length
()-
1
);
stringBuffer
.
delete
(
stringBuffer
.
length
()-
4
,
stringBuffer
.
length
()-
1
);
stringBuffer
.
append
(
") "
);
stringBuffer
.
append
(
") "
);
if
(
fields
.
size
()
==
1
&&
values
.
size
()
==
1
){
return
stringBuffer
.
toString
().
replace
(
" or"
,
""
);
}
return
stringBuffer
.
toString
();
return
stringBuffer
.
toString
();
}
}
...
...
service/src/main/java/com/haomostudio/SeekTruthBackend/service/HmUtils/MybatisExampleHelper.java
View file @
edb870e0
...
@@ -89,32 +89,10 @@ public class MybatisExampleHelper {
...
@@ -89,32 +89,10 @@ public class MybatisExampleHelper {
Method
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
funcName
);
Method
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
funcName
);
switch
(
condition
){
switch
(
condition
){
case
"fi
ledsValue
Or"
:
case
"fi
eldsValues
Or"
:
try
{
try
{
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
condition
);
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
condition
);
m
.
invoke
(
exampleObjCriteria
,(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"fileds"
),((
Map
<
String
,
Object
>)
value
).
get
(
"value"
),
column
);
m
.
invoke
(
exampleObjCriteria
,(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"values"
),(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"fields"
),
column
);
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvocationTargetException
e
)
{
e
.
printStackTrace
();
}
break
;
case
"filedValuesOr"
:
try
{
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
condition
);
m
.
invoke
(
exampleObjCriteria
,(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"values"
),((
Map
<
String
,
Object
>)
value
).
get
(
"filed"
),
column
);
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvocationTargetException
e
)
{
e
.
printStackTrace
();
}
break
;
case
"filedsValuesOr"
:
try
{
m
=
MybatisExampleHelper
.
getMethod
(
exampleObjCriteria
,
condition
);
m
.
invoke
(
exampleObjCriteria
,(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"fileds"
),(
List
<
String
>)((
Map
<
String
,
Object
>)
value
).
get
(
"values"
),
column
);
}
catch
(
IllegalAccessException
e
)
{
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
catch
(
InvocationTargetException
e
)
{
}
catch
(
InvocationTargetException
e
)
{
...
...
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