com.j256.ormlite.dao.Dao类的使用及代码示例

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

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

Dao介绍

[英]The definition of the Database Access Objects that handle the reading and writing a class from the database. Kudos to Robert A. for the general concept of this hierarchy.
[中]

代码示例

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

private void updateWeather(Weather weather) throws SQLException {

    weatherDaoOperation.update(weather);
    apiDaoOperation.update(weather.getAirQualityLive());

    //先删除旧数据
    DeleteBuilder<WeatherForecast, Long> forecastDeleteBuilder = forecastDaoOperation.deleteBuilder();
    forecastDeleteBuilder.where().eq(WeatherForecast.CITY_ID_FIELD_NAME, weather.getCityId());
    PreparedDelete<WeatherForecast> forecastPrepared = forecastDeleteBuilder.prepare();
    forecastDaoOperation.delete(forecastPrepared);
    //再插入新数据
    for (WeatherForecast weatherForecast : weather.getWeatherForecasts()) {
      forecastDaoOperation.create(weatherForecast);
    }

    //先删除旧数据
    DeleteBuilder<LifeIndex, Long> lifeIndexDeleteBuilder = lifeIndexesDaoOperation.deleteBuilder();
    lifeIndexDeleteBuilder.where().eq(LifeIndex.CITY_ID_FIELD_NAME, weather.getCityId());
    PreparedDelete<LifeIndex> lifeIndexPrepared = lifeIndexDeleteBuilder.prepare();
    lifeIndexesDaoOperation.delete(lifeIndexPrepared);
    //再插入新数据
    for (LifeIndex index : weather.getLifeIndexes()) {
      lifeIndexesDaoOperation.create(index);
    }
    realTimeDaoOperation.update(weather.getWeatherLive());
  }
}

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

/**
 * 根据城市查询城市信息
 *
 * @param cityId 城市ID
 * @return city
 * @throws SQLException
 */
public City queryCityById(String cityId) throws SQLException {
  QueryBuilder<City, Integer> queryBuilder = cityDaoOperation.queryBuilder();
  queryBuilder.where().eq(City.CITY_ID_FIELD_NAME, cityId);
  return queryBuilder.queryForFirst();
}

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

/**
 * 查询数据库中的所有已添加的城市
 *
 * @return 结果集中只包括城市信息,天气数据不在其中
 * @throws SQLException
 */
public List<Weather> queryAllSaveCity() throws SQLException {
  return TransactionManager.callInTransaction(WeatherDatabaseHelper.getInstance(context).getConnectionSource(), () -> {
    List<Weather> weatherList = weatherDaoOperation.queryForAll();
    for (Weather weather : weatherList) {
      String cityId = weather.getCityId();
      weather.setAirQualityLive(apiDaoOperation.queryForId(cityId));
      weather.setWeatherForecasts(forecastDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
      weather.setLifeIndexes(lifeIndexesDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
      weather.setWeatherLive(realTimeDaoOperation.queryForId(cityId));
    }
    return weatherList;
  });
}

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

public Weather queryWeather(String cityId) throws SQLException {
  return TransactionManager.callInTransaction(WeatherDatabaseHelper.getInstance(context).getConnectionSource(), () -> {
    Weather weather = weatherDaoOperation.queryForId(cityId);
    if (weather != null) {
      weather.setAirQualityLive(apiDaoOperation.queryForId(cityId));
      weather.setWeatherForecasts(forecastDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
      weather.setLifeIndexes(lifeIndexesDaoOperation.queryForEq(WeatherForecast.CITY_ID_FIELD_NAME, cityId));
      weather.setWeatherLive(realTimeDaoOperation.queryForId(cityId));
    }
    return weather;
  });
}

代码示例来源:origin: magefree/mage

public List<UserStats> getAllUsers() {
  try {
    QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
    return dao.query(qb.prepare());
  } catch (SQLException ex) {
    Logger.getLogger(UserStatsRepository.class).error("Error getting all users from DB - ", ex);
  }
  return null;
}

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

@Test
public void testCreateReserverdTable() throws Exception {
  Dao<Where, String> whereDao = createDao(Where.class, true);
  String id = "from-string";
  Where where = new Where();
  where.id = id;
  whereDao.create(where);
  Where where2 = whereDao.queryForId(id);
  assertEquals(id, where2.id);
  assertEquals(1, whereDao.delete(where2));
  assertNull(whereDao.queryForId(id));
}

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

@Test
public void testUpdate() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo = new Foo();
  assertEquals(1, dao.create(foo));
  foo.equal = 1;
  assertEquals(1, dao.update(foo));
}

代码示例来源:origin: magefree/mage

public static boolean isDatabaseObsolete(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
  TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
  Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
  QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
  queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("version", version);
  List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
  if (dbVersions.isEmpty()) {
    DatabaseVersion dbVersion = new DatabaseVersion();
    dbVersion.setEntity(entityName);
    dbVersion.setVersion(version);
    dbVersionDao.create(dbVersion);
  }
  return dbVersions.isEmpty();
}

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

@Test
public void testDateStringFormatNotDayAlign() throws Exception {
  Dao<DateStringFormat, Object> dao = createDao(DateStringFormat.class, true);
  DateStringFormat dateStringFormat = new DateStringFormat();
  dateStringFormat.date = new SimpleDateFormat("yyyy-MM-dd HH").parse("2012-09-01 12");
  assertEquals(1, dao.create(dateStringFormat));
  List<DateStringFormat> results = dao.queryForAll();
  assertEquals(1, results.size());
  assertFalse(dateStringFormat.date.equals(results.get(0).date));
}

代码示例来源:origin: magefree/mage

public static void updateVersion(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
  TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
  Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
  QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
  queryBuilder.where().eq("entity", new SelectArg(entityName));
  List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
  if (!dbVersions.isEmpty()) {
    DeleteBuilder<DatabaseVersion, Object> deleteBuilder = dbVersionDao.deleteBuilder();
    deleteBuilder.where().eq("entity", new SelectArg(entityName));
    deleteBuilder.delete();
  }
  DatabaseVersion databaseVersion = new DatabaseVersion();
  databaseVersion.setEntity(entityName);
  databaseVersion.setVersion(version);
  dbVersionDao.create(databaseVersion);
}

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

@Test
public void testDelete() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo = new Foo();
  assertEquals(1, dao.create(foo));
  assertNotNull(dao.queryForId(foo.id));
  assertEquals(1, dao.delete(foo));
  assertNull(dao.queryForId(foo.id));
  assertEquals(0, dao.queryForAll().size());
}

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

@Test
public void testDeleteById() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo = new Foo();
  assertEquals(1, dao.create(foo));
  assertNotNull(dao.queryForId(foo.id));
  assertEquals(1, dao.deleteById(foo.id));
  assertNull(dao.queryForId(foo.id));
  assertEquals(0, dao.queryForAll().size());
}

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

private void insertWeather(Weather weather) throws SQLException {
  weatherDaoOperation.create(weather);
  apiDaoOperation.create(weather.getAirQualityLive());
  for (WeatherForecast weatherForecast : weather.getWeatherForecasts()) {
    forecastDaoOperation.create(weatherForecast);
  }
  for (LifeIndex index : weather.getLifeIndexes()) {
    lifeIndexesDaoOperation.create(index);
  }
  realTimeDaoOperation.create(weather.getWeatherLive());
}

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

@Test
public void shouldStoreEntityWithCustomTypeIdAndLoadFromDbOnCreate() throws Exception {
  Dao<EntityWithCustomTypeId, EntityId> dao = createDao(EntityWithCustomTypeId.class, true);
  EntityWithCustomTypeId data = new EntityWithCustomTypeId();
  data.id = EntityId.entityId("generated_id_from_factory");
  data.value = "some value";
  dao.create(data);
  EntityWithCustomTypeId found = dao.queryForId(data.id);
  assertNotNull(found);
  assertEquals(found.id, data.id);
  assertEquals(found.value, data.value);
}

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

@Test
public void testExecuteRaw() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo1 = new Foo();
  assertEquals(1, dao.create(foo1));
  Foo foo2 = new Foo();
  assertEquals(1, dao.create(foo2));
  assertEquals(2, dao.queryForAll().size());
  dao.executeRaw("TRUNCATE TABLE FOO");
  assertEquals(0, dao.queryForAll().size());
}

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

@Test(expected = SQLException.class)
public void testDeletePreparedThrow() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo = new Foo();
  assertEquals(1, dao.create(foo));
  DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
  try {
    conn.close();
    dao.delete(dao.deleteBuilder().prepare());
  } finally {
    connectionSource.releaseConnection(conn);
  }
}

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

@Test(expected = SQLException.class)
public void testDeleteObjectsNoId() throws Exception {
  Dao<NoId, Object> noIdDao = createDao(NoId.class, true);
  NoId noId = new NoId();
  noId.stuff = "1";
  assertEquals(1, noIdDao.create(noId));
  ArrayList<NoId> noIdList = new ArrayList<NoId>();
  noIdList.add(noId);
  noIdDao.delete(noIdList);
}

代码示例来源:origin: magefree/mage

public void saveSets(final List<ExpansionInfo> newSets, final List<ExpansionInfo> updatedSets, long newContentVersion) {
  try {
    expansionDao.callBatchTasks(() -> {
        try {
          for (ExpansionInfo exp : newSets) {
            expansionDao.create(exp);
        try {
          for (ExpansionInfo exp : updatedSets) {
            expansionDao.update(exp);

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

/**
 * 查询表中的所有城市
 *
 * @return 城市列表数据
 */
public List<City> queryCityList() {
  try {
    return cityDaoOperation.queryForAll();
  } catch (SQLException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

//Get helper
DatabaseHelper helper = OpenHelperManager.getHelper(App.getContext(), DatabaseHelper.class);

//get dao
Dao dao = helper.getDao(YOUR_CLASS.class);

//delete elements from table in field by arg
DeleteBuilder<CanteenLog, Integer> deleteBuilder = dao.deleteBuilder();
deleteBuilder.where().eq("FIELD_NAME", arg);
deleteBuilder.delete();

相关文章