org.apache.ibatis.exceptions.TooManyResultsException Java Examples
The following examples show how to use
org.apache.ibatis.exceptions.TooManyResultsException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractService.java From spring-boot-vue-admin with Apache License 2.0 | 5 votes |
@Override public void deleteBy(final String fieldName, final Object value) throws TooManyResultsException { try { final T entity = this.getEntity(fieldName, value); this.assertBy(entity); this.assertDelete(this.mapper.delete(entity) == 1); } catch (final Exception e) { throw new ServiceException(e.getMessage(), e); } }
Example #2
Source File: AbstractService.java From spring-boot-vue-admin with Apache License 2.0 | 5 votes |
@Override public T getBy(final String fieldName, final Object value) throws TooManyResultsException { try { final T entity = this.getEntity(fieldName, value); return this.mapper.selectOne(entity); } catch (final Exception e) { throw new ServiceException(e.getMessage(), e); } }
Example #3
Source File: SqlAutoMapper.java From hui-core-autoreport with Apache License 2.0 | 5 votes |
/** * 获取List中最多只有一个的数据 * * @param list * List结果 * @param <T> * 泛型类型 * @return */ private <T> T getOne(List<T> list) { 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
Source File: SqlSessionTest.java From mybatis with Apache License 2.0 | 5 votes |
@Test(expected=TooManyResultsException.class) public void shouldFailWithTooManyResultsException() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors"); } finally { session.close(); } }
Example #5
Source File: AbstractService.java From mySpringBoot with Apache License 2.0 | 5 votes |
@Override public T selectBy(String fieldName, Object value) throws TooManyResultsException { try { T model = modelClass.newInstance(); Field field = modelClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(model, value); return mapper.selectOne(model); } catch (ReflectiveOperationException e) { throw new ServiceException(e.getMessage(), e); } }
Example #6
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@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 #7
Source File: TestSelectOne.java From tk-mybatis with MIT License | 5 votes |
/** * 查询全部 */ @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(); } }
Example #8
Source File: TestSelectOne.java From tk-mybatis with MIT License | 5 votes |
/** * 入参为null时查询全部 */ @Test(expected = TooManyResultsException.class) public void testDynamicSelectAllByNull() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); mapper.selectOne(null); } finally { sqlSession.close(); } }
Example #9
Source File: AbstractService.java From FS-Blog with Apache License 2.0 | 5 votes |
@Override public T findBy(String property, Object value) throws TooManyResultsException { try { T model = modelClass.newInstance(); Field field = modelClass.getDeclaredField(property); field.setAccessible(true); field.set(model, value); return mapper.selectOne(model); } catch (ReflectiveOperationException e) { // TODO: 17-10-29 更换为一个自定义异常 throw new RuntimeException(e.getMessage(), e); } }
Example #10
Source File: TestSelectOne.java From Mapper with MIT License | 5 votes |
/** * 入参为null时查询全部 */ @Test(expected = TooManyResultsException.class) public void testDynamicSelectAllByNull() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); mapper.selectOne(null); } finally { sqlSession.close(); } }
Example #11
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@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错 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 #12
Source File: SqlSessionTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test(expected=TooManyResultsException.class) public void shouldFailWithTooManyResultsException() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors"); } finally { session.close(); } }
Example #13
Source File: TestSelectOneByExample.java From Mapper with MIT License | 5 votes |
@Test(expected = TooManyResultsException.class) public void testSelectOneByExampleException() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); Example example = new Example(Country.class); example.createCriteria().andGreaterThan("id", 100).andLessThan("id", 151); example.or().andLessThan("id", 41); mapper.selectOneByExample(example); } finally { sqlSession.close(); } }
Example #14
Source File: TestSelectOne.java From Mapper with MIT License | 5 votes |
/** * 查询全部 */ @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(); } }
Example #15
Source File: MyBatisUtil.java From maven-archetype with GNU Lesser General Public License v2.1 | 3 votes |
/** * mybatis自带的条件查询返回的是一个list, 但是某些时候我们需要的是返回单条记录. <br/> * 该方法从mybatis的查询结果list中,取出为一个的第一条记录 * @author Hongbin.Yuan * @param list * @return list 为空或者size为0,返回空; * list 元素超过一个. * list 只包含单独一个元素, 返回该元素 * @throws Exception list 元素超过一个. */ public static <T> T getOneFromList(List<T> list) throws Exception{ if(list == null || list.isEmpty()){ return null; } if(list.size() > 1){ throw new TooManyResultsException("Expected one result (or null) to be returned, but found: " + list.size()); } return list.get(0); }
Example #16
Source File: MyBatisUtil.java From maven-archetype with GNU Lesser General Public License v2.1 | 3 votes |
/** * mybatis自带的条件查询返回的是一个list, 但是某些时候我们需要的是返回单条记录. <br/> * 该方法从mybatis的查询结果list中,取出为一个的第一条记录 * @author Hongbin Yuan * @param list * @return list 为空或者size为0,返回空; * list 元素超过一个. * list 只包含单独一个元素, 返回该元素 * @throws Exception list 元素超过一个. */ public static <T> T getOneFromList(List<T> list) throws Exception{ if(list == null || list.isEmpty()){ return null; } if(list.size() > 1){ throw new TooManyResultsException("Expected one result (or null) to be returned, but found: " + list.size()); } return list.get(0); }
Example #17
Source File: IService.java From FS-Blog with Apache License 2.0 | 2 votes |
/** * 通过某个成员属性查找,value 需符合 unique 约束 * * @param property 属性名 * @param value 属性值 * * @return 实体 * * @throws TooManyResultsException 结果集过大 */ T findBy(String property, Object value) throws TooManyResultsException;
Example #18
Source File: Service.java From spring-boot-api-project-seed with Apache License 2.0 | 2 votes |
/** * 通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束 * @param fieldName * @param value * @return * @throws TooManyResultsException */ T findBy(String fieldName, Object value) throws TooManyResultsException;
Example #19
Source File: Service.java From mySpringBoot with Apache License 2.0 | 2 votes |
/** * @param fieldName * @param value * @throws TooManyResultsException * @Description: 通过Model中某个成员变量名称(非数据表中column的名称)查找,value需符合unique约束 * @Reutrn T */ T selectBy(String fieldName, Object value) throws TooManyResultsException;
Example #20
Source File: Service.java From spring-boot-vue-admin with Apache License 2.0 | 2 votes |
/** * 通过实体中某个成员变量名称查找 value 需符合 unique 约束 * * @param fieldName 字段名 * @param value 字段值 * @return 实体 * @throws TooManyResultsException 多条结果异常 */ T getBy(String fieldName, Object value) throws TooManyResultsException;
Example #21
Source File: Service.java From spring-boot-vue-admin with Apache License 2.0 | 2 votes |
/** * 通过实体中某个成员变量名称(非数据表中 column 的名称)刪除 * * @param fieldName 字段名 * @param value 字段值 * @throws TooManyResultsException 多条结果异常 */ void deleteBy(String fieldName, Object value) throws TooManyResultsException;