mybatis分页实现2

mybatis分页实现2
前一篇写了如何实现sql分页
http://icefire.iteye.com/blogs/1028915

引用
本文对直接使用sqlsession的方式无参考价值,本文只针对只写接口和XML文件的方式


这一篇讲,如何实现一个mapping的方法访问,自动返回page数据,也就是自读计算分页总数。

先看成果对比效果
原先
Java代码 复制代码 收藏代码
  1. public interface HelloMapping { 
  2.  
  3.     public List<Hello> findAll(RowBounds rowBounds); 
  4.      
  5.     public Long findAllCount(); 
public interface HelloMapping {

	public List<Hello> findAll(RowBounds rowBounds);
	
	public Long findAllCount();
}

Java代码 复制代码 收藏代码
  1. public String list(@ModelAttribute Pagination<Hello> pagination,Model model) { 
  2.  
  3.     pagination.setTotal(helloMapping.findAllCount()); 
  4.     pagination.setList(helloMapping.findAll(pagination.newRowBounds()))); 
  5.     model.addAttribute("paging", pagination); 
  6.  
  7.     return "/hello/view/list"
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";
}


现在
Java代码 复制代码 收藏代码
  1. public interface HelloMapping { 
  2.  
  3.     public Pagination<Hello> findAll(Pagination<Hello> pagination); 
public interface HelloMapping {

	public Pagination<Hello> findAll(Pagination<Hello> pagination);
}

Java代码 复制代码 收藏代码
  1. public String list(@ModelAttribute Pagination<Hello> pagination,Model model) { 
  2.  
  3.     model.addAttribute("paging", helloMapping.findAll(pagination)); 
  4.          
  5.     return "/hello/view/list"
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
Java代码 复制代码 收藏代码
  1. public class MyConfiguration extends Configuration { 
  2.  
  3.     protected MapperRegistry mapperRegistry = new PaginationMapperRegistry(this); 
  4.      
  5.     public <T> void addMapper(Class<T> type) { 
  6.         mapperRegistry.addMapper(type); 
  7.     } 
  8.  
  9.     public <T> T getMapper(Class<T> type, SqlSession sqlSession) { 
  10.         return mapperRegistry.getMapper(type, sqlSession); 
  11.     } 
  12.  
  13.     @SuppressWarnings("rawtypes"
  14.     public boolean hasMapper(Class type) { 
  15.         return mapperRegistry.hasMapper(type); 
  16.     } 
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
Java代码 复制代码 收藏代码
  1. public class PaginationMapperRegistry extends MapperRegistry { 
  2.  
  3.     public PaginationMapperRegistry(Configuration config) { 
  4.         super(config); 
  5.     } 
  6.  
  7.     public <T> T getMapper(Class<T> type, SqlSession sqlSession) { 
  8.         if (!hasMapper(type)) { 
  9.             throw new BindingException("Type " + type + " is not known to the MapperRegistry."); 
  10.         } 
  11.         try
  12.             return PaginationMapperProxy.newMapperProxy(type, sqlSession); 
  13.         } catch (Exception e) { 
  14.             throw new BindingException("Error getting mapper instance. Cause: " + e, e); 
  15.         } 
  16.     } 
  17.  
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
Java代码 复制代码 收藏代码
  1. public class PaginationMapperProxy implements InvocationHandler { 
  2.  
  3.     private static final Set<String> OBJECT_METHODS = new HashSet<String>() { 
  4.         private static final long serialVersionUID = -1782950882770203583L; 
  5.         { 
  6.             add("toString"); 
  7.             add("getClass"); 
  8.             add("hashCode"); 
  9.             add("equals"); 
  10.             add("wait"); 
  11.             add("notify"); 
  12.             add("notifyAll"); 
  13.         } 
  14.     }; 
  15.  
  16.     private boolean isObjectMethod(Method method) { 
  17.         return OBJECT_METHODS.contains(method.getName()); 
  18.     } 
  19.  
  20.     private SqlSession sqlSession; 
  21.  
  22.     private <T> PaginationMapperProxy(SqlSession sqlSession) { 
  23.         this.sqlSession = sqlSession; 
  24.     } 
  25.  
  26.     @Override 
  27.     public Object invoke(Object proxy, Method method, Object[] args) 
  28.             throws Throwable { 
  29.         if (isObjectMethod(method)) { 
  30.             return null
  31.         } 
  32.         final Class<?> declaringInterface = findDeclaringInterface(proxy, method); 
  33.         if (Pagination.class.isAssignableFrom(method.getReturnType())) { 
  34.             // 分页处理 
  35.             return new PaginationMapperMethod(declaringInterface, method, sqlSession).execute(args); 
  36.         } 
  37.         // 原处理方式 
  38.         final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession); 
  39.         final Object result = mapperMethod.execute(args); 
  40.         if (result == null && method.getReturnType().isPrimitive()) { 
  41.             throw new BindingException( 
  42.                     "Mapper method '" 
  43.                             + method.getName() 
  44.                             + "' (" 
  45.                             + method.getDeclaringClass() 
  46.                             + ") attempted to return null from a method with a primitive return type (" 
  47.                             + method.getReturnType() + ")."); 
  48.         } 
  49.         return result; 
  50.     } 
  51.  
  52.     private Class<?> findDeclaringInterface(Object proxy, Method method) { 
  53.         Class<?> declaringInterface = null
  54.         for (Class<?> iface : proxy.getClass().getInterfaces()) { 
  55.             Method m = ReflectionUtils.findMethod(iface, method.getName(), method.getParameterTypes()); 
  56.             if (m != null) { 
  57.                 declaringInterface = iface; 
  58.             } 
  59.         } 
  60.         if (declaringInterface == null) { 
  61.             throw new BindingException( 
  62.                     "Could not find interface with the given method " + method); 
  63.         } 
  64.         return declaringInterface; 
  65.     } 
  66.  
  67.     @SuppressWarnings("unchecked"
  68.     public static <T> T newMapperProxy(Class<T> mapperInterface, 
  69.             SqlSession sqlSession) { 
  70.         ClassLoader classLoader = mapperInterface.getClassLoader(); 
  71.         Class<?>[] interfaces = new Class[] { mapperInterface }; 
  72.         PaginationMapperProxy proxy = new PaginationMapperProxy(sqlSession); 
  73.         return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy); 
  74.     } 
  75.  
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
Java代码 复制代码 收藏代码
  1. public class PaginationMapperMethod { 
  2.  
  3.     private SqlSession sqlSession; 
  4.     private Configuration config; 
  5.  
  6.     private SqlCommandType type; 
  7.     private String commandName; 
  8.     private String commandCountName; 
  9.  
  10.     private Class<?> declaringInterface; 
  11.     private Method method; 
  12.  
  13.     private Integer rowBoundsIndex; 
  14.     private Integer paginationIndex; 
  15.     private List<String> paramNames; 
  16.     private List<Integer> paramPositions; 
  17.  
  18.     private boolean hasNamedParameters; 
  19.  
  20.     public PaginationMapperMethod(Class<?> declaringInterface, Method method, 
  21.             SqlSession sqlSession) { 
  22.         paramNames = new ArrayList<String>(); 
  23.         paramPositions = new ArrayList<Integer>(); 
  24.         this.sqlSession = sqlSession; 
  25.         this.method = method; 
  26.         this.config = sqlSession.getConfiguration(); 
  27.         this.hasNamedParameters = false
  28.         this.declaringInterface = declaringInterface; 
  29.         setupFields(); 
  30.         setupMethodSignature(); 
  31.         setupCommandType(); 
  32.         validateStatement(); 
  33.     } 
  34.      
  35.     @SuppressWarnings({ "unchecked", "rawtypes" }) 
  36.     public Object execute(Object[] args) { 
  37.         final Object param = getParam(args); 
  38.         Pagination<Object> page; 
  39.         RowBounds rowBounds; 
  40.  
  41.         if (paginationIndex != null) { 
  42.             page = (Pagination) args[paginationIndex]; 
  43.             rowBounds = page.newRowBounds(); 
  44.         } 
  45.         else if (rowBoundsIndex != null) { 
  46.             rowBounds = (RowBounds) args[rowBoundsIndex]; 
  47.             page = new Pagination<Object>(rowBounds); 
  48.         } 
  49.         else
  50.             throw new BindingException("Invalid bound statement (not found rowBounds or pagination in paramenters)"); 
  51.         } 
  52.         page.setTotal(executeForCount(param)); 
  53.         page.setList(executeForList(param, rowBounds)); 
  54.         return page; 
  55.     } 
  56.      
  57.     private long executeForCount(Object param) { 
  58.         Number result = (Number) sqlSession.selectOne(commandCountName, param); 
  59.         return result.longValue(); 
  60.     } 
  61.  
  62.     @SuppressWarnings("rawtypes"
  63.     private List executeForList(Object param, RowBounds rowBounds) { 
  64.         return sqlSession.selectList(commandName, param, rowBounds); 
  65.     } 
  66.  
  67.     private Object getParam(Object[] args) { 
  68.         final int paramCount = paramPositions.size(); 
  69.         if (args == null || paramCount == 0) { 
  70.             return null
  71.         } else if (!hasNamedParameters && paramCount == 1) { 
  72.             return args[paramPositions.get(0)]; 
  73.         } else
  74.             Map<String, Object> param = new HashMap<String, Object>(); 
  75.             for (int i = 0; i < paramCount; i++) { 
  76.                 param.put(paramNames.get(i), args[paramPositions.get(i)]); 
  77.             } 
  78.             return param; 
  79.         } 
  80.     } 
  81.      
  82.     private void setupMethodSignature() { 
  83.         final Class<?>[] argTypes = method.getParameterTypes(); 
  84.         for (int i = 0; i < argTypes.length; i++) { 
  85.             if (Pagination.class.isAssignableFrom(argTypes[i])) { 
  86.                 paginationIndex = i; 
  87.             } 
  88.             else if (RowBounds.class.isAssignableFrom(argTypes[i])) { 
  89.                 rowBoundsIndex = i; 
  90.             } else
  91.                 String paramName = String.valueOf(paramPositions.size()); 
  92.                 paramName = getParamNameFromAnnotation(i, paramName); 
  93.                 paramNames.add(paramName); 
  94.                 paramPositions.add(i); 
  95.             } 
  96.         } 
  97.     } 
  98.  
  99.     private String getParamNameFromAnnotation(int i, String paramName) { 
  100.         Object[] paramAnnos = method.getParameterAnnotations()[i]; 
  101.         for (Object paramAnno : paramAnnos) { 
  102.             if (paramAnno instanceof Param) { 
  103.                 hasNamedParameters = true
  104.                 paramName = ((Param) paramAnno).value(); 
  105.             } 
  106.         } 
  107.         return paramName; 
  108.     } 
  109.  
  110.     private void setupFields() { 
  111.         commandName = declaringInterface.getName() + "." + method.getName(); 
  112.         commandCountName = commandName + "Count"; // 命名约定 
  113.     } 
  114.  
  115.     private void setupCommandType() { 
  116.         MappedStatement ms = config.getMappedStatement(commandName); 
  117.         type = ms.getSqlCommandType(); 
  118.         if (type != SqlCommandType.SELECT) { 
  119.             throw new BindingException("Unsupport execution method for: " + commandName); 
  120.         } 
  121.     } 
  122.  
  123.     private void validateStatement() { 
  124.         if (!config.hasStatement(commandName)) { 
  125.             throw new BindingException("Invalid bound statement (not found): " + commandName); 
  126.         } 
  127.         if (!config.hasStatement(commandCountName)) { 
  128.             throw new BindingException("Invalid bound statement (not found): " + commandCountName); 
  129.         } 
  130.     } 
  131.  
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配置

Xml代码 复制代码 收藏代码
  1. <select id="findAll" resultMap="helloResultMap"> 
  2.     select * from HELLO 
  3. </select> 
  4.  
  5. <select id="findAllCount" resultType="long"> 
  6.     select count(*) from HELLO 
  7. </select> 


补代码Pagination,分页辅助类
Java代码 复制代码 收藏代码
  1. public class Pagination<T> { 
  2.  
  3.     private long total; 
  4.      
  5.     private int pagesize; 
  6.      
  7.     private List<T> list = Collections.emptyList(); 
  8.      
  9.     private int offset; 
  10.      
  11.     private int limit; 
  12.      
  13.     private int page; 
  14.      
  15.     public Pagination() { 
  16.         this(1, 15); 
  17.     } 
  18.      
  19.     public Pagination(int page) { 
  20.         this(page, 15); 
  21.     } 
  22.      
  23.     public Pagination(int page, int limit) { 
  24.         setPage(page); 
  25.         setLimit(limit); 
  26.     } 
  27.      
  28.     public Pagination(RowBounds rowBounds) { 
  29.         this.limit = rowBounds.getLimit(); 
  30.         this.offset = rowBounds.getOffset(); 
  31.         this.page = offset / limit + 1
  32.     } 
  33.      
  34.     public void setPage(int page) { 
  35.         if (page < 0) { 
  36.             page = 1
  37.         } 
  38.         this.page = page; 
  39.         onInit(); 
  40.     } 
  41.      
  42.     public void setLimit(int limit) { 
  43.         if (limit < 1) { 
  44.             limit = 15
  45.         } 
  46.         this.limit = limit; 
  47.         onInit(); 
  48.     } 
  49.      
  50.     protected void onInit() { 
  51.         offset = (page - 1) * limit; 
  52.     } 
  53.      
  54.     protected void onSetRowsize() { 
  55.         pagesize = (int) (total / limit); 
  56.         if (total % limit > 0) { 
  57.             pagesize ++; 
  58.         } 
  59.         if (page > pagesize) { 
  60.             page = pagesize; 
  61.             onInit(); 
  62.         } 
  63.     } 
  64.      
  65.     protected void onSetList() { 
  66.         if (list == null || list.isEmpty()) { 
  67.             total = 0
  68.             page = 1
  69.             offset = 0
  70.         } 
  71.     } 
  72.  
  73.     public long getTotal() { 
  74.         return total; 
  75.     } 
  76.  
  77.     public void setTotal(long rowsize) { 
  78.         this.total = rowsize; 
  79.         onSetRowsize(); 
  80.     } 
  81.  
  82.     public int getPagesize() { 
  83.         return pagesize; 
  84.     } 
  85.  
  86.     public List<T> getList() { 
  87.         return list; 
  88.     } 
  89.      
  90.     public void setList(List<T> list) { 
  91.         this.list = list; 
  92.         onSetList(); 
  93.     } 
  94.  
  95.     public int getOffset() { 
  96.         return offset; 
  97.     } 
  98.  
  99.     public int getLimit() { 
  100.         return limit; 
  101.     } 
  102.  
  103.     public int getPage() { 
  104.         return page; 
  105.     } 
  106.      
  107.     public RowBounds newRowBounds() { 
  108.         return new RowBounds(getOffset(), getLimit()); 
  109.     } 
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(*),但目前做到这样,已经达到自己的预期了。暂时先就这样吧! 
2
11
分享到:     
评论
4 楼    mygia    2012-08-23           
学习一下!
3 楼    icefire    2011-11-29           
kelor 写道
看了下源代码,在创建sessionFactory,session对象的时候传入的参数都是Configuration。
如果要用自己的Configuration。是不是也要扩展下XMLConfigBuilder,使用自定义的Configuration来初始化configuration属性?


不知道你的需求是怎样的,如果用spring,就没必要用xml来初始化Configuration了。如果是xml要增加自己的属性,可能需要扩展XMLConfigBuilder吧,具体没研究过了。我使用自己的Configuration是因为我要替换其中的属性,Configuration又没提供set方法,所以别无选择。
2 楼    kelor    2011-11-25           
看了下源代码,在创建sessionFactory,session对象的时候传入的参数都是Configuration。
如果要用自己的Configuration。是不是也要扩展下XMLConfigBuilder,使用自定义的Configuration来初始化configuration属性?
1 楼    guoyi_20082008    2011-10-14           
也按照此方式实现,但发现缺少Pagination类的代码,不知能否附上,谢谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值