dawdler-dao-core
Module Introduction
The dao core module is a simple database operation component implemented through reflection. It supports basic CRUD operations and read-write separation.
1. Add the dependency in pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-dao-core</artifactId>
2. Common SuperDAO APIs
The query series is for queries
The insert series is for inserts
The update series is for updates
The delete series is for deletes
The Object ... values varargs are ordered and correspond to the ? placeholders in the SQL statement.
The parameter passed as Class\<T\> c is the database entity object entity.
For insertMap, the first parameter is the table name, and the keys of the second parameter Map are table fields and the values are the values.
The insertPrepareGetKey and insertMapGetKey methods return the primary key generated by the insert; the primary key type must be an integer type.
<T extends Object> List<T> queryList(String sql, Class<T> c) throws SQLException;
<T extends Object> List<T> queryList(String sql, Class<T> c, MappingPolicy policy) throws SQLException;
<T extends Object> List<T> queryListPrepare(String sql, Class<T> c, Object... values) throws SQLException;
<T extends Object> List<T> queryListPrepare(String sql, Class<T> c, MappingPolicy policy, Object... values) throws SQLException;
<T extends Object> T queryObject(String sql, Class<T> c) throws SQLException;
<T extends Object> T queryObjectPrepare(String sql, Class<T> c, Object... values) throws SQLException;
List<Map<String, Object>> queryListMaps(String sql) throws SQLException;
List<Map<String, Object>> queryListMapsPrepare(String sql, Object... values) throws SQLException;
int update(String sql) throws SQLException;
int updatePrepare(String sql, Object... values) throws SQLException;
int insert(String sql) throws SQLException;
int insertPrepare(String sql, Object... values) throws SQLException;
long insertPrepareGetKey(String sql, Object... values) throws SQLException;
int insertMap(String tableName, Map<String, Object> data) throws SQLException;
long insertMapGetKey(String tableName, Map<String, Object> data) throws SQLException;
int delete(String sql) throws SQLException;
int deletePrepare(String sql, Object... values) throws SQLException;
int queryCount(String sql) throws SQLException;
int queryCountPrepare(String sql, Object... values) throws SQLException;
3. Result set mapping policy
queryList/queryListPrepare reflectively map result set columns by column name to entity setters (for example, column name maps to setName), and support conversion within common type families (narrowing between numeric types, numeric to Boolean, date to String, Blob to byte[]).
The default policy is STRICT: when a column has no setter, a type conversion fails, or a setter throws an exception, an ObjectMappingException is thrown (carrying the column name/SQL type/row number); that column is never silently skipped and its data is never lost.
For extra entity-external columns produced by joined-table queries, you can pass MappingPolicy.IGNORE_UNMAPPED, which only skips columns without a setter or of unknown type and warns once per column, while type conversion failures and exceptions thrown by setters are still thrown.
4. Injecting a DAO
Just write a custom DAO that extends SuperDAO, such as UserDAO.
Inject the dao with @Repository in the service layer and you can use the injected dao.
Example:
public class UserDAO extends SuperDAO{
public int insertUser(User user) throws SQLException{
return insertPrepare("insert into t_user(username,`password`,age) values(?,?,?)", user.getUsername(), user.getPassword(), user.getAge());
}
}
public class UserServiceImpl implements UserService{
@Repository
UserDAO userDAO;
@Override
@DBTransaction
public boolean addUser(User user) {
return userDAO.insertUser(user) > 0;
}
}
5. Obtaining read and write connections
When you need a Connection, you can obtain the read connection through getReadConnection in SuperDAO and the write connection through getWriteConnection.
Underneath, these are implemented through LocalConnectionFactory.getReadConnection() and LocalConnectionFactory.getWriteConnection().
For configuration of read-write separation, see dawdler-db-core data source rule configuration.