提交 d027b7f1 authored 作者: HASEE's avatar HASEE

model layer 第一次常用方法修改优化

上级 cf5ab4d5
......@@ -26,7 +26,13 @@ import java.util.Map;
public class ModelController {
@Autowired
private ModelService modelService;
/**
* @Author WWW
* @Description 得到所有数据库信息
* @Date 16:19 2021/3/4
* @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
**/
@RequestMapping("/getAllEntity")
public List<TableInfo> getAllEntity() {
......@@ -36,23 +42,37 @@ public class ModelController {
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return tableInfos;
}
/**
* @Author WWW
* @Description 根据表名得到所有字段名
* @Date 16:20 2021/3/4
* @param tableName
* @return java.util.List<com.tykj.workflowcore.model_layer.model.ColumnInfo>
**/
@GetMapping("/getAllField")
public List<ColumnInfo> getFields(String tableName) {
List<ColumnInfo> all = modelService.showModelFields(tableName);
return all;
if (tableName!=null&&tableName!=""){
return modelService.showModelFields(tableName);
}
return null;
}
/**
* @Author WWW
* @Description 新增数据模型
* @Date 16:21 2021/3/4
* @param
* @return org.springframework.http.ResponseEntity
**/
@PostMapping("/addModel")
//入参使用VO直接接收即可
//jsonStr -> TableVo
public ResponseEntity addModel(@RequestBody TableVO tableVO) throws Exception {
List<TableInfo> tableInfos = modelService.ListAllEntities();
for (TableInfo tableInfo : tableInfos) {
if (tableVO.getModelName()!=null&&tableVO.getModelName().equals(tableInfo.getName())){
if (tableVO.getModelName().equals(tableInfo.getName())){
return new ResponseEntity(HttpStatus.BAD_GATEWAY);
}
}
......@@ -61,6 +81,14 @@ public class ModelController {
return new ResponseEntity(HttpStatus.OK);
}
/**
* @Author WWW
* @Description 对应表插入数据
* map (表名,字段数据)
* @Date 16:22 2021/3/4
* @param map
* @return int
**/
@PostMapping("/insertValues")
public int insertValues(@RequestBody Map<String,Object> map){
int i = modelService.putValueByEntityName(map);
......
......@@ -10,6 +10,7 @@ import com.tykj.workflowcore.model_layer.model.ColumnVO;
import com.tykj.workflowcore.model_layer.model.TableInfo;
import com.tykj.workflowcore.model_layer.model.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
import com.tykj.workflowcore.model_layer.utils.CreatTableUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
......@@ -60,8 +61,7 @@ import java.util.stream.Collectors;
@Service
public class ModelImpl implements ModelService {
@Autowired
private Map<String, Object> cacheMap;
@Autowired
private EntityManagerFactory entityManagerFactory;
......@@ -73,7 +73,6 @@ public class ModelImpl implements ModelService {
private ColumnInfoDao columnInfoDao;
@Override
public List<TableInfo> ListAllEntities() {
List<TableInfo> all = tableInfoDao.findAll();
......@@ -109,55 +108,12 @@ public class ModelImpl implements ModelService {
@Override
public TableVO NewTable(TableVO tableVO) {
String XML_MAPPING = CreatTableUtil.CreatTable(tableVO);
CreatTableUtil creatTableUtil = new CreatTableUtil();
Session session = creatTableUtil.getSession(entityManagerFactory,XML_MAPPING);
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
StandardServiceRegistry serviceRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
sessionFactory.getSessionFactoryOptions();
List<ColumnVO> dataList = tableVO.getDataList();
String XML_MAPPING = "<?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=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n";
XML_MAPPING += " <id name=\"id\" type=\"java.lang.Long\" length=\"64\" unsaved-value=\"null\">\n" +
" <generator class=\"identity\" />\n" +
" </id>";
for (ColumnVO columnVO : dataList) {
XML_MAPPING +=
" <property type=\"" + columnVO.getFiledType() + "\" name=\"" + columnVO.getFieldName() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n";
}
XML_MAPPING += " </class>" +
"</hibernate-mapping>";
metadataSources.addInputStream(new ByteArrayInputStream(XML_MAPPING.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();
// Map<String, Object> entity = new HashMap<>();
// for (ColumnVO columnVO : dataList) {
// entity.put( columnVO.getFieldName(),"2222");
// }
// newSession.save(tableVO.getModuleName(), entity);
cacheMap.put("tableName", tableVO.getModelName());
cacheMap.put("xml", XML_MAPPING);
TableInfo tableInfo = new TableInfo();
tableInfo.setName(tableVO.getModelName());
......@@ -178,9 +134,8 @@ public class ModelImpl implements ModelService {
columnInfoDao.save(columnInfo);
}
//关闭会话
newSession.close();
session.close();
return tableVO;
}
......@@ -217,23 +172,10 @@ public class ModelImpl implements ModelService {
}
public void insertValue(String tableName,String xml,Map map){
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();
CreatTableUtil creatTableUtil = new CreatTableUtil();
Session newSession = creatTableUtil.getSession(entityManagerFactory,xml);
SessionImpl session = (SessionImpl) newSession;
EntityPersister entityPersister = session.getEntityPersister(tableName, map);
Type[] propertyTypes = entityPersister.getPropertyTypes();
String[] propertyNames = entityPersister.getEntityPersister().getPropertyNames();
Object[] propertyValuesToInsert = entityPersister.getPropertyValuesToInsert(map, null, session);
......@@ -246,19 +188,19 @@ public class ModelImpl implements ModelService {
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) value);
map.put(propertyNames[i],parse);
} catch (ParseException e) {
e.printStackTrace();
}
e.printStackTrace(); }
}
if (propertyType instanceof IntegerType){
//然后调用强转方法
int i1 = Integer.parseInt(propertyNames[i]);
map.put(propertyNames[i],i1);
}
if (propertyType instanceof StringType){
//然后调用强转方法
propertyNames[i]=propertyNames[i]+"";
map.put(propertyNames[i],propertyNames[i]);
}
}
newSession.saveOrUpdate(tableName,map);
}
......
package com.tykj.workflowcore.model_layer.utils;
import com.tykj.workflowcore.model_layer.model.ColumnVO;
import com.tykj.workflowcore.model_layer.model.TableVO;
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.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import javax.persistence.EntityManagerFactory;
import java.io.ByteArrayInputStream;
import java.util.EnumSet;
import java.util.List;
/**
* @ClassName CreatTableUtil
* @Description TODO
* @Author WWW
* @Date 2021/3/1 14:35
* @Version 1.0
*/
public class CreatTableUtil {
public static String CreatTable(TableVO tableVO){
List<ColumnVO> dataList = tableVO.getDataList();
String XML_MAPPING = "<?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=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n";
XML_MAPPING += " <id name=\"id\" type=\"java.lang.Long\" length=\"64\" unsaved-value=\"null\">\n" +
" <generator class=\"identity\" />\n" +
" </id>";
for (ColumnVO columnVO : dataList) {
XML_MAPPING +=
" <property type=\"" + columnVO.getFiledType() + "\" name=\"" + columnVO.getFieldName() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n";
}
XML_MAPPING += " </class>" +
"</hibernate-mapping>";
return XML_MAPPING;
}
public Session getSession(EntityManagerFactory entityManagerFactory,String XML){
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();
return newSession;
}
}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/model_layer?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true
type: com.alibaba.druid.pool.DruidDataSource
jpa:
hibernate:
ddl-auto: update
naming:
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
spring:
datasource:
username: root
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论