zoukankan      html  css  js  c++  java
  • spring Jdbc自己主动获取主键。

    学习了下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]; } } @Autowired private 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

  • 相关阅读:
    MySQL-Linux升级MySQL
    查看linux 版本
    mysql 密码找回方法
    CentOS7.6利用systemctl添加自定义系统服务
    centos7.6下定时监测MySQL进程终止后自动重启的方法
    Linux实操篇-Linux磁盘分区、挂载
    阿里云centos7.6下MongoDB安装和配置
    Linux中文件权限 chmod、u+x、u、r、w、x分别代表什么
    ABP 发布以后nlog4.NET写入不到日志文件里
    Android studio gradle 下载很缓慢的解决方法,gradle版本不对
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5153239.html
Copyright © 2011-2022 走看看