public Object mapRow(ResultSet rs)throws SQLException;
}
3.环境类
public static List executeQuery(RowMapper rm,String sql,Object...params){
List list=new ArrayList();
try {
Connection con=CommonDao.getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject((i+1), params[i]);
}
}
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
Object obj=rm.mapRow(rs);
list.add(obj);
}
CommonDao.closeAll(rs, pstmt, con);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
4具体策略类
这个在测试代码中客户根据自己想要返回的返回值类型定。
本文介绍了如何运用策略模式来实现MyBatis中的查询映射方法。通过创建一个通用的executeQuery方法,接受RowMapper作为参数,根据不同的RowMapper实例来映射不同的结果集,从而达到灵活处理查询结果的目的。在实际应用中,客户可以根据需求定义具体的RowMapper策略类。
3950

被折叠的 条评论
为什么被折叠?



