今天老师讲了mybatis的入门,作为入门基础没有将太多的mybatis的用法.而是通过一个实例,讲解了mybatis所实现的功能.这个实例就是定义了一个接口,用来执行所有的sql查询.在实现这个接口的过程中,用到了包括反射机制,generic等很多知识.
public interface SqlSession {
// <T> T selectOne(String sql, Class<T> clazz, Object... params);
<T> T selectOne(String sql, Class<T> clazz, Map<String, Object> params) throws SQLException;
/*
* 1, (sql, params) => statement
*
* 2, (clazz) => typehandler
* 3, typehandler.handler(rs)
* 4, return t
*/
<E> List<E> selectList(String sql, Class<E> clazz, Object... params) throws SQLException;
int update(String sql, Object... params) throws SQLException;
}
public <T> T selectOne(String sqlId, Class<T> clazz, Map<String, Object> params) {
// 1. Open connection <= Connection pool
// 2. Prepare statement <= PreparedStatement (maybe cached to reuse)
// 3. Execute SQL with parameter <= Database
// 4. Get appropriate type handler <= TypeHandler
// 5. Handle result set => handle(result set, class type) => Object/List
// 6. Release connection etc.
// 7. Return result of step 5
return null;
}