zoukankan      html  css  js  c++  java
  • JAVA 数据库切换 -4 QueryRunnerHelper(CRUD方法封装)

    public class QueryRunnerHelper extends QueryRunner {
        
        private static Logger logger = Logger.getLogger(QueryRunnerHelper.class);
        
        private  QueryRunner qr;
        
        public QueryRunnerHelper(DataSource ds) {        
             qr = new QueryRunner(ds);
        }
        
        private void close() throws SQLException {        
            DbUtils.closeQuietly(qr.getDataSource().getConnection());
            qr.getDataSource().getConnection().close();        
        }
        
        
        public  List<Map<String,Object>> getlist(String sql)throws SQLException{
            logger.info(sql);       
            List<Map<String,Object>> list = qr.query(sql, new MapListHandler()); 
            close();
            return list;       
            
        }
        
        public  <T> int save(String table,T bean) throws Exception { 
            String sql= getSaveSQL(table,bean);     
            logger.info(sql);       
            int row = qr.update(sql);
            System.out.printf("已经有[%d]发生了改变",row);
            close();
            return row;
       }
    
         public  int save(String sql) throws Exception {  
            logger.info(sql);     
            int row = qr.update (sql);
            System.out.printf("已经有[%d]发生了改变",row);
            close();
            return row;
        }
    
    
        public  int disOrEnAble(String table,String enabledMark,String keyFields,String keyValue)throws SQLException{
            String sql=  "UPDATE "+ table+ "SET EnabledMark = "+enabledMark+" WHERE "+ keyFields + "= '"+keyValue+"'";     
            logger.info(sql);  
            int row = qr.update(sql);
            System.out.printf("已经有[%d]发生了改变",row);     
            close();
            return row;
        }    
        
        public int update(String sql) throws SQLException {         
            logger.info(sql);
            int row = qr.update(sql);
            System.out.printf("已经有[%d]发生了改变",row);
            close();      
            return row;
           
        }
    
        public  int delete(String table,String keyFields,String keyValue)throws SQLException{
            String sql=  "UPDATE "+ table+ " SET DeleteMark = 1 WHERE "+ keyFields + "= '"+keyValue+"'";
            logger.info(sql);  
            int row = qr.update(sql);
            System.out.printf("已经有[%d]发生了改变",row);     
            close();
            return row;
        }
    
        public  int deleteTrue(String table,String keyFields,String keyValue)throws SQLException{
            String sql=  "DELETE FROM "+ table+ " WHERE "+ keyFields + "= '"+keyValue+"'";        
            logger.info(sql);  
            int row = qr.update( sql);
            System.out.printf("已经有[%d]发生了改变",row);     
            close();
            return row;
        }
    
        private  String getUpdateSQL(String table,Object obj) throws Exception {
            StringBuilder  sqlString = new StringBuilder();
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String fieldName = property.getName();
                String key = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);            
                Method getter = property.getReadMethod();
                Object valueObject = getter.invoke(obj); 
                if(key.toUpperCase().equals("CLASS")) {
                    continue;
                }
                if(valueObject !=null) {               
                    if(property.getPropertyType() == java.util.Date.class) {
                        valueObject = ToolsOfDate.toYYYYMMDDHHMISS((Date)valueObject);
                    }
                    sqlString.append( key+" = '"+ valueObject +"',");               
                }
            }
            return sqlString.deleteCharAt(sqlString.length()-1).toString() ;
        }
        /*
         *  定义方法,使用QueryRunner类的方法update将数据表的数据修改
         */
        public  <T> int update(String table,T bean,String keyFields,String keyValue)throws Exception{
            
            String updateFieldString = getUpdateSQL(table,bean);
            String sql=  "UPDATE "+ table+ " SET "+updateFieldString+" WHERE "+ keyFields + "= '"+keyValue+"'";   
            logger.info(sql); 
            int row = qr.update(sql);
            System.out.printf("已经有[%d]发生了改变",row);
            close();        
            return row;
        }
    
    
    
    
    //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
    private   String getSaveSQL(String table,Object obj) throws Exception {
        StringBuilder  sqlString = new StringBuilder();
        StringBuilder  keyString = new StringBuilder();
        keyString.append(" ( ");
        StringBuilder  valString = new StringBuilder();
        valString.append(" ( ");
        sqlString.append("INSERT INTO " + table);
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String fieldName = property.getName();
            String key = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);            
            Method getter = property.getReadMethod();
            Object valueObject = getter.invoke(obj); 
            if(key.toUpperCase().equals("CLASS")) {
                continue;
            }
            if(valueObject !=null) {
                keyString.append( key+",");
                if(property.getPropertyType() == java.util.Date.class) {
                    valueObject = ToolsOfDate.toYYYYMMDDHHMISS((Date)valueObject);
                }
                valString.append("'"+ valueObject +"',");
            }
        }
        return sqlString.toString() + keyString.deleteCharAt(keyString.length()-1).append(")").toString()+"VALUES"+ valString.deleteCharAt(valString.length()-1).append(")").toString();
    }
    
    public  <T> List<T>  getlist(String sql, Class<T> oclass) throws Exception {  
       
        logger.info(sql);
        T beanT = oclass.newInstance();       
        List<T> list = qr.query(sql, new BeanListHandler<T>((Class<? extends T>) beanT.getClass()));
        close();
        return list;
    }
  • 相关阅读:
    一键清理 Nexus 中无用的 Docker 镜像
    python 获取Linux和Windows硬件信息
    基于Docker安装破解版Jira(无坑)
    vim安装vundle时遇到的问题
    SpringBoot注解大全
    SpringBoot 应用JPA中的一些知识点
    记录初学SpringBoot使用Redis序列化的坑
    Spring Boot连接MySql报错
    钉钉扫码登录中的签名算法在python中的实现
    让Linux的history命令显示用户名和时间
  • 原文地址:https://www.cnblogs.com/chuangjie1988/p/14899654.html
Copyright © 2011-2022 走看看