Java Examples for org.apache.ibatis.exceptions.TooManyResultsException
The following java examples will help you to understand the usage of org.apache.ibatis.exceptions.TooManyResultsException. These source code samples are taken from different open source projects.
Example 1
| Project: mybatis-shards-master File: SelectOneExitStrategy.java View source code |
@Override
public Object compileResults(ExitOperationsCollector exitOperationsCollector) {
List<Object> list = exitOperationsCollector.apply(nonNullResult);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}Example 2
| Project: ccshop-master File: BaseDao.java View source code |
/**
* 查询一条记录
*
* @param arg0
* @param arg1
* @return
*/
public <M extends BaseBean> M selectOne(String arg0, M param) {
if (param == null) {
return null;
}
SqlSession session = openSession();
M bean = null;
try {
bean = session.selectOne(arg0, param);
} catch (MyBatisSystemException e) {
if (e != null && e.getCause() instanceof TooManyResultsException) {
} else {
throw e;
}
}
return bean;
}Example 3
| Project: mybatis-master File: DefaultSqlSession.java View source code |
//核心selectOne
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
//转而去调用selectList,很简单的,如果得到0条则返回null,得到1条则返回1条,得到多条报TooManyResultsException错
// 特别需要主要的是当没有查询到结果的时候就会返回null。因此一般建议在mapper中编写resultType的时候使用包装类型
//而不是基本类型,比如推荐使用Integer而不是int。这样就可以避免NPE
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}Example 4
| Project: java.old-master File: PageQueryInterceptor.java View source code |
@SuppressWarnings("unchecked")
private void queryCount(Invocation invocation, Connection connection) throws Throwable {
Object[] queryArgs = invocation.getArgs();
MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
Object parameter = queryArgs[PARAMETER_INDEX];
RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
// 将需要执行的查询语句修改成 select count(*) 的形式
MappedStatement queryCountMs = MyBatisConfig.getQueryCountStatement(ms);
if (null == queryCountMs) {
queryCountMs = createMappedStatement(ms);
MyBatisConfig.registerQueryCountStatement(ms, queryCountMs);
}
Executor executor = (Executor) invocation.getTarget();
SimpleExecutor s = new SimpleExecutor(ms.getConfiguration(), executor.getTransaction());
List<Integer> list = (List<Integer>) s.doQuery(queryCountMs, parameter, RowBounds.DEFAULT, null);
if (list.size() == 1) {
int count = (Integer) list.get(0);
MyBatisConfig.setRecordNum(connection, count);
int offset = rowBounds.getOffset();
if ((offset > count) && (offset != RowBounds.NO_ROW_OFFSET)) {
PageHelper helper = new PageHelper(count, rowBounds.getLimit());
helper.setCurrPage(helper.getMaxPage());
queryArgs[ROWBOUNDS_INDEX] = helper.getRowBounds();
}
} else {
throw new TooManyResultsException("Expected one result to be returned by PageQueryInterceptor.intercept()");
}
}Example 5
| Project: lanyuan-mybaits-master File: DefaultSqlSession.java View source code |
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}Example 6
| Project: mybatis-3-master File: DefaultSqlSession.java View source code |
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}Example 7
| Project: Mapper-master File: TestSelectOne.java View source code |
/**
* 查询全部
*/
@Test(expected = TooManyResultsException.class)
public void testDynamicSelectAll() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = mapper.selectOne(new Country());
} finally {
sqlSession.close();
}
}