package com.changhong.autoform.entity.sql.insert; import java.util.Arrays; import java.util.Map; import org.springframework.ui.ModelMap; import com.changhong.autoform.entity.sql.Sql; public class Insert extends Sql{ private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Insert(String tableName, Map fields) { super(tableName, fields); } @Override public String getSql() { StringBuilder sql = new StringBuilder(); sql.append(" INSERT INTO ") .append(getTableName()) .append('('); boolean isOne = true; StringBuilder sb = new StringBuilder(); for (Map.Entry field : getFields().entrySet()) { if(!isOne){ sql.append(','); sb.append(','); } sql.append(field.getKey()); sb.append('?'); isOne = false; } sql.append(") VALUES (").append(sb).append(')'); return sql.toString(); } public static void main(String[] args) { Insert i = new Insert("abc", new ModelMap("123", "abc").addAttribute("456", "def")); System.err.println(i.getSql()); System.err.println(Arrays.toString(i.getParams())); } }