Atitit 数据库结果集映射 ResultSetHandler 目录 1. 常见的四种配置ResultSetHandler 1 2. Dbutil 1 3. Mybatis 致敬 3 4. H
Atitit 数据库结果集映射 ResultSetHandler
目录
ColumnListHandler
MapListHandler
MapHandler map to a object map
ScalarHandler map to a object value
package miniCodePrjPkg;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
//import com.wix.mysql.EmbeddedMysql;
//
//import static com.wix.mysql.EmbeddedMysql.anEmbeddedMysql;
//import static com.wix.mysql.ScriptResolver.classPathScript;
//import static com.wix.mysql.distribution.Version.v5_7_latest;
public class DslQueryCollList {
public static void main(String[] args) throws Exception {
// apache comm coll cant ,only array is ok..cant json_object eff
// Map m=Maps.
Map myMap = Maps.newHashMap(ImmutableMap.of("name", 999999999, "age", 22));
Map myMap2 = Maps.newHashMap(ImmutableMap.of("name", 8888888, "age", 33));
List li = new ImmutableList.Builder().add(myMap).add(myMap2).build();
System.out.println(li);
// /db/xx.sql
// EmbeddedMysql mysqld = anEmbeddedMysql(v5_7_latest)
// .addSchema("aschema", classPathScript("iniListCache.sql"))
// .start();
// this just start..and u need a cliednt as common to conn..looks trouble than
// sqlite
Class.forName("org.sqlite.JDBC");
Connection c = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement stmt = c.createStatement();
String sql2 = "drop TABLE sys_data ";
exeUpdateSafe(stmt, sql2);
sql2 = "CREATE TABLE sys_data (jsonfld json )";
exeUpdateSafe(stmt, sql2);
// insert into facts values(json_object("mascot", "Our mascot is a dolphin name
// sakila"));
//
String sql = "insert into sys_data values('{\"id\":\"19\", \"name\":\"Lida\"}');";
exeUpdateSafe(stmt, sql);
sql="SELECT json_extract(jsonfld,'$.name') as name1 FROM sys_data limit 1;" ;
System.out.println(sql);
QueryRunner run = new QueryRunner();
//maphandler scare_handler
List<Map<String, Object>> query = run.query(c,sql, new MapListHandler());
System.out.println(query);
// run.query(conn, sql, rsh)
}
private static void exeUpdateSafe(Statement stmt, String sql2) throws SQLException {
try {
System.out.println(sql2);
System.out.println(stmt.executeUpdate(sql2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
直接LIst<Map》银蛇
public class hiberDemo {
public static void main(String[] args) throws Exception {
// org.hibernate.dialect.Dialect.getDialect()
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session1 = sessionFactory.openSession();
String sql = "select * from tab1";
org.hibernate.query.Query q = session1.createSQLQuery(sql);
List li = q.list();
System.out.println(JSON.toJSONString(li, true));
System.out.println("ok");
}
- PDO::query() 主要是用于有记录结果返回的操作,特别是SELECT操作
- PDO::exec() 主要是针对没有结果集合返回的操作,如INSERT、UPDATE等操作
- PDO::prepare() 主要是预处理操作,需要通过$rs->execute()来执行预处理里面的SQL语句,这个方法可以绑定参数,功能比较强大(防止sql注入就靠这个)
- PDO::lastInsertId() 返回上次插入操作,主键列类型是自增的最后的自增ID
- PDOStatement::fetch() 是用来获取一条记录
- PDOStatement::fetchAll() 是获取所有记录集到一个集合
- PDOStatement::fetchColumn() 是获取结果指定第一条记录的某个字段,缺省是第一个字段
Atitit php pdo的api使用
Atitit hibernate5 使用总结