java 根據實體物件產生 增刪改的SQL語句

來源:互聯網
上載者:User

標籤:java

<pre name="code" class="java">import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.util.List;import java.util.Vector;import org.apache.commons.lang3.reflect.FieldUtils;/** * @author Gary Huang * 部落格地址:http://blog.csdn.net/hfmbook/article/details/41290641 *  * */public class UpdateSql {private Object target ; private String idName ; private Object idValue ; private SqlType currentType ; public enum SqlType {INSERT, UPDATE, DELETE }public UpdateSql(SqlType sqlType, Object target) {this.target = target ; switch (sqlType) {case INSERT:currentType = SqlType.INSERT ;createInsert(); break;  case UPDATE:currentType = SqlType.UPDATE ;createUpdate() ; break;case DELETE:currentType = SqlType.DELETE ;createDelete() ;  break ;}}/** * 建立跟刪除 * */private void createDelete() {String tableName = getTableName() ;Field[] fields = target.getClass().getDeclaredFields() ;StringBuffer sqlBuffer = new StringBuffer() ; sqlBuffer.append("DELETE FROM ").append(tableName).append(" WHERE ") ;for(Field field : fields){if(!Modifier.isStatic(field.getModifiers())){ID id = field.getAnnotation(ID.class);if(null != id){sqlBuffer.append( field.getName()).append("=?");param.add(readField(field));}}}this.sqlBuffer = sqlBuffer.toString();}public Object readField(Field field ){try {return FieldUtils.readField( field , target, true )  ; } catch (Exception e) {throw new RuntimeException( currentType.name() ,e ) ;}}/** * 建立跟新語句 * */private void createUpdate() {String tableName = getTableName() ;Field[] fields = target.getClass().getDeclaredFields() ;StringBuffer sqlBuffer = new StringBuffer() ; sqlBuffer.append("UPDATE ").append(tableName).append(" SET ") ;  for(Field field : fields){if(!Modifier.isStatic(field.getModifiers())){ID id = field.getAnnotation(ID.class);if(id == null){sqlBuffer.append( field.getName()).append("=?") ; param.add( readField(field)  ) ;  }else{idName = field.getName() ; idValue = readField(field)   ; }}}if(idName == null){throw new RuntimeException( "not found of " + target.getClass() + "'s ID") ; }sqlBuffer.append(" WHERE ").append(idName).append("=?") ;param.add( idValue ); this.sqlBuffer = sqlBuffer.toString() ;}/** * 根據註解擷取表名 * */private String getTableName() {String tableName = null ;Class<?> clazz = target.getClass() ;Table table = clazz.getAnnotation(Table.class) ; if(null != table){tableName = table.name() ;if("".equalsIgnoreCase(tableName)){tableName = clazz.getSimpleName() ;}}else{tableName = clazz.getSimpleName() ;}return tableName ;}/** * 建立插入語句 * */private void createInsert() {String tableName = getTableName() ;Field[] fields = target.getClass().getDeclaredFields() ;StringBuffer sqlBuffer = new StringBuffer() ; sqlBuffer.append("INSERT INTO ").append(tableName).append("("); for(Field field : fields){if(!Modifier.isStatic(field.getModifiers())){ID id = field.getAnnotation(ID.class);if(id == null){sqlBuffer.append( field.getName()).append(",") ; param.add( readField(field) ) ; }}}int length = sqlBuffer.length() ;sqlBuffer.delete(length-1, length).append(")values("); int size = param.size() ;for(int x=0;x<size;x++){if(x != 0){sqlBuffer.append(",") ;  }sqlBuffer.append("?") ; }sqlBuffer.append(")"); this.sqlBuffer = sqlBuffer.toString() ;}private List<Object> param = new Vector<Object>();private String sqlBuffer;public List<Object> getParam() {return param;}public String getSqlBuffer() {return sqlBuffer;}public String getIdName() {return idName;}public Object getIdValue() {return idValue;}@java.lang.annotation.Target(value={java.lang.annotation.ElementType.TYPE})@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)public @interface Table {String name() default "";}@java.lang.annotation.Target(value={java.lang.annotation.ElementType.FIELD})@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)public @interface ID{}}



java 根據實體物件產生 增刪改的SQL語句

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.