Atitit sql的执行功能
目录
2.4. 跨语言cli模式 Atitit 命令行执行sql 4
列表查询是一个列表,更新插入是返回受影响行数量int
建立工厂,获取session,相当于conn_session,执行sql
/ormHibernateDemo/src/pkg/sqlQueryHibernateMybatisDbutil.java
private static void mybatisQuery(String sql) {
String MYBATIS_XML = "mybatis.xml";
// 构建sqlSession 的工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(mybatisdemo.class.getClassLoader().getResourceAsStream(MYBATIS_XML));
SqlSession conn_sessioln = sqlSessionFactory.openSession(true);
List<Map> li = conn_sessioln.getMapper(MybatisMapperCls.class).query(sql);
System.out.println(JSON.toJSONString(li, true));
}
private static void dbutilQuery(String sql) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?serverTimezone=UTC", "root", "");
List<Map<String, Object>> list = new QueryRunner(true).query(conn, sql, new MapListHandler());
System.out.println(JSON.toJSONString(list, true));
}
private static void hbntQuery(String sql) {
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.mysql.xml").buildSessionFactory();
Session conn_sessioln = sessionFactory.openSession();
// conn_sessioln.
List li = conn_sessioln.createSQLQuery(sql).list();
System.out.println(JSON.toJSONString(li, true));
目录
1.1. 无需输入密码,那就不要-p参数即可 1
1.2. 4.使用mysql命令执行 1
1.3. 5.mysql命令执行sql,并将查询结果保存到文本中 2
1.4. 垂直打印输出,方便解析 2
1.5. XML方式输出 3
1.6. Tab模式输出 -s 3
1.7. Htnl格式显示 -H 3
//mysql.exe -uroot -D db -e "select * from db.user_tab" -X
private static void cliMysql(String sql) throws IOException {
String cmd="D:\\wampv2\\bin\\mysql\\mysql5.7.9\\bin\\mysql.exe -uroot -D db -e \"{0}\" -H";
cmd= MessageFormat.format(cmd, sql);
String rzt = IOUtils.toString(Runtime.getRuntime().exec(cmd).getInputStream(), "gbk");
System.out.println(rzt);
}