http://icefire.iteye.com/blogs/1028915
引用
本文对直接使用sqlsession的方式无参考价值,本文只针对只写接口和XML文件的方式
这一篇讲,如何实现一个mapping的方法访问,自动返回page数据,也就是自读计算分页总数。
先看成果对比效果
原先
public interface HelloMapping { public List<Hello> findAll(RowBounds rowBounds); public Long findAllCount(); }
public String list(@ModelAttribute Pagination<Hello> pagination,Model model) { pagination.setTotal(helloMapping.findAllCount()); pagination.setList(helloMapping.findAll(pagination.newRowBounds()))); model.addAttribute("paging", pagination); return "/hello/view/list"; }
现在
public interface HelloMapping { public Pagination<Hello> findAll(Pagination<Hello> pagination); }
public String list(@ModelAttribute Pagination<Hello> pagination,Model model) { model.addAttribute("paging", helloMapping.findAll(pagination)); return "/hello/view/list"; }
如果看源码,会发现,mapping接口自动代理涉及Configuration,MapperRegistry,MapperProxy,MapperMethod。
其中比较可惜的是,MapperProxy,MapperMethod完全无法重用,只能参考其中代码自己来实现了。还有那个mybatis通过的spring集成类SqlSessionFactoryBean,由于要实现自己Configuration,也导致SqlSessionFactoryBean无法重用。这点上,和spring比较起来,就差太多了。
MyConfiguration
public class MyConfiguration extends Configuration { protected MapperRegistry mapperRegistry = new PaginationMapperRegistry(this); public <T> void addMapper(Class<T> type) { mapperRegistry.addMapper(type); } public <T> T getMapper(Class<T> type, SqlSession sqlSession) { return mapperRegistry.getMapper(type, sqlSession); } @SuppressWarnings("rawtypes") public boolean hasMapper(Class type) { return mapperRegistry.hasMapper(type); } }
PaginationMapperRegistry
public class PaginationMapperRegistry extends MapperRegistry { public PaginationMapperRegistry(Configuration config) { super(config); } public <T> T getMapper(Class<T> type, SqlSession sqlSession) { if (!hasMapper(type)) { throw new BindingException("Type " + type + " is not known to the MapperRegistry."); } try { return PaginationMapperProxy.newMapperProxy(type, sqlSession); } catch (Exception e) { throw new BindingException("Error getting mapper instance. Cause: " + e, e); } } }
PaginationMapperProxy
public class PaginationMapperProxy implements InvocationHandler { private static final Set<String> OBJECT_METHODS = new HashSet<String>() { private static final long serialVersionUID = -1782950882770203583L; { add("toString"); add("getClass"); add("hashCode"); add("equals"); add("wait"); add("notify"); add("notifyAll"); } }; private boolean isObjectMethod(Method method) { return OBJECT_METHODS.contains(method.getName()); } private SqlSession sqlSession; private <T> PaginationMapperProxy(SqlSession sqlSession) { this.sqlSession = sqlSession; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (isObjectMethod(method)) { return null; } final Class<?> declaringInterface = findDeclaringInterface(proxy, method); if (Pagination.class.isAssignableFrom(method.getReturnType())) { // 分页处理 return new PaginationMapperMethod(declaringInterface, method, sqlSession).execute(args); } // 原处理方式 final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession); final Object result = mapperMethod.execute(args); if (result == null && method.getReturnType().isPrimitive()) { throw new BindingException( "Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ")."); } return result; } private Class<?> findDeclaringInterface(Object proxy, Method method) { Class<?> declaringInterface = null; for (Class<?> iface : proxy.getClass().getInterfaces()) { Method m = ReflectionUtils.findMethod(iface, method.getName(), method.getParameterTypes()); if (m != null) { declaringInterface = iface; } } if (declaringInterface == null) { throw new BindingException( "Could not find interface with the given method " + method); } return declaringInterface; } @SuppressWarnings("unchecked") public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) { ClassLoader classLoader = mapperInterface.getClassLoader(); Class<?>[] interfaces = new Class[] { mapperInterface }; PaginationMapperProxy proxy = new PaginationMapperProxy(sqlSession); return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy); } }
PaginationMapperMethod
public class PaginationMapperMethod { private SqlSession sqlSession; private Configuration config; private SqlCommandType type; private String commandName; private String commandCountName; private Class<?> declaringInterface; private Method method; private Integer rowBoundsIndex; private Integer paginationIndex; private List<String> paramNames; private List<Integer> paramPositions; private boolean hasNamedParameters; public PaginationMapperMethod(Class<?> declaringInterface, Method method, SqlSession sqlSession) { paramNames = new ArrayList<String>(); paramPositions = new ArrayList<Integer>(); this.sqlSession = sqlSession; this.method = method; this.config = sqlSession.getConfiguration(); this.hasNamedParameters = false; this.declaringInterface = declaringInterface; setupFields(); setupMethodSignature(); setupCommandType(); validateStatement(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public Object execute(Object[] args) { final Object param = getParam(args); Pagination<Object> page; RowBounds rowBounds; if (paginationIndex != null) { page = (Pagination) args[paginationIndex]; rowBounds = page.newRowBounds(); } else if (rowBoundsIndex != null) { rowBounds = (RowBounds) args[rowBoundsIndex]; page = new Pagination<Object>(rowBounds); } else { throw new BindingException("Invalid bound statement (not found rowBounds or pagination in paramenters)"); } page.setTotal(executeForCount(param)); page.setList(executeForList(param, rowBounds)); return page; } private long executeForCount(Object param) { Number result = (Number) sqlSession.selectOne(commandCountName, param); return result.longValue(); } @SuppressWarnings("rawtypes") private List executeForList(Object param, RowBounds rowBounds) { return sqlSession.selectList(commandName, param, rowBounds); } private Object getParam(Object[] args) { final int paramCount = paramPositions.size(); if (args == null || paramCount == 0) { return null; } else if (!hasNamedParameters && paramCount == 1) { return args[paramPositions.get(0)]; } else { Map<String, Object> param = new HashMap<String, Object>(); for (int i = 0; i < paramCount; i++) { param.put(paramNames.get(i), args[paramPositions.get(i)]); } return param; } } private void setupMethodSignature() { final Class<?>[] argTypes = method.getParameterTypes(); for (int i = 0; i < argTypes.length; i++) { if (Pagination.class.isAssignableFrom(argTypes[i])) { paginationIndex = i; } else if (RowBounds.class.isAssignableFrom(argTypes[i])) { rowBoundsIndex = i; } else { String paramName = String.valueOf(paramPositions.size()); paramName = getParamNameFromAnnotation(i, paramName); paramNames.add(paramName); paramPositions.add(i); } } } private String getParamNameFromAnnotation(int i, String paramName) { Object[] paramAnnos = method.getParameterAnnotations()[i]; for (Object paramAnno : paramAnnos) { if (paramAnno instanceof Param) { hasNamedParameters = true; paramName = ((Param) paramAnno).value(); } } return paramName; } private void setupFields() { commandName = declaringInterface.getName() + "." + method.getName(); commandCountName = commandName + "Count"; // 命名约定 } private void setupCommandType() { MappedStatement ms = config.getMappedStatement(commandName); type = ms.getSqlCommandType(); if (type != SqlCommandType.SELECT) { throw new BindingException("Unsupport execution method for: " + commandName); } } private void validateStatement() { if (!config.hasStatement(commandName)) { throw new BindingException("Invalid bound statement (not found): " + commandName); } if (!config.hasStatement(commandCountName)) { throw new BindingException("Invalid bound statement (not found): " + commandCountName); } } }
具体某个查询的SQL配置
<select id="findAll" resultMap="helloResultMap"> select * from HELLO </select> <select id="findAllCount" resultType="long"> select count(*) from HELLO </select>
补代码Pagination,分页辅助类
public class Pagination<T> { private long total; private int pagesize; private List<T> list = Collections.emptyList(); private int offset; private int limit; private int page; public Pagination() { this(1, 15); } public Pagination(int page) { this(page, 15); } public Pagination(int page, int limit) { setPage(page); setLimit(limit); } public Pagination(RowBounds rowBounds) { this.limit = rowBounds.getLimit(); this.offset = rowBounds.getOffset(); this.page = offset / limit + 1; } public void setPage(int page) { if (page < 0) { page = 1; } this.page = page; onInit(); } public void setLimit(int limit) { if (limit < 1) { limit = 15; } this.limit = limit; onInit(); } protected void onInit() { offset = (page - 1) * limit; } protected void onSetRowsize() { pagesize = (int) (total / limit); if (total % limit > 0) { pagesize ++; } if (page > pagesize) { page = pagesize; onInit(); } } protected void onSetList() { if (list == null || list.isEmpty()) { total = 0; page = 1; offset = 0; } } public long getTotal() { return total; } public void setTotal(long rowsize) { this.total = rowsize; onSetRowsize(); } public int getPagesize() { return pagesize; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; onSetList(); } public int getOffset() { return offset; } public int getLimit() { return limit; } public int getPage() { return page; } public RowBounds newRowBounds() { return new RowBounds(getOffset(), getLimit()); } }
目前无可避免的需要两段sql。在考虑如何实现自动生成count(*),但目前做到这样,已经达到自己的预期了。暂时先就这样吧!