• commons-dbutils工具栏的编写


    db.properties

    driverClass=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&jdbcCompliantTruncation=false
    user=root
    password=
    

      

    commonsDbutils工具类

    package com.variflight.b.gateway.utils;
    
    import com.variflight.b.gateway.entity.po.VisitorRateConfPO;
    import org.apache.commons.dbutils.BasicRowProcessor;
    import org.apache.commons.dbutils.GenerousBeanProcessor;
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.BeanMapHandler;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    /**
     * @author muyang
     * @date 2019/5/23 11:37
     */
    public class CommonsDbutils {
    
        private static Connection conn;
    
        private static String driverClass;
    
        private static String url;
    
        private static String user;
    
        private static String password;
    
        static {
    
            try {
                readDBConfig();
                Class.forName(driverClass);
                conn = DriverManager.getConnection(url, user, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static void readDBConfig(){
            try {
                InputStream in = CommonsDbutils.class.getClassLoader().getResourceAsStream("db.properties");
                Properties pro = new Properties();
                pro.load(in);
                driverClass = pro.getProperty("driverClass");
                url = pro.getProperty("url");
                user = pro.getProperty("user");
                password = pro.getProperty("password");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static Connection getConnection(){
            return conn;
        }
    
    
        public static <T> T queryBean(Class<T> clazz, String sql){
            T t  = null;
            QueryRunner queryRunner = new QueryRunner();
            try {
                t = queryRunner.query(conn, sql, new BeanHandler<>(clazz, new BasicRowProcessor(
                        new GenerousBeanProcessor()
                )));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return t;
        }
    
        public static <T> List<T> queryBeanList(Class<T> clazz, String sql){
            List<T> t  = null;
            QueryRunner queryRunner = new QueryRunner();
            try {
                t = queryRunner.query(conn, sql, new BeanListHandler<>(clazz, new BasicRowProcessor(
                        new GenerousBeanProcessor()
                )));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return t;
        }
    
        public static <T> Map<String, T> queryMap(Class<T> clazz,  String sql){
            Map<String,  T> t =null;
            QueryRunner runner = new QueryRunner();
            try {
                t = runner.query(conn, sql, new BeanMapHandler<String, T>(clazz));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return t;
        }
    
    
    
    }
    

      

    测试

      public static void main(String[] args){
    
            //获取连接
            String sql = "select * from visitor_rate_conf";
            //List<VisitorRateConfPO> confPOList = queryBeanList(conn, sql);
            //VisitorRateConfPO confPOList = CommonsDbutils.queryBean(VisitorRateConfPO.class, sql);
            List<VisitorRateConfPO> confPOList = CommonsDbutils.queryBeanList(VisitorRateConfPO.class,  sql);
            //Map<String, VisitorRateConfPO> confPOList = CommonsDbutils.queryMap(VisitorRateConfPO.class,sql);
            System.out.println(confPOList);
        }
    

      

  • 相关阅读:
    SQL Server CHARINDEX和PATINDEX详解
    MVC ListBoxFor raises “value cannot be null” exception
    jquery.uploadify动态传递表单元素
    C# 判断一字符串是否为合法数字(正则表达式)
    jquery 操作Listbox
    JQuery 操作 ListBox间移动和ListBox内移动
    jQuery获取Select选择的Text和 Value
    SQL 语句的执行效率
    JSON 序列化长度限制问题
    C#中利用FileSystemWatcher对单个文件内容的监视
  • 原文地址:https://www.cnblogs.com/achengmu/p/10911083.html
Copyright © 2020-2023  润新知