The cause of

JDBC is a must for everyone in Java, and of course There was a time when That happened. Learned in reading, connect to the database, the reader doesn’t know all of you have been cao egg resultSet. Get string, getInteger… To annoy? At that time, I was looking for tools to bypass this, but at the beginning there was no power, can only look at its empty tears, tears over and over again, write. I think of that dream now. I remember my original intention again, so damn!

The principle of

Get all the properties using the reflection of the class, and then iterate over the properties, putting the property name in getObject(), OK

May be helpful to you

Revisit JDBC, the basic API for reflection

Pre-configuration (skip)

Review the basic steps of JDBC

  1. Preparing configuration Parameters
  /**
     * 驱动类路径
     */
    private static String driver = "com.mysql.cj.jdbc.Driver";
 
    /** * link string */
    private static String url = "jdbc:mysql://localhost:3306/dee";
    /** * User name */
    private static String user = "root";
    /** * Password */
    private static String pass = "123123123";
Copy the code
  1. Get the connection object
 /** * Initialize the Connection object *@author [email protected]
     * @timeThen *@params* /
    static {
        if (conn == null) {
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, user, pass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch(SQLException e) { e.printStackTrace(); }}}Copy the code
  1. SQL statement conversion PreparedStatement
    /** * Execute argument *@author [email protected]
     * @time 23:08
     * @params* /
    private static PreparedStatement prepared(String sql, Object... params) {
        // Returns the last value
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            if(params ! =null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]); }}}catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }

Copy the code

The official start of the

Class. GetDeclaredFields (); Class. GetDeclaredFields (); Field. Set () sets the value of this property. Field-setaccessible () sets the state of this property

    /** * query list *@author [email protected]
     * @time 23:04
     * @params [sql, className, params]
     */
    public static <T> List<T> selectList(String sql, Class<T> className, Object... params) {
        // Non-null judgment
        if (StringUtils.isBlank(sql) || className == null) {
            return null;
        }
        printBasicLogger(sql, params);
        // SQL conversion operation
        PreparedStatement prepared = prepared(sql, params);
        List<T> result = null;
        try {
            // execute SQL to get results
            ResultSet resultSet = prepared.executeQuery();
            result = new ArrayList<>();
            while (resultSet.next()) {
                // Determine whether it is a basic data type
                if(! isValType(className)) { T t = className.newInstance();// Get all attributes
                    Field[] fields = className.getDeclaredFields();
                    // Iterate over all attributes of the current column
                    for (Field field : fields) {
                        // Set the property value
                        field.setAccessible(true);
                        field.set(t, resultSet.getObject(humpToUnderline(field.getName())));
                    }
                    result.add(t);
                }else {
                    T t = (T) resultSet.getObject(0); result.add(t); }}}catch(SQLException e){
            e.printStackTrace();
        } catch(IllegalAccessException e){
            e.printStackTrace();
        } catch(InstantiationException e){
            e.printStackTrace();
        }
        return result;
}
Copy the code

So far, this is the end of the article. This JDBC I also encapsulated the single table add, delete, change and check the basic operation, if you need to go to my GitHub GitHub