@Autowired
private RunSqlDao runSqlDao;
/**
* 读取文件内容到SQL中执行
* @param sqlPath SQL文件的路径:如:D:/TestProject/web/sql/脚本.Sql
*/
public void runSqlByReadFileContent(String sqlPath) throws Exception {
try {
String sqlStr = readFileByLines(sqlPath);
// System.out.println("获得的文本:" + sqlStr);
if (sqlStr.length() > 0) {
runSqlDao.runSqlBySqlStr(sqlStr);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
private String readFileByLines(String filePath) throws Exception {
StringBuffer str = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
// System.out.println("line " + line + ": " + tempString);
str = str.append(" " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return str.toString();
}
/**
* @param sqlStr
*/
public void runSqlBySqlStr(String sqlStr) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("sqlStr", sqlStr);
sqlSessionTemplate.selectList("runSql.runSqlBySqlStr", map);
}
<mapper namespace="runSql">
<select id="runSqlBySqlStr" parameterType="map">
<![CDATA[ ${sqlStr}]]>
</select>
</mapper>
/**
* 执行sql脚本文件 使用ScriptRunner
* @param sqlPath SQL文件的路径:如:D:/TestProject/web/sql/脚本.Sql
*/
public void runSqlByScriptRunner(String sqlPath) throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ScriptRunner runner = new ScriptRunner(conn);
runner.setEscapeProcessing(false);
runner.setSendFullScript(true);
runner.runScript(new InputStreamReader(new FileInputStream(sqlPath), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 执行sql脚本文件 使用Spring工具类
*/
public void runSqlBySpringUtils() throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
ScriptUtils.executeSqlScript(conn, rc);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 执行sql脚本文件 使用Spring工具类
*/
public void runSqlBySpringUtils() throws Exception {
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
EncodedResource er = new EncodedResource(rc, "utf-8");
ScriptUtils.executeSqlScript(conn, er);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有