com.j256.ormlite.dao.Dao.queryForId()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(222)

本文整理了Java中com.j256.ormlite.dao.Dao.queryForId()方法的一些代码示例,展示了Dao.queryForId()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.queryForId()方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:queryForId

Dao.queryForId介绍

[英]Retrieves an object associated with a specific ID.
[中]检索与特定ID关联的对象。

代码示例

代码示例来源:origin: BaronZ88/MinimalistWeather

  1. public Weather queryWeather(String cityId) throws SQLException {
  2. return TransactionManager.callInTransaction(WeatherDatabaseHelper.getInstance(context).getConnectionSource(), () -> {
  3. Weather weather = weatherDaoOperation.queryForId(cityId);
  4. if (weather != null) {
  5. weather.setAirQualityLive(apiDaoOperation.queryForId(cityId));
  6. weather.setWeatherForecasts(forecastDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
  7. weather.setLifeIndexes(lifeIndexesDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
  8. weather.setWeatherLive(realTimeDaoOperation.queryForId(cityId));
  9. }
  10. return weather;
  11. });
  12. }

代码示例来源:origin: BaronZ88/MinimalistWeather

  1. /**
  2. * 查询数据库中的所有已添加的城市
  3. *
  4. * @return 结果集中只包括城市信息,天气数据不在其中
  5. * @throws SQLException
  6. */
  7. public List<Weather> queryAllSaveCity() throws SQLException {
  8. return TransactionManager.callInTransaction(WeatherDatabaseHelper.getInstance(context).getConnectionSource(), () -> {
  9. List<Weather> weatherList = weatherDaoOperation.queryForAll();
  10. for (Weather weather : weatherList) {
  11. String cityId = weather.getCityId();
  12. weather.setAirQualityLive(apiDaoOperation.queryForId(cityId));
  13. weather.setWeatherForecasts(forecastDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
  14. weather.setLifeIndexes(lifeIndexesDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
  15. weather.setWeatherLive(realTimeDaoOperation.queryForId(cityId));
  16. }
  17. return weatherList;
  18. });
  19. }

代码示例来源:origin: tianshaojie/AndroidFine

  1. public User get(int id) {
  2. try {
  3. return userDaoOpe.queryForId(id);
  4. } catch (SQLException e) {
  5. e.printStackTrace();
  6. }
  7. return null;
  8. }

代码示例来源:origin: tianshaojie/AndroidFine

  1. /**
  2. * 通过Id得到一篇文章
  3. *
  4. * @param id
  5. * @return
  6. */
  7. public Article get(int id)
  8. {
  9. Article article = null;
  10. try
  11. {
  12. article = articleDaoOpe.queryForId(id);
  13. } catch (SQLException e)
  14. {
  15. e.printStackTrace();
  16. }
  17. return article;
  18. }

代码示例来源:origin: tianshaojie/AndroidFine

  1. /**
  2. * 通过Id得到一个Article
  3. *
  4. * @param id
  5. * @return
  6. */
  7. @SuppressWarnings("unchecked")
  8. public Article getArticleWithUser(int id)
  9. {
  10. Article article = null;
  11. try
  12. {
  13. article = articleDaoOpe.queryForId(id);
  14. helper.getDao(User.class).refresh(article.getUser());
  15. } catch (SQLException e)
  16. {
  17. e.printStackTrace();
  18. }
  19. return article;
  20. }

代码示例来源:origin: com.j256.ormlite/ormlite-core

  1. /**
  2. * @see Dao#queryForId(Object)
  3. */
  4. @Override
  5. public T queryForId(ID id) {
  6. try {
  7. return dao.queryForId(id);
  8. } catch (SQLException e) {
  9. logMessage(e, "queryForId threw exception on: " + id);
  10. throw new RuntimeException(e);
  11. }
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Test(expected = RuntimeException.class)
  2. public void testQueryForIdThrow() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. expect(dao.queryForId(isA(String.class))).andThrow(new SQLException("Testing catch"));
  7. replay(dao);
  8. rtDao.queryForId("wow");
  9. verify(dao);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Override
  2. public Void call() throws Exception {
  3. assertEquals(1, dao.create(foo1));
  4. assertNotNull(dao.queryForId(foo1.id));
  5. return null;
  6. }
  7. });

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

  1. @Override
  2. public Void call() throws Exception {
  3. // we do the delete
  4. assertEquals(1, accountDao.delete(account));
  5. assertNull(accountDao.queryForId(account.getId()));
  6. // but then (as an example) we throw an exception which rolls back the delete
  7. throw new Exception("We throw to roll back!!");
  8. }
  9. });

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testCreateReserverdTable() throws Exception {
  3. Dao<Where, String> whereDao = createDao(Where.class, true);
  4. String id = "from-string";
  5. Where where = new Where();
  6. where.id = id;
  7. whereDao.create(where);
  8. Where where2 = whereDao.queryForId(id);
  9. assertEquals(id, where2.id);
  10. assertEquals(1, whereDao.delete(where2));
  11. assertNull(whereDao.queryForId(id));
  12. }

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

  1. @Test
  2. public void testCreateReserverdTable() throws Exception {
  3. Dao<Create, String> whereDao = createDao(Create.class, true);
  4. String id = "from-string";
  5. Create where = new Create();
  6. where.id = id;
  7. assertEquals(1, whereDao.create(where));
  8. Create where2 = whereDao.queryForId(id);
  9. assertEquals(id, where2.id);
  10. assertEquals(1, whereDao.delete(where2));
  11. assertNull(whereDao.queryForId(id));
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testDisableAlreadyDisabled() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. dao.setObjectCache(false);
  5. Foo foo = new Foo();
  6. int val = 12312321;
  7. foo.val = val;
  8. assertEquals(1, dao.create(foo));
  9. Foo result = dao.queryForId(foo.id);
  10. assertNotSame(foo, result);
  11. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testBigIntegerId() throws Exception {
  3. Dao<BigIntegerId, BigInteger> dao = createDao(BigIntegerId.class, true);
  4. BigIntegerId foo = new BigIntegerId();
  5. dao.create(foo);
  6. assertEquals(BigInteger.ONE, foo.id);
  7. BigIntegerId result = dao.queryForId(BigInteger.ONE);
  8. assertNotNull(result);
  9. assertEquals(foo.id, result.id);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testUuidDao() throws Exception {
  3. Dao<UuidClass, Integer> dao = createDao(UuidClass.class, true);
  4. UuidClass uuid = new UuidClass();
  5. uuid.uuid = null;
  6. assertEquals(1, dao.create(uuid));
  7. UuidClass uuidResult = dao.queryForId(uuid.id);
  8. assertNotNull(uuidResult);
  9. assertNull(uuidResult.uuid);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void shouldStoreEntityWithStringIdAndLoadFromDbOnCreateOrUpdate() throws Exception {
  3. Dao<EntityWithStringId, String> dao = createDao(EntityWithStringId.class, true);
  4. EntityWithStringId data = new EntityWithStringId();
  5. data.id = "generated_id_from_factory";
  6. data.value = "some value";
  7. dao.createOrUpdate(data);
  8. EntityWithStringId found = dao.queryForId(data.id);
  9. assertNotNull(found);
  10. assertEquals(found.id, data.id);
  11. assertEquals(found.value, data.value);
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testByteArrayId() throws Exception {
  3. Class<ByteArrayId> clazz = ByteArrayId.class;
  4. Dao<ByteArrayId, Object> dao = createDao(clazz, true);
  5. ByteArrayId foo = new ByteArrayId();
  6. foo.id = new byte[] { 1, 2, 3, 4, 5 };
  7. assertEquals(1, dao.create(foo));
  8. ByteArrayId result = dao.queryForId(foo.id);
  9. assertNotNull(result);
  10. assertArrayEquals(foo.id, result.id);
  11. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testForeignNull() throws Exception {
  3. Dao<Foreign, Integer> dao = createDao(Foreign.class, true);
  4. Foreign foreign = new Foreign();
  5. foreign.foo = null;
  6. assertEquals(1, dao.create(foreign));
  7. Foreign foreign2 = dao.queryForId(foreign.id);
  8. assertNotNull(foreign2);
  9. assertNull(foreign2.foo);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testForeignIntIdNull() throws Exception {
  3. Dao<ForeignIntId, Integer> dao = createDao(ForeignIntId.class, true);
  4. ForeignIntId foreign = new ForeignIntId();
  5. foreign.one = null;
  6. assertEquals(1, dao.create(foreign));
  7. ForeignIntId foreign2 = dao.queryForId(foreign.id);
  8. assertNotNull(foreign2);
  9. assertNull(foreign2.one);
  10. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void shouldStoreEntityWithCustomTypeIdAndLoadFromDbOnCreate() throws Exception {
  3. Dao<EntityWithCustomTypeId, EntityId> dao = createDao(EntityWithCustomTypeId.class, true);
  4. EntityWithCustomTypeId data = new EntityWithCustomTypeId();
  5. data.id = EntityId.entityId("generated_id_from_factory");
  6. data.value = "some value";
  7. dao.create(data);
  8. EntityWithCustomTypeId found = dao.queryForId(data.id);
  9. assertNotNull(found);
  10. assertEquals(found.id, data.id);
  11. assertEquals(found.value, data.value);
  12. }

代码示例来源:origin: j256/ormlite-core

  1. @Test
  2. public void testBaseClassAnnotations() throws Exception {
  3. Sub sub = new Sub();
  4. String stuff = "djeqpodjewdopjed";
  5. sub.stuff = stuff;
  6. Dao<Sub, Object> dao = createDao(Sub.class, true);
  7. assertEquals(0, sub.id);
  8. assertEquals(1, dao.create(sub));
  9. Sub sub2 = dao.queryForId(sub.id);
  10. assertNotNull(sub2);
  11. assertEquals(sub.stuff, sub2.stuff);
  12. }

相关文章