Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dict
Overview
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈文顺
dict
Commits
e78193c1
Commit
e78193c1
authored
Nov 27, 2018
by
陈文顺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据字典完成
parent
4f38f52c
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
342 additions
and
47 deletions
+342
-47
demo-service/src/main/java/com/freemud/demo/cache/DictRedisCache.java
+1
-0
demo-service/src/main/java/com/freemud/demo/cache/DictRedisHashCache.java
+57
-0
demo-service/src/main/java/com/freemud/demo/service/IDictTypeService.java
+4
-1
demo-service/src/main/java/com/freemud/demo/service/impl/DictTypeServiceImpl.java
+101
-38
demo-service/src/main/java/com/freemud/demo/util/RedisCache.java
+13
-3
demo-service/src/main/java/com/freemud/demo/util/RedisHashCache.java
+104
-0
springboot-demo/pom.xml
+5
-5
springboot-demo/src/main/java/com/freemud/springbootdemo/context/StartupRunner.java
+15
-0
springboot-demo/src/main/java/com/freemud/springbootdemo/context/TaskRunner.java
+23
-0
springboot-demo/src/main/java/com/freemud/springbootdemo/controller/DictController.java
+19
-0
No files found.
demo-service/src/main/java/com/freemud/demo/cache/DictRedisCache.java
View file @
e78193c1
...
...
@@ -10,6 +10,7 @@ import com.freemud.demo.util.RedisCache;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
tk.mybatis.mapper.entity.Example
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
...
...
demo-service/src/main/java/com/freemud/demo/cache/DictRedisHashCache.java
0 → 100644
View file @
e78193c1
package
com
.
freemud
.
demo
.
cache
;
import
com.freemud.demo.dto.DictDataDto
;
import
com.freemud.demo.mapper.TDictDataMapper
;
import
com.freemud.demo.mapper.TDictTypeMapper
;
import
com.freemud.demo.model.TDictData
;
import
com.freemud.demo.util.RedisHashCache
;
import
com.google.common.collect.Lists
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
/**
* Created by chenwenshun on 2018/11/27.
*/
@Component
public
class
DictRedisHashCache
extends
RedisHashCache
<
DictDataDto
>{
@Autowired
TDictTypeMapper
dictTypeMapper
;
@Autowired
TDictDataMapper
dictDataMapper
;
@Override
public
Map
<
String
,
DictDataDto
>
searchDB
(
Object
key
)
{
String
typeCd
=
key
.
toString
();
List
<
TDictData
>
dictDataList
=
dictDataMapper
.
loadDatasByTypeCd
(
typeCd
);
if
(
dictDataList
.
isEmpty
()){
return
null
;
}
List
<
DictDataDto
>
dictDataDtos
=
dictDataList
.
stream
()
.
map
(
d
->
{
DictDataDto
dictDataDto
=
new
DictDataDto
();
BeanUtils
.
copyProperties
(
d
,
dictDataDto
);
return
dictDataDto
;
})
.
collect
(
Collectors
.
toList
());
return
dictDataDtos
.
stream
()
.
collect
(
Collectors
.
toMap
(
d
->
d
.
getDictCd
(),
d
->
d
));
}
@Override
public
int
getExprie
()
{
return
24
;
}
}
demo-service/src/main/java/com/freemud/demo/service/IDictTypeService.java
View file @
e78193c1
...
...
@@ -30,7 +30,10 @@ import java.util.Map;
DictDataDto
loadDictDataByCd
(
String
dictTypeCd
,
String
dictCd
,
boolean
onlyEnable
);
/** 保存 */
TDictType
saveDict
(
DictTypeDto
dto
);
void
saveDict
(
DictTypeDto
dto
);
void
edit
(
DictTypeDto
dto
);
/** 批量保存字典列表 */
void
saveDatas
(
List
<
DictDataDto
>
dictDataDtos
);
...
...
demo-service/src/main/java/com/freemud/demo/service/impl/DictTypeServiceImpl.java
View file @
e78193c1
package
com
.
freemud
.
demo
.
service
.
impl
;
import
com.freemud.demo.cache.DictRedisHashCache
;
import
com.freemud.demo.dto.DictDataDto
;
import
com.freemud.demo.dto.DictTypeDto
;
import
com.freemud.demo.dto.PageData
;
...
...
@@ -16,7 +17,10 @@ import com.google.common.collect.Lists;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
tk.mybatis.mapper.entity.Example
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
...
...
@@ -36,6 +40,9 @@ public class DictTypeServiceImpl implements IDictTypeService{
@Autowired
DictRedisCache
dictRedisCache
;
@Autowired
DictRedisHashCache
dictRedisHashCache
;
@Override
public
TDictType
loadDictById
(
long
dictTypeId
)
{
return
null
;
...
...
@@ -48,25 +55,79 @@ public class DictTypeServiceImpl implements IDictTypeService{
@Override
public
DictDataDto
loadDictDataByCd
(
String
dictTypeCd
,
String
dictCd
,
boolean
onlyEnable
)
{
TDictData
dictData
=
this
.
dictDataMapper
.
getDictDataByCd
(
dictTypeCd
,
dictCd
);
if
(
dictData
==
null
){
return
null
;
}
//
TDictData dictData = this.dictDataMapper.getDictDataByCd(dictTypeCd, dictCd);
//
if (dictData == null){
//
return null;
//
}
DictDataDto
dictDataDto
=
new
DictDataDto
();
BeanUtils
.
copyProperties
(
dictData
,
dictDataDto
);
if
(
onlyEnable
){
if
(!
dictData
.
getEnableFlg
()){
return
null
;
}
}
// BeanUtils.copyProperties(dictData, dictDataDto);
// if (onlyEnable ){
// if (!dictData.getEnableFlg()){
// return null;
// }
// }
dictDataDto
=
dictRedisHashCache
.
getValue
(
dictTypeCd
,
dictCd
);
return
dictDataDto
;
}
@Override
public
TDictType
saveDict
(
DictTypeDto
dto
)
{
return
null
;
public
void
saveDict
(
DictTypeDto
dto
)
{
TDictType
dictTypeEntity
=
new
TDictType
();
BeanUtils
.
copyProperties
(
dto
,
dictTypeEntity
);
dictTypeEntity
.
setCreatedDate
(
new
Date
());
dictTypeEntity
.
setUpdatedDate
(
new
Date
());
this
.
dictTypeMapper
.
insertUseGeneratedKeys
(
dictTypeEntity
);
// int r = this.dictTypeMapper.insertSelective(dictTypeEntity);
if
(
dto
.
getDictDataDtos
()==
null
||
dto
.
getDictDataDtos
().
isEmpty
()){
return
;
}
List
<
TDictData
>
dictDataList
=
dto
.
getDictDataDtos
().
stream
()
.
map
(
d
->
{
TDictData
dictDataEntity
=
new
TDictData
();
BeanUtils
.
copyProperties
(
d
,
dictDataEntity
);
dictDataEntity
.
setDictTypeId
(
dictTypeEntity
.
getId
());
return
dictDataEntity
;
})
.
collect
(
Collectors
.
toList
());
this
.
dictDataMapper
.
insertList
(
dictDataList
);
}
@Override
public
void
edit
(
DictTypeDto
dto
){
TDictType
dictType
=
this
.
dictTypeMapper
.
selectByPrimaryKey
(
dto
.
getId
());
BeanUtils
.
copyProperties
(
dto
,
dictType
);
dictType
.
setUpdatedDate
(
new
Date
());
dictTypeMapper
.
updateByPrimaryKey
(
dictType
);
if
(
dto
.
getDictDataDtos
()==
null
||
dto
.
getDictDataDtos
().
isEmpty
()){
return
;
}
Example
exampleDel
=
new
Example
(
TDictData
.
class
);
exampleDel
.
createCriteria
()
.
andEqualTo
(
"dictTypeId"
,
dto
.
getId
());
dictDataMapper
.
deleteByExample
(
exampleDel
);
List
<
TDictData
>
dictDataList
=
dto
.
getDictDataDtos
().
stream
()
.
map
(
d
->
{
TDictData
dictDataEntity
=
new
TDictData
();
BeanUtils
.
copyProperties
(
d
,
dictDataEntity
);
dictDataEntity
.
setDictTypeId
(
dto
.
getId
());
return
dictDataEntity
;
})
.
collect
(
Collectors
.
toList
());
this
.
dictDataMapper
.
insertList
(
dictDataList
);
dictRedisCache
.
delCache
(
dto
.
getId
());
dictRedisHashCache
.
delCache
(
dto
.
getDictTypeCd
());
}
@Override
...
...
@@ -95,7 +156,7 @@ public class DictTypeServiceImpl implements IDictTypeService{
List
<
DictTypeDto
>
list
=
page
.
stream
()
.
map
(
id
->
{
DictTypeDto
dictTypeDto
=
this
.
dictRedisCache
.
getCache
(
id
,
DictTypeDto
.
class
);
DictTypeDto
dictTypeDto
=
this
.
dictRedisCache
.
getCache
(
id
);
return
dictTypeDto
;
})
.
collect
(
Collectors
.
toList
());
...
...
@@ -106,31 +167,33 @@ public class DictTypeServiceImpl implements IDictTypeService{
@Override
public
List
<
DictDataDto
>
loadDatasByTypeCd
(
String
dictTypeCd
,
boolean
onlyEnable
)
{
List
<
DictDataDto
>
dictDataDtos
=
Lists
.
newArrayList
();
List
<
TDictData
>
dictDataList
=
this
.
dictDataMapper
.
loadDatasByTypeCd
(
dictTypeCd
);
if
(
dictDataList
.
isEmpty
()){
return
dictDataDtos
;
}
if
(
onlyEnable
){
dictDataDtos
=
dictDataList
.
stream
()
.
filter
(
TDictData:
:
getEnableFlg
)
.
map
(
d
->
{
DictDataDto
dictDataDto
=
new
DictDataDto
();
BeanUtils
.
copyProperties
(
d
,
dictDataDto
);
return
dictDataDto
;
})
.
collect
(
Collectors
.
toList
());
}
else
{
dictDataDtos
=
dictDataList
.
stream
()
.
map
(
d
->
{
DictDataDto
dictDataDto
=
new
DictDataDto
();
BeanUtils
.
copyProperties
(
d
,
dictDataDto
);
return
dictDataDto
;
})
.
collect
(
Collectors
.
toList
());
}
// List<TDictData> dictDataList = this.dictDataMapper.loadDatasByTypeCd(dictTypeCd);
// if (dictDataList.isEmpty()){
// return dictDataDtos;
// }
// if (onlyEnable){
// dictDataDtos = dictDataList.stream()
// .filter(TDictData::getEnableFlg)
// .map(d -> {
// DictDataDto dictDataDto = new DictDataDto();
// BeanUtils.copyProperties(d, dictDataDto);
// return dictDataDto;
// })
// .collect(Collectors.toList());
//
// }else {
// dictDataDtos = dictDataList.stream()
// .map(d -> {
// DictDataDto dictDataDto = new DictDataDto();
// BeanUtils.copyProperties(d, dictDataDto);
// return dictDataDto;
// })
// .collect(Collectors.toList());
// }
Map
<
String
,
DictDataDto
>
map
=
dictRedisHashCache
.
getAllValue
(
dictTypeCd
);
dictDataDtos
=
new
ArrayList
<>(
map
.
values
());
return
dictDataDtos
;
}
...
...
demo-service/src/main/java/com/freemud/demo/util/RedisCache.java
View file @
e78193c1
...
...
@@ -2,6 +2,8 @@ package com.freemud.demo.util;
import
org.springframework.beans.factory.annotation.Autowired
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.util.concurrent.TimeUnit
;
/**
...
...
@@ -13,14 +15,22 @@ public abstract class RedisCache<T> {
@Autowired
protected
RedisUitl
redisUitl
;
public
Class
<
T
>
getClazz
(){
Type
type
=
getClass
().
getGenericSuperclass
();
Type
trueType
=
((
ParameterizedType
)
type
).
getActualTypeArguments
()[
0
];
return
(
Class
<
T
>)
trueType
;
}
/**
* 获取缓存
* @param key
* @param clazz
* @return
*/
public
T
getCache
(
Object
key
,
Class
<
T
>
clazz
){
String
redisKey
=
getRedisKey
(
clazz
.
getName
(),
key
);
public
T
getCache
(
Object
key
){
String
redisKey
=
getRedisKey
(
getClazz
()
.
getName
(),
key
);
T
value
=
redisUitl
.
get
(
redisKey
);
if
(
value
!=
null
){
return
value
;
...
...
@@ -50,7 +60,7 @@ public abstract class RedisCache<T> {
* 删除缓存
*/
public
void
delCache
(
Object
key
){
redisUitl
.
getRedisTemplate
().
delete
(
key
);
redisUitl
.
getRedisTemplate
().
delete
(
getRedisKey
(
getClazz
().
getName
(),
key
)
);
}
...
...
demo-service/src/main/java/com/freemud/demo/util/RedisHashCache.java
0 → 100644
View file @
e78193c1
package
com
.
freemud
.
demo
.
util
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
/**
* Created by chenwenshun on 2018/9/4.
*/
public
abstract
class
RedisHashCache
<
T
>
{
@Autowired
protected
RedisTemplate
redisTemplate
;
public
Class
<
T
>
getClazz
(){
Type
type
=
getClass
().
getGenericSuperclass
();
Type
trueType
=
((
ParameterizedType
)
type
).
getActualTypeArguments
()[
0
];
return
(
Class
<
T
>)
trueType
;
}
public
<
T
>
T
getValue
(
Object
key
,
Object
filed
){
T
result
=
(
T
)
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
get
(
filed
);
if
(
result
!=
null
){
return
result
;
}
boolean
hasKey
=
redisTemplate
.
hasKey
(
key
);
if
(
hasKey
){
return
null
;
}
Map
map
=
this
.
searchDB
(
key
);
if
(
map
==
null
||
map
.
size
()
==
0
){
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
put
(
""
,
""
);
}
else
{
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
putAll
(
map
);
}
redisTemplate
.
expire
(
getRedisKey
(
key
),
getExprie
(),
TimeUnit
.
HOURS
);
result
=
(
T
)
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
get
(
filed
);
return
result
;
}
public
<
T
>
Map
<
String
,
T
>
getAllValue
(
Object
key
){
Map
<
String
,
T
>
result
=
(
Map
<
String
,
T
>)
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
entries
();
if
(
result
!=
null
&&
result
.
size
()
>
0
){
return
result
;
}
boolean
hasKey
=
redisTemplate
.
hasKey
(
key
);
if
(
hasKey
){
return
null
;
}
Map
map
=
this
.
searchDB
(
key
);
if
(
map
==
null
||
map
.
size
()
==
0
){
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
put
(
""
,
""
);
}
else
{
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
putAll
(
map
);
}
redisTemplate
.
expire
(
getRedisKey
(
key
),
getExprie
(),
TimeUnit
.
HOURS
);
result
=
(
Map
<
String
,
T
>)
redisTemplate
.
boundHashOps
(
getRedisKey
(
key
)).
entries
();
return
result
;
}
/**
* 删除缓存
*/
public
void
delCache
(
Object
key
){
redisTemplate
.
delete
(
getRedisKey
(
key
));
}
public
abstract
Map
<
String
,
T
>
searchDB
(
Object
key
);
public
abstract
int
getExprie
();
private
String
getRedisKey
(
Object
key
){
return
this
.
getClazz
().
getName
()+
"_HASH_"
+
key
.
toString
();
}
}
springboot-demo/pom.xml
View file @
e78193c1
...
...
@@ -51,11 +51,11 @@
<version>
1.0-SNAPSHOT
</version>
</dependency>
<
dependency
>
<
groupId>
cn.freemud.commons
</groupId
>
<
artifactId>
commons-base
</artifactId
>
<
version>
1.4.9-release
</version
>
<
/dependency
>
<
!--<dependency>--
>
<
!--<groupId>cn.freemud.commons</groupId>--
>
<
!--<artifactId>commons-base</artifactId>--
>
<
!--<version>1.4.9-release</version>--
>
<
!--</dependency>--
>
...
...
springboot-demo/src/main/java/com/freemud/springbootdemo/context/StartupRunner.java
0 → 100644
View file @
e78193c1
package
com
.
freemud
.
springbootdemo
.
context
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.stereotype.Component
;
/**
* Created by chenwenshun on 2018/11/19.
*/
@Component
public
class
StartupRunner
implements
CommandLineRunner
{
@Override
public
void
run
(
String
...
strings
)
throws
Exception
{
System
.
out
.
println
(
strings
);
}
}
springboot-demo/src/main/java/com/freemud/springbootdemo/context/TaskRunner.java
0 → 100644
View file @
e78193c1
package
com
.
freemud
.
springbootdemo
.
context
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.Ordered
;
/**
* Created by chenwenshun on 2018/11/19.
*/
@Configuration
public
class
TaskRunner
implements
ApplicationRunner
,
Ordered
{
@Override
public
void
run
(
ApplicationArguments
applicationArguments
)
throws
Exception
{
System
.
out
.
println
(
applicationArguments
);
}
@Override
public
int
getOrder
()
{
return
0
;
}
}
springboot-demo/src/main/java/com/freemud/springbootdemo/controller/DictController.java
View file @
e78193c1
...
...
@@ -2,6 +2,7 @@ package com.freemud.springbootdemo.controller;
import
com.freemud.demo.dto.*
;
import
com.freemud.demo.service.IDictTypeService
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
...
...
@@ -18,6 +19,7 @@ public class DictController {
@Autowired
IDictTypeService
dictTypeService
;
@ApiOperation
(
"分页查询列表"
)
@PostMapping
(
"/page"
)
public
ResDto
page
(
@RequestBody
PageReq
pageReq
){
PageData
<
DictTypeDto
>
pageData
=
dictTypeService
.
findPageDict
(
pageReq
);
...
...
@@ -26,6 +28,7 @@ public class DictController {
}
@ApiOperation
(
"typeCd获取字典数据"
)
@GetMapping
(
value
=
"/dictDatas/{dictTypeCd}"
)
public
ResDto
dictDatas
(
@PathVariable
String
dictTypeCd
){
List
<
DictDataDto
>
list
=
this
.
dictTypeService
.
loadDatasByTypeCd
(
dictTypeCd
,
true
);
...
...
@@ -33,6 +36,7 @@ public class DictController {
}
@ApiOperation
(
"typeCd & dataCd获取字典数据"
)
@GetMapping
(
"/dictData/{dictTypeCd}/{dictCd}"
)
public
ResDto
<
DictDataDto
>
dictData
(
@PathVariable
String
dictTypeCd
,
@PathVariable
String
dictCd
){
DictDataDto
dictDataDto
=
this
.
dictTypeService
.
loadDictDataByCd
(
dictTypeCd
,
dictCd
,
true
);
...
...
@@ -40,4 +44,19 @@ public class DictController {
}
@ApiOperation
(
"新增字典信息"
)
@PostMapping
(
"/save"
)
public
ResDto
save
(
@RequestBody
DictTypeDto
dict
){
this
.
dictTypeService
.
saveDict
(
dict
);
return
new
ResDto
();
}
@ApiOperation
(
"修改字典信息"
)
@PostMapping
(
"/edit"
)
public
ResDto
edit
(
@RequestBody
DictTypeDto
dict
){
this
.
dictTypeService
.
edit
(
dict
);
return
new
ResDto
();
}
}
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