spring Jdbc自動擷取主鍵。,springjdbc

來源:互聯網
上載者:User

spring Jdbc自動擷取主鍵。,springjdbc

學習了下springjdbc,感覺挺實用的,相對來說springjdbc 擴充性相當好了



package com.power.dao;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.jdbc.support.GeneratedKeyHolder;import org.springframework.jdbc.support.KeyHolder;import com.power.sql.UpdateSql;import com.power.sql.UpdateSql.SqlType;import com.power.utils.ArrayAssistant;import com.power.utils.FieldAssistant;import com.power.utils.TransformUtils;@SuppressWarnings("unchecked")public class BaseDao<T>{private Class<T> entityClass ; public BaseDao() {this.entityClass = null;Class<?> c = getClass();Type type = c.getGenericSuperclass();if (type instanceof ParameterizedType) {Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();this.entityClass = (Class<T>) parameterizedType[0];}}@Autowiredprivate JdbcTemplate jdbcTemplate ;  private UpdateSql updateSql ; public Integer insert(T t){updateSql = new UpdateSql(SqlType.INSERT , t) ;  if(null == updateSql.getIdName()){return jdbcTemplate.update( updateSql.getSqlBuffer() , ArrayAssistant.asArray(updateSql.getParam()) ) ;}KeyHolder holder = new GeneratedKeyHolder() ;jdbcTemplate.update(new PreparedStatementCreator() {public PreparedStatement createPreparedStatement(Connection conn)throws SQLException {PreparedStatement ps = conn.prepareStatement( updateSql.getSqlBuffer() , new String[] { updateSql.getIdName() } ) ; List<Object> param = updateSql.getParam() ;int size = param.size() ;for(int x=1;x<=size;x++){ps.setObject(x, param.get(x-1)); }return ps ;}} , holder ) ; int id = TransformUtils.toInt(holder.getKey()) ;FieldAssistant.writeField(updateSql.getIdName(),  t , id, true); return TransformUtils.toInt(holder.getKey()) ;  }public int update(T t){updateSql = new UpdateSql(SqlType.UPDATE , t) ;  int result = jdbcTemplate.update(updateSql.getSqlBuffer(), ArrayAssistant.asArray(updateSql.getParam())) ;return result ;  }public int delete(T t){updateSql = new UpdateSql(SqlType.DELETE , t) ;  int result = jdbcTemplate.update(updateSql.getSqlBuffer(), ArrayAssistant.asArray(updateSql.getParam())) ;return result ;  }public int delete(Integer id){updateSql = new UpdateSql( entityClass ) ;  int result = jdbcTemplate.update(updateSql.getSqlBuffer(), id ) ; return result ; }}



UpdateSql 類在:http://blog.csdn.net/hfmbook/article/details/41290641
轉載請表名出處:http://blog.csdn.net/hfmbook

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.