The introduction

You can write a generic method to directly add any object to a table specified by the MySql database. This can reduce THE number of SQL statements to a certain extent and improve the generality of the code.

1. Add, delete, change and check DAO layer interface with generics

public interface GeneralDao {
    / / to add:
    <T> int addMess(T t);

    // Delete: delete rows based on table name, primary key name, primary key ID
    int deleteMess(Integer id, String tableName, String primeKey);

    / / change:
    <T> int alterMess(T t, Integer id,String primeKey);

    / / check:
    <T> List<T> selectMess(T t, Class<T> clazz, String... params);

    // paging query:
    <T> PageInfo<T> findMessByPage1(T t, Class<T> clazz, String nowPage, String pageNum, String... params);
}
Copy the code

Advantages: do not write a single table to add, delete, change and check THE SQL statement, reduce some workload.

Disadvantages: Cannot be used for join table query, the use of limitations, Java entity class name, attribute name must be exactly the same as MySql table name, field name.

2. Concrete implementation of the interface and key tool class methods

  • Add a piece of data
public <T> int addMess(T t) {
    // The table name is required, the number of fields of the table (i.e. the number of attributes of the entity class), and the field name
    String[] allFieldName = TableUtil.getAllFieldName(t);
    String s = StringUtil.getMark(allFieldName.length, "?");
    String sql = "INSERT INTO " + TableUtil.getClassName(t).toLowerCase() + "(" + StringUtil.getMark("", allFieldName) + ") VALUES(" + s + ");";
    return JDBCUtil.operateDML(sql, TableUtil.getInstanceAllField(t));
}
Copy the code
  • Delete a piece of data
public int deleteMess(Integer id, String tableName, String primeKey) {
    // The id of the object is required
    String sql = "delete from " + tableName + " where " + primeKey + "=?";
    return JDBCUtil.operateDML(sql, id);
Copy the code

– Modify a data item

public <T> int alterMess(T t, Integer id,String primeKey) {
    String[] allFieldName = TableUtil.getAllNotNullFieldName(t);
    String sql = "UPDATE " + TableUtil.getClassName(t).toLowerCase() + " SET " + StringUtil.getMark("=?", allFieldName) + " WHERE "+primeKey+"=" + id + ";";
    return JDBCUtil.operateDML(sql, TableUtil.getInstanceAllNotNullField(t));
}
Copy the code
  • Query a piece of data
public <T> List<T> selectMess(T t, Class<T> clazz, String... params) {
    String selectSql = StringUtil.getSelectSql(t, params);
    return JDBCUtil.operateDQL(clazz, selectSql);
}
Copy the code
  • Utility class methods

int JDBCUtil.operateDML(String sql,Object… Parmas) — everyone has written about JDBC’s universal method of executing DML statements

List

JDBCUtil.operateDQL(Class clazz,String selectSql,Object… Parmas)– a universal method for executing DQL statements

Field [] TableUtil. GetAllFieldName (T T) – get all the members of the generic object properties

String stringutil. getMark(int fieldLength, String mark) – Concatenates characters based on the number of member attributes

String [] TableUtil. GetAllFieldName (T T) – for generic object property names

Object [] TableUtil. GetInstanceAllField (T T) – get all the attribute values of the generic objects

String [] TableUtil. GetAllNotNullFieldName (T T) – get all the attribute name is not empty of generic objects

Object [] TableUtil. GetInstanceAllNotNullField (T T) – get all the not null attribute value of the generic objects

String stringutil. getSelectSql(t, params) — Query statement to get generic objects :t non-empty attributes are query criteria, parmas are fuzzy query fields array

The implementation of the concrete utility class methods is in the next article