Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
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
曲欣亮
common
Commits
35e0869d
Commit
35e0869d
authored
Dec 16, 2020
by
张永
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://gitlab.linkfern.com/key/common.git
parents
1188f535
071beacc
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
103 additions
and
36 deletions
+103
-36
pom.xml
pom.xml
+1
-1
DateUtil.java
src/main/java/com/egolm/common/DateUtil.java
+3
-1
ExcelUtil.java
src/main/java/com/egolm/common/ExcelUtil.java
+47
-28
StringUtil.java
src/main/java/com/egolm/common/StringUtil.java
+20
-0
JdbcTemplate.java
src/main/java/com/egolm/common/jdbc/JdbcTemplate.java
+22
-3
Dialect.java
src/main/java/com/egolm/common/jdbc/dialect/Dialect.java
+10
-3
No files found.
pom.xml
View file @
35e0869d
...
...
@@ -67,7 +67,7 @@
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
3.8
</version>
<version>
4.1.2
</version>
</dependency>
<dependency>
<groupId>
org.bouncycastle
</groupId>
...
...
src/main/java/com/egolm/common/DateUtil.java
View file @
35e0869d
...
...
@@ -106,7 +106,9 @@ public class DateUtil {
* @return
*/
public
static
Date
parse
(
Object
obj
)
{
if
(
obj
instanceof
Date
)
{
if
(
obj
==
null
)
{
return
null
;
}
else
if
(
obj
instanceof
Date
)
{
return
new
Date
(((
Date
)
obj
).
getTime
());
}
else
{
String
str
=
String
.
valueOf
(
obj
).
trim
();
...
...
src/main/java/com/egolm/common/ExcelUtil.java
View file @
35e0869d
...
...
@@ -3,25 +3,25 @@ package com.egolm.common;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.text.DecimalFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.poi.hssf.usermodel.HSSFCell
;
import
org.apache.poi.hssf.usermodel.HSSFCellStyle
;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
import
org.apache.poi.hssf.usermodel.HSSFRichTextString
;
import
org.apache.poi.hssf.usermodel.HSSFRow
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.CellType
;
import
org.apache.poi.ss.usermodel.Row
;
/**
...
...
@@ -32,7 +32,35 @@ import org.apache.poi.ss.usermodel.Row;
*/
public
class
ExcelUtil
{
/**
* 生成Excel
* @param os 输出流
* @param args 获取数据的键
* @param titles Excel表头
* @param widths Excel列宽度
* @param list 数据集
*/
public
static
void
excel
(
OutputStream
os
,
LinkedHashMap
<
String
,
String
>
titleMap
,
List
<
Map
<
String
,
Object
>>
list
)
{
List
<
String
>
argsList
=
new
ArrayList
<
String
>();
List
<
String
>
titleList
=
new
ArrayList
<
String
>();
for
(
String
key
:
titleMap
.
keySet
())
{
argsList
.
add
(
key
);
titleList
.
add
(
titleMap
.
get
(
key
));
}
String
[]
args
=
argsList
.
toArray
(
new
String
[
argsList
.
size
()]);
String
[]
titles
=
titleList
.
toArray
(
new
String
[
titleList
.
size
()]);
Object
[][]
objs
=
new
Object
[
list
.
size
()+
1
][
args
.
length
];
objs
[
0
]
=
titles
;
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
Map
<
String
,
Object
>
map
=
list
.
get
(
i
);
Object
[]
obj
=
new
Object
[
args
.
length
];
for
(
int
k
=
0
;
k
<
obj
.
length
;
k
++)
{
obj
[
k
]
=
map
.
get
(
args
[
k
]);
}
objs
[
i
+
1
]
=
obj
;
}
ExcelUtil
.
excel
(
os
,
objs
,
null
);
}
/**
...
...
@@ -80,8 +108,7 @@ public class ExcelUtil {
* @param widths 列宽度{24, 12, 15, 15}
*/
public
static
void
excel
(
OutputStream
os
,
Object
[][]
objs
,
Integer
[]
widths
)
{
try
{
HSSFWorkbook
wb
=
new
HSSFWorkbook
();
try
(
HSSFWorkbook
wb
=
new
HSSFWorkbook
())
{
HSSFCellStyle
bodyStyle
=
wb
.
createCellStyle
();
bodyStyle
.
setWrapText
(
true
);
HSSFSheet
sheet
=
wb
.
createSheet
(
"sheet1"
);
...
...
@@ -196,8 +223,7 @@ public class ExcelUtil {
* @throws IOException
*/
public
static
String
[][]
excel
(
InputStream
is
)
{
try
{
HSSFWorkbook
wb
=
new
HSSFWorkbook
(
is
);
try
(
HSSFWorkbook
wb
=
new
HSSFWorkbook
(
is
))
{
HSSFSheet
sheet
=
wb
.
getSheetAt
(
0
);
String
[][]
datas
=
new
String
[
sheet
.
getLastRowNum
()+
1
][];
for
(
int
i
=
0
;
i
<
datas
.
length
;
i
++)
{
...
...
@@ -222,39 +248,32 @@ public class ExcelUtil {
* @return
*/
private
static
String
stringVal
(
Cell
cell
)
{
try
{
int
cellType
=
cell
.
getCellType
();
if
(
cellType
==
HSSFCell
.
CELL_TYPE_
NUMERIC
)
{
if
(
HSSF
DateUtil
.
isCellDateFormatted
(
cell
))
{
return
DateUtil
.
format
(
HSSF
DateUtil
.
getJavaDate
(
cell
.
getNumericCellValue
()));
if
(
cell
!=
null
)
{
CellType
cellType
=
cell
.
getCellType
();
if
(
cellType
==
CellType
.
NUMERIC
)
{
if
(
org
.
apache
.
poi
.
ss
.
usermodel
.
DateUtil
.
isCellDateFormatted
(
cell
))
{
return
DateUtil
.
format
(
org
.
apache
.
poi
.
ss
.
usermodel
.
DateUtil
.
getJavaDate
(
cell
.
getNumericCellValue
()));
}
else
{
return
String
.
valueOf
(
FMT_INT
.
format
(
cell
.
getNumericCellValue
()));
}
}
else
if
(
cellType
==
HSSFCell
.
CELL_TYPE_
STRING
)
{
return
String
.
valueOf
(
cell
.
getRichStringCellValue
()
);
}
else
if
(
cellType
==
HSSFCell
.
CELL_TYPE_
FORMULA
)
{
}
else
if
(
cellType
==
CellType
.
STRING
)
{
return
cell
.
getStringCellValue
(
);
}
else
if
(
cellType
==
CellType
.
FORMULA
)
{
String
value
=
String
.
valueOf
(
cell
.
getNumericCellValue
());
if
(
value
.
equals
(
"NaN"
))
{
value
=
String
.
valueOf
(
cell
.
getRichStringCellValue
());
}
return
value
;
}
else
if
(
cellType
==
HSSFCell
.
CELL_TYPE_
BOOLEAN
)
{
}
else
if
(
cellType
==
CellType
.
BOOLEAN
)
{
return
String
.
valueOf
(
cell
.
getBooleanCellValue
());
}
else
if
(
cellType
==
HSSFCell
.
CELL_TYPE_BLANK
)
{
return
""
;
}
else
if
(
cellType
==
HSSFCell
.
CELL_TYPE_ERROR
)
{
}
else
if
(
cellType
==
CellType
.
BLANK
||
cellType
==
CellType
.
ERROR
)
{
return
""
;
}
else
{
return
String
.
valueOf
(
cell
.
getRichStringCellValue
()
);
return
cell
.
getStringCellValue
(
);
}
}
catch
(
Exception
e
)
{
return
null
;
}
else
{
return
""
;
}
}
public
static
void
main
(
String
[]
args
)
throws
FileNotFoundException
,
IOException
{
String
[][]
ds
=
new
String
[][]{{
"1"
,
"2"
,
"3"
},
{
"4"
,
"5"
,
"6"
}};
ExcelUtil
.
csv
(
new
FileOutputStream
(
"D:/test.csv"
),
ds
);
}
}
src/main/java/com/egolm/common/StringUtil.java
View file @
35e0869d
...
...
@@ -1161,6 +1161,8 @@ public class StringUtil {
return
null
;
}
else
if
(
object
instanceof
Date
)
{
return
DateUtil
.
format
((
Date
)
object
);
}
else
if
(
object
instanceof
Throwable
)
{
return
toStackString
((
Throwable
)
object
).
toString
();
}
return
object
.
toString
();
}
...
...
@@ -1169,4 +1171,22 @@ public class StringUtil {
return
str1
==
null
?
str2
==
null
:
str1
.
equals
(
str2
);
}
public
static
StringBuffer
toStackString
(
Throwable
ex
)
{
StringBuffer
stack
=
new
StringBuffer
();
if
(
ex
!=
null
)
{
stack
.
append
(
ex
.
getClass
().
getName
()).
append
(
": "
).
append
(
ex
.
getMessage
()).
append
(
System
.
lineSeparator
());
StackTraceElement
[]
elms
=
ex
.
getStackTrace
();
for
(
StackTraceElement
elm
:
elms
)
{
stack
.
append
(
"\t"
).
append
(
elm
.
getClassName
()).
append
(
"."
).
append
(
elm
.
getMethodName
()).
append
(
"("
).
append
(
elm
.
getFileName
()).
append
(
" "
).
append
(
elm
.
getLineNumber
()).
append
(
")\n"
);
}
if
(
ex
.
getCause
()
!=
null
)
{
stack
.
append
(
"\n\n"
).
append
(
"Cause By: \n"
).
append
(
toStackString
(
ex
.
getCause
()));
}
for
(
Throwable
e
:
ex
.
getSuppressed
())
{
stack
.
append
(
"\n\n"
).
append
(
"Suppressed By: \n"
).
append
(
toStackString
(
e
));
}
}
return
stack
;
}
}
src/main/java/com/egolm/common/jdbc/JdbcTemplate.java
View file @
35e0869d
...
...
@@ -274,9 +274,28 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
}
public
int
save
(
String
tableName
,
Map
<
String
,
Object
>
objMap
)
{
Sql
desc
=
dialect
.
argsInsert
(
tableName
,
objMap
);
int
save_count
=
this
.
executeUpdate
(
desc
.
getSql
(),
desc
.
getArgs
());
return
save_count
;
Sql
iSql
=
dialect
.
argsInsert
(
tableName
,
objMap
);
KeyHolder
keyHolder
=
new
GeneratedKeyHolder
();
int
count
=
super
.
update
(
new
PreparedStatementCreator
()
{
public
PreparedStatement
createPreparedStatement
(
Connection
connection
)
throws
SQLException
{
PreparedStatement
ps
=
connection
.
prepareStatement
(
iSql
.
getSql
(),
Statement
.
RETURN_GENERATED_KEYS
);
Object
[]
args
=
iSql
.
getArgs
();
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
Object
argObj
=
args
[
i
];
if
(
argObj
instanceof
java
.
util
.
Date
)
{
java
.
util
.
Date
argDate
=
(
java
.
util
.
Date
)
argObj
;
ps
.
setObject
(
i
+
1
,
new
java
.
sql
.
Timestamp
(
argDate
.
getTime
()));
}
else
if
(
argObj
.
getClass
().
isEnum
())
{
ps
.
setObject
(
i
+
1
,
args
[
i
].
toString
());
}
else
{
ps
.
setObject
(
i
+
1
,
args
[
i
]);
}
}
return
ps
;
}
},
keyHolder
);
objMap
.
putAll
(
keyHolder
.
getKeys
());
return
count
;
}
public
int
delete
(
Object
...
objs
)
{
...
...
src/main/java/com/egolm/common/jdbc/dialect/Dialect.java
View file @
35e0869d
...
...
@@ -333,9 +333,16 @@ public interface Dialect {
default
Sql
argsQuery
(
String
tableName
,
Map
<
String
,
Object
>
objMap
)
{
List
<
String
>
wheres
=
new
ArrayList
<
String
>();
List
<
Object
>
args
=
new
ArrayList
<
Object
>();
for
(
String
key
:
objMap
.
keySet
())
{
wheres
.
add
(
getColumnLeft
()
+
key
+
getColumnRight
()
+
" = ?"
);
args
.
add
(
objMap
.
get
(
key
));
if
(
objMap
!=
null
)
{
for
(
String
key
:
objMap
.
keySet
())
{
Object
val
=
objMap
.
get
(
key
);
if
(
val
==
null
)
{
wheres
.
add
(
getColumnLeft
()
+
key
+
getColumnRight
()
+
" is null"
);
}
else
{
wheres
.
add
(
getColumnLeft
()
+
key
+
getColumnRight
()
+
" = ?"
);
args
.
add
(
val
);
}
}
}
String
sql
=
"select * from "
+
tableName
+
StringUtil
.
join
(
" and "
,
" where "
,
""
,
wheres
);
return
new
Sql
(
sql
,
args
.
toArray
());
...
...
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