Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
W
workflow
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
2
议题
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄夏豪
workflow
Commits
cc953f6b
提交
cc953f6b
authored
4月 01, 2021
作者:
ww1xhqc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[数据模型] 新增缓存配置。
上级
de7de381
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
187 行增加
和
98 行删除
+187
-98
MyCacheConfig.java
...m/tykj/workflowcore/model_layer/config/MyCacheConfig.java
+94
-74
ModelController.java
.../workflowcore/model_layer/controller/ModelController.java
+13
-5
ModelService.java
...m/tykj/workflowcore/model_layer/service/ModelService.java
+11
-7
ModelImpl.java
...tykj/workflowcore/model_layer/service/impl/ModelImpl.java
+5
-4
WorkflowCoreApplicationTests.java
...a/com/tykj/workflowcore/WorkflowCoreApplicationTests.java
+64
-8
没有找到文件。
src/main/java/com/tykj/workflowcore/model_layer/config/MyCacheConfig.java
浏览文件 @
cc953f6b
//package com.tykj.workflowcore.model_layer.config;
package
com
.
tykj
.
workflowcore
.
model_layer
.
config
;
//
//
//import com.github.benmanes.caffeine.cache.CacheLoader;
import
com.github.benmanes.caffeine.cache.CacheLoader
;
//import com.github.benmanes.caffeine.cache.Caffeine;
import
com.github.benmanes.caffeine.cache.Caffeine
;
//import org.springframework.cache.CacheManager;
import
org.checkerframework.checker.nullness.qual.NonNull
;
//import org.springframework.cache.annotation.EnableCaching;
import
org.checkerframework.checker.nullness.qual.Nullable
;
//import org.springframework.cache.caffeine.CaffeineCacheManager;
import
org.hibernate.SessionFactory
;
//import org.springframework.cache.concurrent.ConcurrentMapCache;
import
org.hibernate.boot.MetadataSources
;
//import org.springframework.cache.support.SimpleCacheManager;
import
org.hibernate.boot.registry.StandardServiceRegistry
;
//import org.springframework.context.annotation.Bean;
import
org.springframework.beans.factory.annotation.Autowired
;
//import org.springframework.context.annotation.Configuration;
import
org.springframework.cache.Cache
;
//
import
org.springframework.cache.CacheManager
;
//
import
org.springframework.cache.annotation.EnableCaching
;
//import java.util.Arrays;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
//import java.util.Collections;
import
org.springframework.cache.concurrent.ConcurrentMapCache
;
//import java.util.concurrent.TimeUnit;
import
org.springframework.cache.support.SimpleCacheManager
;
//
import
org.springframework.context.annotation.Bean
;
//
import
org.springframework.context.annotation.Configuration
;
///**
// * @Author WWW
// * @Description
import
javax.persistence.EntityManagerFactory
;
// * @Date 10:47 2021/3/26
import
java.util.Arrays
;
// * @return
import
java.util.Collections
;
// **/
import
java.util.concurrent.TimeUnit
;
//@Configuration
//@EnableCaching
//public class MyCacheConfig {
/**
//
* @Author WWW
// @Bean
* @Description
// public CacheManager cacheManager() {
* @Date 10:47 2021/3/26
// /*
* @return
// api + cache spring cache + hashmap
**/
// myCode + redis
@Configuration
// spring cache + caffeine
@EnableCaching
// */
public
class
MyCacheConfig
{
@Autowired
EntityManagerFactory
entityManagerFactory
;
@Bean
public
CacheManager
cacheManager
()
{
/*
api + cache spring cache + hashmap
myCode + redis
spring cache + caffeine
*/
// SimpleCacheManager cacheManager = new SimpleCacheManager();
// SimpleCacheManager cacheManager = new SimpleCacheManager();
//
//// CaffeineCacheManager cacheManager = new CaffeineCacheManager();
CaffeineCacheManager
cacheManager
=
new
CaffeineCacheManager
();
//// Caffeine caffeine = Caffeine.newBuilder()
Caffeine
caffeine
=
Caffeine
.
newBuilder
()
//// /* cache的初始容量值 */
/* cache的初始容量值 */
//// .initialCapacity(150)
.
initialCapacity
(
150
)
//// //maximumSize用来控制cache的最大缓存数量,maximumSize和maximumWeight(最大权重)不可以同时使用,
//maximumSize用来控制cache的最大缓存数量,maximumSize和maximumWeight(最大权重)不可以同时使用,
//// .maximumSize(1000);
.
maximumSize
(
1000
)
//// //最后一次写入或者访问后过久过期
//最后一次写入或者访问后过久过期
////// .expireAfterAccess(10, TimeUnit.SECONDS)
.
expireAfterAccess
(
30
,
TimeUnit
.
DAYS
)
////// //创建或更新之后多久刷新,需要设置cacheLoader
// //创建或更新之后多久刷新,需要设置cacheLoader
////// .refreshAfterWrite(5, TimeUnit.SECONDS);
.
refreshAfterWrite
(
5
,
TimeUnit
.
SECONDS
);
//// cacheManager.setCaffeine(caffeine);
cacheManager
.
setCaffeine
(
caffeine
);
//// cacheManager.setCacheLoader(cacheLoader());
cacheManager
.
setCacheLoader
(
cacheLoader
());
//// /* 根据名字可以创建多个cache,但是多个cache使用相同的策略 */
/* 根据名字可以创建多个cache,但是多个cache使用相同的策略 */
//// cacheManager.setCacheNames(Arrays
cacheManager
.
setCacheNames
(
Arrays
.
asList
(
"tableInfos"
));
//// .asList("tableInfos"));
//是否允许值为空
//// //是否允许值为空
cacheManager
.
setAllowNullValues
(
false
);
//// cacheManager.setAllowNullValues(false);
/* cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("tableInfos"))); */
// cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("tableInfos")));
return
cacheManager
;
// return cacheManager;
}
// }
//
//
@Bean
// @Bean
public
CacheLoader
cacheLoader
(){
// public CacheLoader<Object, Object> cacheLoader() {
return
new
CacheLoader
()
{
// return new CacheLoader<Object, Object>() {
@Nullable
// @Override
@Override
// public Object load(Object key) {
public
Object
load
(
@NonNull
Object
o
)
{
// return null;
return
null
;
// }
}
// // 重写这个方法将oldValue值返回回去,进而刷新缓存
// @Override
@Nullable
// public Object reload(Object key, Object oldValue) {
@Override
// return oldValue;
public
Object
reload
(
@NonNull
Object
key
,
@NonNull
Object
oldValue
)
{
// }
return
oldValue
;
// };
}
// }
};
//}
}
@Bean
public
MetadataSources
metadataSources
(){
SessionFactory
sessionFactory
=
entityManagerFactory
.
unwrap
(
SessionFactory
.
class
);
StandardServiceRegistry
serviceRegistry
=
sessionFactory
.
getSessionFactoryOptions
().
getServiceRegistry
();
return
new
MetadataSources
(
serviceRegistry
);
}
}
src/main/java/com/tykj/workflowcore/model_layer/controller/ModelController.java
浏览文件 @
cc953f6b
package
com
.
tykj
.
workflowcore
.
model_layer
.
controller
;
package
com
.
tykj
.
workflowcore
.
model_layer
.
controller
;
import
com.tykj.workflowcore.base.result.ApiException
;
import
com.tykj.workflowcore.base.result.ResultUtil
;
import
com.tykj.workflowcore.base.result.ResultUtil
;
import
com.tykj.workflowcore.model_layer.entity.vo.*
;
import
com.tykj.workflowcore.model_layer.entity.vo.*
;
import
com.tykj.workflowcore.model_layer.entity.TableInfo
;
import
com.tykj.workflowcore.model_layer.entity.TableInfo
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
com.tykj.workflowcore.model_layer.utils.ClassTypeLength
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
...
@@ -17,7 +16,7 @@ import org.springframework.http.ResponseEntity;
...
@@ -17,7 +16,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -28,6 +27,7 @@ import java.util.Map;
...
@@ -28,6 +27,7 @@ import java.util.Map;
* @Date 2021/2/26 13:35
* @Date 2021/2/26 13:35
* @Version 1.0
* @Version 1.0
*/
*/
@Slf4j
@RestController
@RestController
@RequestMapping
(
"/model"
)
@RequestMapping
(
"/model"
)
@Api
(
tags
=
"数据模型层接口"
)
@Api
(
tags
=
"数据模型层接口"
)
...
@@ -50,7 +50,7 @@ public class ModelController {
...
@@ -50,7 +50,7 @@ public class ModelController {
try
{
try
{
tableInfos
=
modelService
.
listAllEntities
(
searchTableInfoVo
);
tableInfos
=
modelService
.
listAllEntities
(
searchTableInfoVo
);
}
catch
(
SQLException
throwables
)
{
}
catch
(
SQLException
throwables
)
{
return
ResultUtil
.
failed
(
"
因为某些神秘原因,
数据查询失败!"
);
return
ResultUtil
.
failed
(
"数据查询失败!"
);
}
}
return
ResultUtil
.
success
(
tableInfos
,
"数据查询成功!"
);
return
ResultUtil
.
success
(
tableInfos
,
"数据查询成功!"
);
...
@@ -85,7 +85,12 @@ public class ModelController {
...
@@ -85,7 +85,12 @@ public class ModelController {
return
ResultUtil
.
failed
(
"表已经存在!"
);
return
ResultUtil
.
failed
(
"表已经存在!"
);
}
}
}
}
long
start
=
System
.
currentTimeMillis
();
modelService
.
newTable
(
tableVO
);
modelService
.
newTable
(
tableVO
);
long
end
=
System
.
currentTimeMillis
();
log
.
warn
(
"创建总用时为:{}秒"
,(
end
-
start
)/
1000.0
);
return
ResultUtil
.
success
(
""
,
"新建成功!"
);
return
ResultUtil
.
success
(
""
,
"新建成功!"
);
}
}
...
@@ -105,7 +110,7 @@ public class ModelController {
...
@@ -105,7 +110,7 @@ public class ModelController {
if
(
i
==
0
){
if
(
i
==
0
){
return
ResultUtil
.
success
(
""
,
"数据插入成功!"
);
return
ResultUtil
.
success
(
""
,
"数据插入成功!"
);
}
}
return
ResultUtil
.
success
(
""
,
"模型类型不支持插入数据!"
);
return
ResultUtil
.
failed
(
"模型类型不支持插入数据!"
);
}
}
...
@@ -146,8 +151,11 @@ public class ModelController {
...
@@ -146,8 +151,11 @@ public class ModelController {
@ApiOperation
(
"编辑操作"
)
@ApiOperation
(
"编辑操作"
)
@PutMapping
(
"/update"
)
@PutMapping
(
"/update"
)
public
ResponseEntity
updateTable
(
@RequestBody
UpdateTableInfoVO
updateTableInfoVO
){
public
ResponseEntity
updateTable
(
@RequestBody
UpdateTableInfoVO
updateTableInfoVO
){
long
start
=
System
.
currentTimeMillis
();
int
i
=
modelService
.
updateTable
(
updateTableInfoVO
);
int
i
=
modelService
.
updateTable
(
updateTableInfoVO
);
if
(
i
==
1
){
if
(
i
==
1
){
long
end
=
System
.
currentTimeMillis
();
log
.
warn
(
"创建总用时为:{}秒"
,(
end
-
start
)/
1000.0
);
return
ResultUtil
.
success
(
""
,
"修改成功!"
);
return
ResultUtil
.
success
(
""
,
"修改成功!"
);
}
}
return
ResultUtil
.
success
(
"业务类型不支持修改!"
);
return
ResultUtil
.
success
(
"业务类型不支持修改!"
);
...
...
src/main/java/com/tykj/workflowcore/model_layer/service/ModelService.java
浏览文件 @
cc953f6b
...
@@ -4,6 +4,7 @@ package com.tykj.workflowcore.model_layer.service;
...
@@ -4,6 +4,7 @@ package com.tykj.workflowcore.model_layer.service;
import
com.tykj.workflowcore.model_layer.entity.ColumnInfo
;
import
com.tykj.workflowcore.model_layer.entity.ColumnInfo
;
import
com.tykj.workflowcore.model_layer.entity.TableInfo
;
import
com.tykj.workflowcore.model_layer.entity.TableInfo
;
import
com.tykj.workflowcore.model_layer.entity.vo.*
;
import
com.tykj.workflowcore.model_layer.entity.vo.*
;
import
org.springframework.cache.annotation.CacheConfig
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.CachePut
;
import
org.springframework.cache.annotation.CachePut
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.cache.annotation.Cacheable
;
...
@@ -31,7 +32,7 @@ public interface ModelService {
...
@@ -31,7 +32,7 @@ public interface ModelService {
* @return
* @return
* @throws SQLException
* @throws SQLException
*/
*/
// @Cacheable(cacheNames = "tableInfos"
)
@Cacheable
(
cacheNames
=
"tableInfos"
,
sync
=
true
)
Page
<
TableInfo
>
listAllEntities
(
SearchTableInfoVo
searchTableInfoVo
)
throws
SQLException
;
Page
<
TableInfo
>
listAllEntities
(
SearchTableInfoVo
searchTableInfoVo
)
throws
SQLException
;
...
@@ -49,6 +50,7 @@ public interface ModelService {
...
@@ -49,6 +50,7 @@ public interface ModelService {
* @param searchColumnInfoVo
* @param searchColumnInfoVo
* @return
* @return
*/
*/
@Cacheable
(
cacheNames
=
"tableInfos"
)
List
<
ColumnInfo
>
showModelFields
(
SearchColumnInfoVo
searchColumnInfoVo
);
List
<
ColumnInfo
>
showModelFields
(
SearchColumnInfoVo
searchColumnInfoVo
);
...
@@ -58,8 +60,9 @@ public interface ModelService {
...
@@ -58,8 +60,9 @@ public interface ModelService {
* @param tableVO
* @param tableVO
* @return
* @return
*/
*/
// @CachePut(cacheNames = "tableInfos")
@CachePut
(
cacheNames
=
"tableInfos"
)
TableVO
newTable
(
TableVO
tableVO
);
@CacheEvict
(
cacheNames
=
"tableInfos"
,
allEntries
=
true
)
TableInfo
newTable
(
TableVO
tableVO
);
/**
/**
* 根据表名插入数据
* 根据表名插入数据
...
@@ -94,10 +97,12 @@ public interface ModelService {
...
@@ -94,10 +97,12 @@ public interface ModelService {
/**
/**
* 修改表
* 修改表
* @param updateTableInfoVO
* @param updateTableInfoVO
* @return
* @return
*/
*/
// @CachePut(cacheNames = "tableInfos")
@CachePut
(
cacheNames
=
"tableInfos"
)
@CacheEvict
(
cacheNames
=
"tableInfos"
,
allEntries
=
true
)
int
updateTable
(
UpdateTableInfoVO
updateTableInfoVO
);
int
updateTable
(
UpdateTableInfoVO
updateTableInfoVO
);
...
@@ -106,8 +111,7 @@ public interface ModelService {
...
@@ -106,8 +111,7 @@ public interface ModelService {
* @param delTableVO
* @param delTableVO
* @return
* @return
*/
*/
// @CacheEvict(cacheNames = "tableInfos")
@CacheEvict
(
cacheNames
=
"tableInfos"
,
allEntries
=
true
)
// @Transactional
int
delTable
(
DelTableVO
delTableVO
);
int
delTable
(
DelTableVO
delTableVO
);
...
...
src/main/java/com/tykj/workflowcore/model_layer/service/impl/ModelImpl.java
浏览文件 @
cc953f6b
...
@@ -121,7 +121,7 @@ public class ModelImpl implements ModelService {
...
@@ -121,7 +121,7 @@ public class ModelImpl implements ModelService {
* @Date 16:16 2021/3/5
* @Date 16:16 2021/3/5
**/
**/
@Override
@Override
public
Table
VO
newTable
(
TableVO
tableVO
)
{
public
Table
Info
newTable
(
TableVO
tableVO
)
{
String
xmlMapping
=
createTable
(
tableVO
);
String
xmlMapping
=
createTable
(
tableVO
);
Integer
modelType
=
tableVO
.
getModelType
();
Integer
modelType
=
tableVO
.
getModelType
();
String
parentTable
=
null
;
String
parentTable
=
null
;
...
@@ -159,9 +159,10 @@ public class ModelImpl implements ModelService {
...
@@ -159,9 +159,10 @@ public class ModelImpl implements ModelService {
columnInfo
.
setDbId
(
tableInfo
.
getId
());
columnInfo
.
setDbId
(
tableInfo
.
getId
());
columnInfoDao
.
save
(
columnInfo
);
columnInfoDao
.
save
(
columnInfo
);
}
}
//关闭会话
//关闭会话
session
.
close
();
session
.
close
();
return
table
VO
;
return
table
Info
;
}
}
/**
/**
...
@@ -219,8 +220,8 @@ public class ModelImpl implements ModelService {
...
@@ -219,8 +220,8 @@ public class ModelImpl implements ModelService {
* @Date 16:17 2021/3/5
* @Date 16:17 2021/3/5
**/
**/
public
void
insertValue
(
String
tableName
,
String
xml
,
Map
map
)
{
public
void
insertValue
(
String
tableName
,
String
xml
,
Map
map
)
{
CreateTableUtil
creatTableUtil
=
new
CreateTableUtil
();
CreateTableUtil
creat
e
TableUtil
=
new
CreateTableUtil
();
Session
newSession
=
creatTableUtil
.
getSession
(
entityManagerFactory
,
xml
);
Session
newSession
=
creat
e
TableUtil
.
getSession
(
entityManagerFactory
,
xml
);
SessionImpl
session
=
(
SessionImpl
)
newSession
;
SessionImpl
session
=
(
SessionImpl
)
newSession
;
EntityPersister
entityPersister
=
session
.
getEntityPersister
(
tableName
,
map
);
EntityPersister
entityPersister
=
session
.
getEntityPersister
(
tableName
,
map
);
Type
[]
propertyTypes
=
entityPersister
.
getPropertyTypes
();
Type
[]
propertyTypes
=
entityPersister
.
getPropertyTypes
();
...
...
src/main/test/java/com/tykj/workflowcore/WorkflowCoreApplicationTests.java
浏览文件 @
cc953f6b
...
@@ -7,6 +7,14 @@ import com.tykj.workflowcore.model_layer.entity.vo.TableAndColumnInfoVO;
...
@@ -7,6 +7,14 @@ import com.tykj.workflowcore.model_layer.entity.vo.TableAndColumnInfoVO;
import
com.tykj.workflowcore.model_layer.myEnum.ModelType
;
import
com.tykj.workflowcore.model_layer.myEnum.ModelType
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
com.tykj.workflowcore.model_layer.service.ModelService
;
import
org.aspectj.lang.annotation.Before
;
import
org.aspectj.lang.annotation.Before
;
import
org.hibernate.Session
;
import
org.hibernate.SessionFactory
;
import
org.hibernate.boot.Metadata
;
import
org.hibernate.boot.MetadataSources
;
import
org.hibernate.boot.registry.StandardServiceRegistry
;
import
org.hibernate.query.Query
;
import
org.hibernate.tool.hbm2ddl.SchemaUpdate
;
import
org.hibernate.tool.schema.TargetType
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
...
@@ -19,7 +27,10 @@ import org.springframework.test.web.servlet.RequestBuilder;
...
@@ -19,7 +27,10 @@ import org.springframework.test.web.servlet.RequestBuilder;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
javax.persistence.EntityManagerFactory
;
import
java.io.ByteArrayInputStream
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.EnumSet
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
...
@@ -49,6 +60,12 @@ class WorkflowCoreApplicationTests {
...
@@ -49,6 +60,12 @@ class WorkflowCoreApplicationTests {
@Autowired
@Autowired
ModelService
modelService
;
ModelService
modelService
;
@Autowired
MetadataSources
metadataSources
;
@Autowired
EntityManagerFactory
entityManagerFactory
;
@Before
(
""
)
@Before
(
""
)
public
void
setUp
()
{
public
void
setUp
()
{
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
modelController
).
build
();
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
modelController
).
build
();
...
@@ -193,12 +210,8 @@ class WorkflowCoreApplicationTests {
...
@@ -193,12 +210,8 @@ class WorkflowCoreApplicationTests {
*/
*/
@Test
@Test
public
void
testDelTable
()
throws
Exception
{
public
void
testDelTable
()
throws
Exception
{
String
delete_JSON
=
"{\n"
+
"\"id\":\"4\"\n"
+
"}"
;
request
=
delete
(
"/model/delete/"
)
request
=
delete
(
"/model/delete/"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"id"
,
"5"
)
.
content
(
delete_JSON
)
.
accept
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
);
.
accept
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
);
mockMvc
.
perform
(
request
)
mockMvc
.
perform
(
request
)
.
andExpect
(
status
().
isOk
())
.
andExpect
(
status
().
isOk
())
...
@@ -208,12 +221,55 @@ class WorkflowCoreApplicationTests {
...
@@ -208,12 +221,55 @@ class WorkflowCoreApplicationTests {
@Test
@Test
public
void
testAll
()
{
public
void
testAll
()
{
/*
Integer[] ids = {2, 3, 22};
Integer[] ids = {2, 3, 22};
// List<Integer> integers = Arrays.asList(ids);
List<Integer> integers = Arrays.asList(ids);
//
// Object[] objects = integers.toArray();
Object[] objects = integers.toArray();
TableAndColumnInfoVO all = modelService.getTableInfoAndColumnInfoByBatch(ids);
TableAndColumnInfoVO all = modelService.getTableInfoAndColumnInfoByBatch(ids);
System.out.println(all);
System.out.println(all);
*/
String
xml
=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+
"<!DOCTYPE hibernate-mapping PUBLIC\n"
+
" \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n"
+
" \"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd\">\n"
+
"<hibernate-mapping>\n"
+
" <class entity-name=\"teacher\" table=\"teacher\">\n"
+
" <id name=\"id\" type=\"java.lang.Integer\" length=\"11\" unsaved-value=\"null\">\n"
+
" <generator class=\"identity\" />\n"
+
" </id> <property type=\"java.lang.Integer\" name=\"id\" length=\"11\" column=\"id\"/>\n"
+
" <property type=\"java.lang.String\" name=\"name\" length=\"255\" column=\"name\"/>\n"
+
" <property type=\"java.lang.Integer\" name=\"age\" length=\"11\" column=\"age\"/>\n"
+
" <property type=\"java.lang.String\" name=\"sex\" length=\"255\" column=\"sex\"/>\n"
+
"\n"
+
" <set name=\"students\" table=\"stu_teacher_ref\" >\n"
+
" \t<key column=\"id\"/>\n"
+
" \t<many-to-many class=\"stu\" column=\"\"/>\n"
+
" </set>\n"
+
"\n"
+
" </class>\n"
+
"</hibernate-mapping>"
;
SessionFactory
sessionFactory
=
entityManagerFactory
.
unwrap
(
SessionFactory
.
class
);
StandardServiceRegistry
serviceRegistry
=
sessionFactory
.
getSessionFactoryOptions
().
getServiceRegistry
();
MetadataSources
metadataSources
=
new
MetadataSources
(
serviceRegistry
);
sessionFactory
.
getSessionFactoryOptions
();
metadataSources
.
addInputStream
(
new
ByteArrayInputStream
(
xml
.
getBytes
()));
Metadata
metadata
=
metadataSources
.
buildMetadata
();
//更新数据库Schema,如果不存在就创建表,存在就更新字段,不会影响已有数据
SchemaUpdate
schemaUpdate
=
new
SchemaUpdate
();
schemaUpdate
.
execute
(
EnumSet
.
of
(
TargetType
.
DATABASE
),
metadata
,
serviceRegistry
);
metadata
=
metadataSources
.
buildMetadata
();
//创建会话工厂
SessionFactory
newSessionFactory
=
metadata
.
buildSessionFactory
();
//保存对象
Session
newSession
=
newSessionFactory
.
openSession
();
Query
query
=
newSession
.
createQuery
(
""
);
List
list
=
query
.
list
();
list
.
forEach
(
o
->
System
.
out
.
println
(
o
));
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论