一. 首先,在数据库有一张对应的表:
二. controller层代码如下:
@PutMapping("/dynamicallyTable")
public AjaxResult dynamicallyTable(@RequestBody Data data){
List<Template> list &#61; templateService.DynamicallyTable(data.getSid());
if (list.size()&#61;&#61;0){
return AjaxResult.success("未设置模板消息,请设置模板信息",false);
}
Project tablename &#61; (Project) templateService.creatTableName(data.getSid());
String name &#61; tablename.getYear() &#43; "_" &#43; tablename.getName() &#43; "_" &#43; tablename.getStages().getName();
Map<String, String> mymap &#61; new HashMap<>();
for (Template template : list) {
String type &#61; template.getType();
String field &#61; template.getField();
mymap.put(field, type);
}
templateService.creatTemplateTable(name, mymap);
iStageService.updateByStageId(data.getSid(), name);
return AjaxResult.success("成功生成定稿表");
}
三 . Service接口及其实现类:
3.1 Service接口代码如下:
public void creatTemplateTable(String name, Map<String, String> mymap);
3.2 Service接口实现类代码如下:
&#64;Override
public void creatTemplateTable(String name, Map<String, String> mymap) {
templateMapper.creatTemplateTable(name,mymap);
}
四, Mapper接口及其Mapper.xml
4.1 Mapper接口:
public void creatTemplateTable(&#64;Param("name") String name,&#64;Param("mymap") Map<String, String> mymap);
4.2 Mapper.xml:
<update id&#61;"creatTemplateTable" parameterType&#61;"map">
create table ${name} (
&#96;id&#96; int(11) NOT NULL AUTO_INCREMENT,
<foreach collection&#61;"mymap" index&#61;"key" item&#61;"value" separator&#61;",">
${key} ${value}(4000)
foreach>
,PRIMARY KEY (&#96;id&#96;)
) ENGINE&#61;InnoDB DEFAULT CHARSET&#61;utf8
update>