Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
b5b2baef
Commit
b5b2baef
authored
Dec 08, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update starrocks
parent
2765b561
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
41 deletions
+57
-41
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/StarrocksAutoConfiguration.java
...k/autoconfigure/starrocks/StarrocksAutoConfiguration.java
+17
-0
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/operation/StarrocksService.java
...k/autoconfigure/starrocks/operation/StarrocksService.java
+7
-8
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/operation/StarrocksServiceImpl.java
...toconfigure/starrocks/operation/StarrocksServiceImpl.java
+20
-24
support/schbrain-base-dao/src/main/java/com/schbrain/framework/dao/util/SQLUtil.java
...rc/main/java/com/schbrain/framework/dao/util/SQLUtil.java
+13
-9
No files found.
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/StarrocksAutoConfiguration.java
View file @
b5b2baef
package
com.schbrain.framework.autoconfigure.starrocks
;
package
com.schbrain.framework.autoconfigure.starrocks
;
import
com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties
;
import
com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties
;
import
com.zaxxer.hikari.HikariConfig
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.springframework.boot.autoconfigure.AutoConfiguration
;
import
org.springframework.boot.autoconfigure.AutoConfiguration
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
;
import
static
com
.
schbrain
.
framework
.
autoconfigure
.
starrocks
.
constants
.
StarrocksConstants
.
JDBC_URL_TEMPLATE
;
/**
/**
* @author liaozan
* @author liaozan
...
@@ -12,4 +18,15 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
...
@@ -12,4 +18,15 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
@EnableConfigurationProperties
(
StarrocksProperties
.
class
)
@EnableConfigurationProperties
(
StarrocksProperties
.
class
)
public
class
StarrocksAutoConfiguration
{
public
class
StarrocksAutoConfiguration
{
@Bean
(
"starrocksJdbcTemplate"
)
public
NamedParameterJdbcTemplate
starrocksJdbcTemplate
(
StarrocksProperties
config
)
{
HikariConfig
hikariConfig
=
new
HikariConfig
();
hikariConfig
.
setPoolName
(
config
.
getDatabase
());
hikariConfig
.
setDriverClassName
(
config
.
getDriverClassName
());
hikariConfig
.
setJdbcUrl
(
String
.
format
(
JDBC_URL_TEMPLATE
,
config
.
getHost
(),
config
.
getPort
(),
config
.
getDatabase
()));
hikariConfig
.
setUsername
(
config
.
getUsername
());
hikariConfig
.
setPassword
(
config
.
getPassword
());
return
new
NamedParameterJdbcTemplate
(
new
HikariDataSource
(
hikariConfig
));
}
}
}
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/operation/StarrocksService.java
View file @
b5b2baef
package
com.schbrain.framework.autoconfigure.starrocks.operation
;
package
com.schbrain.framework.autoconfigure.starrocks.operation
;
import
org.springframework.jdbc.core.PreparedStatementCreator
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
/**
/**
* @author liaozan
* @author liaozan
...
@@ -16,17 +15,17 @@ public interface StarrocksService<T> {
...
@@ -16,17 +15,17 @@ public interface StarrocksService<T> {
void
upsert
(
T
entity
);
void
upsert
(
T
entity
);
/**
/**
*
单个保存/更新,传入 columns 只会处理响应的 column
*
批量保存/更新
*/
*/
void
upsert
(
T
entity
,
List
<
String
>
columns
);
void
upsert
Batch
(
List
<
T
>
entityList
);
/**
/**
*
批量保存/更新
*
单个保存/更新,传入 columns 只会处理相应的 columns
*/
*/
void
upsert
Batch
(
List
<
T
>
entityList
);
void
upsert
(
T
entity
,
List
<
String
>
columns
);
/**
/**
* 批量保存/更新,传入 columns 只会处理
响应的 column
* 批量保存/更新,传入 columns 只会处理
相应的 columns
*/
*/
void
upsertBatch
(
List
<
T
>
entityList
,
List
<
String
>
columns
);
void
upsertBatch
(
List
<
T
>
entityList
,
List
<
String
>
columns
);
...
@@ -43,6 +42,6 @@ public interface StarrocksService<T> {
...
@@ -43,6 +42,6 @@ public interface StarrocksService<T> {
/**
/**
* 根据 sql 查询
* 根据 sql 查询
*/
*/
List
<
T
>
search
(
PreparedStatementCreator
creator
);
List
<
T
>
search
(
String
sql
,
Map
<
String
,
Object
>
params
);
}
}
starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/operation/StarrocksServiceImpl.java
View file @
b5b2baef
...
@@ -3,18 +3,16 @@ package com.schbrain.framework.autoconfigure.starrocks.operation;
...
@@ -3,18 +3,16 @@ package com.schbrain.framework.autoconfigure.starrocks.operation;
import
com.schbrain.common.util.ValidateUtils
;
import
com.schbrain.common.util.ValidateUtils
;
import
com.schbrain.framework.autoconfigure.starrocks.annotation.StarrocksTable
;
import
com.schbrain.framework.autoconfigure.starrocks.annotation.StarrocksTable
;
import
com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties
;
import
com.schbrain.framework.autoconfigure.starrocks.properties.StarrocksProperties
;
import
com.zaxxer.hikari.HikariConfig
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.jdbc.core.*
;
import
org.springframework.jdbc.core.BeanPropertyRowMapper
;
import
org.springframework.jdbc.core.namedparam.*
;
import
java.util.Collections
;
import
java.util.*
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
static
com
.
schbrain
.
framework
.
autoconfigure
.
starrocks
.
constants
.
StarrocksConstants
.
JDBC_URL_TEMPLATE
;
import
static
com
.
schbrain
.
framework
.
autoconfigure
.
starrocks
.
constants
.
StarrocksConstants
.
STREAM_LOAD_TEMPLATE
;
import
static
com
.
schbrain
.
framework
.
autoconfigure
.
starrocks
.
constants
.
StarrocksConstants
.
STREAM_LOAD_TEMPLATE
;
/**
/**
...
@@ -28,8 +26,10 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
...
@@ -28,8 +26,10 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
@Autowired
@Autowired
protected
StarrocksProperties
config
;
protected
StarrocksProperties
config
;
@Autowired
@Qualifier
(
"starrocksJdbcTemplate"
)
protected
NamedParameterJdbcTemplate
jdbcTemplate
;
protected
JdbcTemplate
jdbcTemplate
;
protected
StarrocksStreamLoadHandler
handler
;
protected
StarrocksStreamLoadHandler
handler
;
@SuppressWarnings
({
"unchecked"
,
"DataFlowIssue"
})
@SuppressWarnings
({
"unchecked"
,
"DataFlowIssue"
})
...
@@ -44,13 +44,13 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
...
@@ -44,13 +44,13 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
}
}
@Override
@Override
public
void
upsert
(
T
entity
,
List
<
String
>
columns
)
{
public
void
upsert
Batch
(
List
<
T
>
entityList
)
{
upsertBatch
(
List
.
of
(
ValidateUtils
.
notNull
(
entity
,
"entity不能为空"
)),
columns
);
upsertBatch
(
entityList
,
Collections
.
emptyList
()
);
}
}
@Override
@Override
public
void
upsert
Batch
(
List
<
T
>
entityList
)
{
public
void
upsert
(
T
entity
,
List
<
String
>
columns
)
{
upsertBatch
(
entityList
,
Collections
.
emptyList
()
);
upsertBatch
(
List
.
of
(
ValidateUtils
.
notNull
(
entity
,
"entity不能为空"
)),
columns
);
}
}
@Override
@Override
...
@@ -69,15 +69,21 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
...
@@ -69,15 +69,21 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
}
}
@Override
@Override
public
List
<
T
>
search
(
PreparedStatementCreator
callback
)
{
public
List
<
T
>
search
(
String
sql
,
Map
<
String
,
Object
>
params
)
{
return
jdbcTemplate
.
queryForStream
(
callback
,
rowMapper
).
collect
(
Collectors
.
toList
());
SqlParameterSource
parameterSource
;
if
(
params
.
isEmpty
())
{
parameterSource
=
EmptySqlParameterSource
.
INSTANCE
;
}
else
{
parameterSource
=
new
MapSqlParameterSource
(
params
);
}
// noinspection SqlSourceToSinkFlow 关掉idea sql注入检查
return
jdbcTemplate
.
queryForStream
(
sql
,
parameterSource
,
rowMapper
).
collect
(
Collectors
.
toList
());
}
}
@Override
@Override
public
void
afterPropertiesSet
()
{
public
void
afterPropertiesSet
()
{
StarrocksTable
annotation
=
ValidateUtils
.
notNull
(
entityClass
.
getAnnotation
(
StarrocksTable
.
class
),
StarrocksTable
.
class
.
getName
()
+
"不能为空"
);
StarrocksTable
annotation
=
ValidateUtils
.
notNull
(
entityClass
.
getAnnotation
(
StarrocksTable
.
class
),
StarrocksTable
.
class
.
getName
()
+
"不能为空"
);
this
.
handler
=
createHandler
(
annotation
.
value
());
this
.
handler
=
createHandler
(
annotation
.
value
());
this
.
jdbcTemplate
=
createJdbcTemplate
(
annotation
.
value
());
}
}
protected
StarrocksStreamLoadHandler
createHandler
(
String
tableName
)
{
protected
StarrocksStreamLoadHandler
createHandler
(
String
tableName
)
{
...
@@ -85,14 +91,4 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
...
@@ -85,14 +91,4 @@ public class StarrocksServiceImpl<T> implements StarrocksService<T>, Initializin
return
new
StarrocksStreamLoadHandler
(
streamLoadUrl
,
config
.
getUsername
(),
config
.
getPassword
());
return
new
StarrocksStreamLoadHandler
(
streamLoadUrl
,
config
.
getUsername
(),
config
.
getPassword
());
}
}
protected
JdbcTemplate
createJdbcTemplate
(
String
tableName
)
{
HikariConfig
hikariConfig
=
new
HikariConfig
();
hikariConfig
.
setPoolName
(
String
.
format
(
"%s:%s"
,
config
.
getDatabase
(),
tableName
));
hikariConfig
.
setDriverClassName
(
config
.
getDriverClassName
());
hikariConfig
.
setJdbcUrl
(
String
.
format
(
JDBC_URL_TEMPLATE
,
config
.
getHost
(),
config
.
getPort
(),
config
.
getDatabase
()));
hikariConfig
.
setUsername
(
config
.
getUsername
());
hikariConfig
.
setPassword
(
config
.
getPassword
());
return
new
JdbcTemplate
(
new
HikariDataSource
(
hikariConfig
));
}
}
}
support/schbrain-base-dao/src/main/java/com/schbrain/framework/dao/util/SQLUtil.java
View file @
b5b2baef
...
@@ -3,7 +3,6 @@ package com.schbrain.framework.dao.util;
...
@@ -3,7 +3,6 @@ package com.schbrain.framework.dao.util;
import
com.schbrain.common.util.StreamUtils
;
import
com.schbrain.common.util.StreamUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.util.ClassUtils
;
import
java.util.*
;
import
java.util.*
;
...
@@ -14,28 +13,33 @@ import java.util.*;
...
@@ -14,28 +13,33 @@ import java.util.*;
*/
*/
public
class
SQLUtil
{
public
class
SQLUtil
{
private
static
final
Set
<
Class
<?>>
NUMBER_TYPES
=
Set
.
of
(
Integer
.
class
,
Long
.
class
,
Short
.
class
,
Byte
.
class
,
Double
.
class
,
Float
.
class
);
public
static
<
T
>
String
buildInClause
(
String
columnName
,
Class
<
T
>
valueType
,
List
<
T
>
values
)
{
public
static
<
T
>
String
buildInClause
(
String
columnName
,
Class
<
T
>
valueType
,
List
<
T
>
values
)
{
Set
<
T
>
valueList
=
StreamUtils
.
filterToSet
(
values
,
Objects:
:
nonNull
);
Set
<
T
>
valueList
=
StreamUtils
.
filterToSet
(
values
,
Objects:
:
nonNull
);
if
(
CollectionUtils
.
isEmpty
(
valueList
))
{
if
(
CollectionUtils
.
isEmpty
(
valueList
))
{
throw
new
IllegalArgumentException
(
"Value list can not be empty."
);
throw
new
IllegalArgumentException
(
"Value list can not be empty."
);
}
}
StringBuilder
builder
=
new
StringBuilder
(
" "
);
StringBuilder
builder
=
new
StringBuilder
(
columnName
).
append
(
" in ("
);
builder
.
append
(
columnName
).
append
(
" in ("
);
if
(
isNumberType
(
valueType
))
{
if
(
ClassUtils
.
isPrimitiveWrapper
(
valueType
))
{
builder
.
append
(
StreamUtils
.
join
(
valueList
));
builder
.
append
(
StreamUtils
.
join
(
valueList
)).
append
(
")"
);
}
else
{
}
else
{
builder
.
append
(
StreamUtils
.
join
(
valueList
,
SQLUtil:
:
escapeSql
))
.
append
(
")"
)
;
builder
.
append
(
StreamUtils
.
join
(
valueList
,
SQLUtil:
:
escapeSql
));
}
}
builder
.
append
(
"
"
);
builder
.
append
(
"
)
"
);
return
builder
.
toString
(
);
return
StringUtils
.
wrap
(
builder
.
toString
(),
" "
);
}
}
private
static
String
escapeSql
(
Object
value
)
{
private
static
String
escapeSql
(
Object
value
)
{
if
(
value
instanceof
String
)
{
if
(
value
instanceof
String
)
{
return
StringUtils
.
replace
((
String
)
value
,
"'"
,
"'
'"
);
return
StringUtils
.
wrap
((
String
)
value
,
"
'"
);
}
else
{
}
else
{
return
String
.
valueOf
(
value
);
return
String
.
valueOf
(
value
);
}
}
}
}
private
static
boolean
isNumberType
(
Class
<?>
valueType
)
{
return
NUMBER_TYPES
.
contains
(
valueType
);
}
}
}
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