package com.changhong.autoform.entity.sql.update; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import org.springframework.ui.ModelMap; import com.changhong.autoform.entity.sql.Sql; import com.changhong.autoform.entity.sql.Where; import com.changhong.autoform.entity.sql.Where.Conditions; import com.changhong.autoform.entity.sql.Where.JoinType; import com.changhong.autoform.entity.sql.Where.Type; public class Update extends Sql{ public Update(String tableName, Map fields) { super(tableName, fields); } public Update(String tableName, Map fields, Where where) { super(tableName, fields); this.where = where; } private Where where; public Where getWhere() { return where; } public void setWhere(Where where) { this.where = where; } @Override public String getSql() { StringBuilder sql = new StringBuilder(); sql.append(" UPDATE ").append(getTableName()) .append(" SET "); boolean isOne = true; for (Map.Entry field : getFields().entrySet()) { if(!isOne){ sql.append(','); } sql.append(field.getKey()).append(" = ?"); isOne = false; } sql.append(where.getSql()); return sql.toString(); } @Override public Object[] getParams() { Object[] param = super.getParams(); List paramList = new ArrayList<>(); paramList.addAll(Arrays.asList(param)); paramList.addAll(Arrays.asList(where.getParams())); return paramList.toArray(); } public static void main(String[] args) { Update u = new Update("user" , new ModelMap("dataStart", "99") , new Where( new Conditions("id",Type.EQ,"123"), new Conditions("name",Type.IN, JoinType.AND, Arrays.asList("张三","李四")) )); System.err.println(u.getSql()); System.err.println(Arrays.toString(u.getParams())); } }