Java实现动态表单

更新时间:2023-05-21 12:49:51 阅读: 评论:0

Java实现动态表单
前提
我们的业务上会有各种各样的表单,常规的开发是⼀个表单,我们⼀般会在数据库⾥⾯建⽴⼀个对应的表,然后在代码中建⽴⼀个model 与之对应,⼀旦建⽴好以后,就不能再对表单进⾏修改了,如果需要修改,就需要修改数据库表,修改model重新发版上线,对于频繁需要表单变更的情况来说很不友好。那么有没有⽅法可以对现有表单的列进⾏增删改甚⾄动态⽣成⼀个表单呢?这样的话表单变动就不⽤发版重新上线了,岂不美哉。解决办法⾃然是有的。
⽅案
整体思路就是数据库⾏转列,在java层⾯动态⽣成VO返回给前台。
具体实现
使⽤Mybatis3.4.6 + mysql5.7.26 + cglib3.2.5 + cglib-nodep3.2.5来进⾏实现
1.sql:CreateDB.sql
drop table  if exists form_metadata;
CREATE TABLE form_metadata
(
ID int AUTO_INCREMENT,
name varchar(100) comment '列名',
property varchar(100) comment '属性名',
property_type varchar(100) comment '属性类型',
form_type int comment '表单类型 1 请假表  2 ⽀付表 3 报销表',
PRIMARY KEY (ID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE=utf8_general_ci;
drop table  if exists form_data;
CREATE TABLE form_data
(
ID int AUTO_INCREMENT,
property varchar(100) comment '属性名',
`value` varchar(100) comment '属性值',
form_type int comment '表单类型',
row_id int comment '⾏数据ID,区分⼀⾏数据',
ur_id int comment '⽤户ID',
PRIMARY KEY (ID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 DEFAULT COLLATE=utf8_general_ci;
inrt into form_metadata (name, property, property_type, form_type) values ("主键", "id", "java.lang.String", 1);
inrt into form_metadata (name, property, property_type, form_type) values ("姓名", "name", "java.lang.String", 1);
inrt into form_metadata (name, property, property_type, form_type) values ("请假天数", "days", "java.lang.String", 1);
inrt into form_metadata (name, property, property_type, form_type) values ("请假原因", "reason", "java.lang.String", 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("id", "1", 1, 1, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("name", "张三", 1, 1, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("days", "3", 1, 1, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("reason", "想睡觉", 1, 1, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("id", "2", 1, 2, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("name", "张三", 1, 2, 1);紧急制动
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("days", "4", 1, 2, 1);
inrt into form_data (property, `value`, form_type, row_id, ur_id) values ("reason", "想吃饭", 1, 2, 1);
2.Mybatis配置:l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<ttings>
<tting name="mapUnderscoreToCamelCa" value="true"/>
</ttings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
英语a级真题<property name="" value=""/>
</transactionManager>
joyo
<dataSource type="UNPOOLED">
非主流翻译<property name="driver" value="sql.jdbc.Driver"/>
<property name="url" value="xxx"/>
<property name="urname" value="xxx"/>
<property name="password" value="xxx"/>洛杉矶湖人英文
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="org.apache.ibatis.dynamicform.FormDataMapper"/>
<mapper class="org.apache.ibatis.dynamicform.FormMetadataMapper"/>    </mappers>
</configuration>
3.数据库表对应的model:FormData、FormMetadata
public class FormData {
private Integer id;
private String property;
private String value;
private Integer formType;
private Integer rowId;
private Integer urId;
// 省略getter、tter
}
public class FormMetadata {
private Integer id;
private String name;
private String property;
private String propertyType;
private Integer formType;
/
/ 省略getter、tter
第一的英文}
4.Mapper:FormDataMapper、FormMetadataMapper
capital city
public interface FormDataMapper {
@Select("lect * from form_data where ur_id = #{urId} and form_type = #{formType}")
List<FormData> listFormData(@Param("urId") Integer urId, @Param("formType") Integer formType); }
public interface FormMetadataMapper {
@Select("lect * from form_metadata where form_type = #{formType}")
List<FormMetadata> listFormMetadatas(@Param("formType") Integer formType);
}
5.测试代码
public class DynamicFormTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void tUp() throws Exception {
Reader reader = ResourceAsReader("org/apache/ibatis/l");  sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.clo();
tflsSqlSession ssion = sqlSessionFactory.openSession();
Connection conn = Connection();
reader = ResourceAsReader("org/apache/ibatis/dynamicform/CreateDB.sql");
reader = ResourceAsReader("org/apache/ibatis/dynamicform/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.tLogWriter(null);
runner.runScript(reader);
netapp
conn.clo();
reader.clo();
ssion.clo();
}
@Test
public void getDynamicForm() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
FormMetadataMapper formMetadataMapper = Mapper(FormMetadataMapper.class);  FormDataMapper formMapper = Mapper(FormDataMapper.class);
// 查询请假表元数据
List<FormMetadata> formMetadatas = formMetadataMapper.listFormMetadatas(1);
// 准备使⽤cglibg准备根据表单元数据动态⽣成VO
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
for (FormMetadata formMetadata : formMetadatas) {
map.Property(), Class.PropertyType()));
}
BeanGenerator generator = new BeanGenerator();
for (Map.Entry<String, Class<?>> entry : Set()) {
generator.Key(), Value());
}
/
/ 查询⽤户ID为1的请假表数据
List<FormData> formDatas = formMapper.listFormData(1, 1);
// 区分⼀⾏数据
Map<Integer, List<FormData>> diffRow = new HashMap<>();
for (FormData formData : formDatas) {
if (!RowId())) {
diffRow.RowId(), new ArrayList<>());
}
<(RowId()).add(formData);
}
List<Object> vos = new ArrayList();
for (Map.Entry<Integer, List<FormData>> entry : Set()) {
List<FormData> list = Value();
// ⽣成bean
Object vo = ate();
// 给bean赋值
新泽西理工大学BeanMap beanMap = ate(vo);
for (FormData ele : list) {
beanMap.Property(), Value());
}
vos.add(vo);
}
/
/ 页⾯VO结果打印
for (Object vo : vos) {
System.out.JSONString(vo));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.clo();
}
}
}
执⾏测试代码运⾏结果:
代码已经贴全,好好捋⼀下思路就可以了,我这⾥就不细说了,cglib⽣成bean也只是⽤了最简单的⽅法,没有做优化,⽣产⽣不能直接使⽤。

本文发布于:2023-05-21 12:49:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/719682.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:表单   数据   修改   需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图