Package com.xiaoleilu.hutool.db

Examples of com.xiaoleilu.hutool.db.SqlRunner


  private static void sqlRunnerDemo(DataSource ds) {
    Entity entity = Entity.create(TABLE_NAME).set("字段1", "值").set("字段2", 2);
    Entity where = Entity.create(TABLE_NAME).set("条件1", "条件值");

    try {
      SqlRunner runner = SqlRunner.create(ds);
      // 根据DataSource会自动识别数据库方言
      runner = SqlRunner.create(ds);

      // 增,生成SQL为 INSERT INTO `table_name` SET(`字段1`, `字段2`) VALUES(?,?)
      runner.insert(entity);

      // 删,生成SQL为 DELETE FROM `table_name` WHERE `条件1` = ?
      runner.del(where);

      // 改,生成SQL为 UPDATE `table_name` SET `字段1` = ?, `字段2` = ? WHERE `条件1` = ?
      runner.update(entity, where);

      // 查,生成SQL为 SELECT * FROM `table_name` WHERE WHERE `条件1` = ? 第一个参数为返回的字段列表,如果null则返回所有字段
      List<Entity> entityList = runner.find(null, where, new EntityHandler());
      log.info("{}", entityList);

      // 分页,注意,ANSI SQL中不支持分页!
      List<Entity> pagedEntityList = runner.page(null, where, 0, 20, new EntityHandler());
      log.info("{}", pagedEntityList);

      // 满足条件的结果数,生成SQL为 SELECT count(1) FROM `table_name` WHERE WHERE `条件1` = ?
      int count = runner.count(where);
      log.info("count: {}", count);
    } catch (SQLException e) {
      Log.error(log, e, "SQL error!");
    } finally {
    }
View Full Code Here

TOP

Related Classes of com.xiaoleilu.hutool.db.SqlRunner

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.