瀏覽代碼

support mysql datasource

sunyj 8 年之前
父節點
當前提交
ac582dbb83

+ 4 - 0
pom.xml

@@ -85,6 +85,10 @@
 			<artifactId>ojdbc6</artifactId>
 			<version>${oracle.jdbc.version}</version>
 		</dependency>
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+		</dependency>
 
 		<!-- 打印报表 -->
 		<dependency>

+ 22 - 11
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -1,5 +1,7 @@
 package com.uas.report.service.impl;
 
+import com.alibaba.druid.pool.DruidDataSource;
+import com.alibaba.druid.util.JdbcUtils;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.SpecialProperties;
 import com.uas.report.SystemProperties;
@@ -107,7 +109,7 @@ public class PrintServiceImpl implements PrintService {
 		// throw new ReportException(e).setDetailedMessage(e);
 		// }
 
-		DataSource dataSource = MasterManager.getDataSource(userName, profile);
+		DruidDataSource dataSource = MasterManager.getDataSource(userName, profile);
 		if (dataSource == null) {
 			throw new SQLException("获取数据源失败");
 		}
@@ -137,7 +139,7 @@ public class PrintServiceImpl implements PrintService {
 		Connection connection = null;
 		try {
 			connection = dataSource.getConnection();
-			String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, false, otherParameters, connection);
+			String jasperFilePath = maybeCompileJrxmlFile(jrxmlFilePath, false, otherParameters, connection, dataSource.getDbType());
 			if(StringUtils.isEmpty(jasperFilePath)){
 				throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath);
 			}
@@ -222,13 +224,14 @@ public class PrintServiceImpl implements PrintService {
 	 *            是否为子报表
 	 * @param otherParameters
 	 * @param connection
+	 * @param dbType 数据库类型
 	 * @return 编译后的jasper文件路径
 	 * @throws IOException
 	 * @throws SQLException
 	 * @throws DocumentException
 	 */
 	private String maybeCompileJrxmlFile(String jrxmlFilePath, boolean isSub, String otherParameters,
-			Connection connection) throws IOException, DocumentException, SQLException {
+			Connection connection, String dbType) throws IOException, DocumentException, SQLException {
 		if (StringUtils.isEmpty(jrxmlFilePath)) {
 			logger.error("参数不能为空:" + jrxmlFilePath);
 			return null;
@@ -243,7 +246,7 @@ public class PrintServiceImpl implements PrintService {
 		// 首先解析jrxml文件,检查是否引用子报表,如果是,先(递归)编译子报表
 		List<String> subJrxmlFilePaths = getSubJrxmlFilePath(jrxmlFilePath);
 		for (String subJrxmlFilePath : subJrxmlFilePaths) {
-			maybeCompileJrxmlFile(subJrxmlFilePath, true, otherParameters, connection);
+			maybeCompileJrxmlFile(subJrxmlFilePath, true, otherParameters, connection, dbType);
 		}
 
 		String jasperFilePath = jrxmlFilePath.replace(".jrxml", ".jasper");
@@ -253,15 +256,15 @@ public class PrintServiceImpl implements PrintService {
 			// 报表模板未编译过
 			if (!jasperFile.exists()) {
 				logger.info("正在编译报表模板... " + jrxmlFilePath);
-				compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection);
+				compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection, dbType);
 			}
 			// 如果在编译之后,报表模板有更改过 ,重新编译
 			else if (jrxmlFile.lastModified() > jasperFile.lastModified()) {
 				logger.info("正在重新编译报表模板... " + jrxmlFilePath);
-				compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection);
+				compileReportToFile(jrxmlFilePath, isSub, jasperFilePath, otherParameters, connection, dbType);
 			}
 		} catch (JRException e) {
-			throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath + " " + ExceptionUtils.getDetailedMessage(e));
+			throw new IllegalStateException("编译报表模板失败: " + jrxmlFilePath, e);
 		}
 		return jasperFilePath;
 	}
@@ -307,6 +310,7 @@ public class PrintServiceImpl implements PrintService {
 	 * @param otherParameters
 	 * @param connection
 	 *            数据库连接
+	 * @param dbType 数据库类型
 	 * @throws JRException
 	 * @throws IOException
 	 * @throws FileNotFoundException
@@ -314,12 +318,12 @@ public class PrintServiceImpl implements PrintService {
 	 * @throws SQLException
 	 */
 	private void compileReportToFile(String jrxmlFilePath, boolean isSub, String jasperFilePath, String otherParameters,
-			Connection connection)
+			Connection connection, String dbType)
 			throws JRException, FileNotFoundException, IOException, DocumentException, SQLException {
 		replaceFont(jrxmlFilePath);
 		// 不处理子报表,因为其参数来自主报表,无法处理
 		if (!isSub) {
-			processFields(jrxmlFilePath, otherParameters, connection);
+			processFields(jrxmlFilePath, otherParameters, connection, dbType);
 		}
 		JasperCompileManager.compileReportToFile(jrxmlFilePath, jasperFilePath);
 	}
@@ -330,11 +334,12 @@ public class PrintServiceImpl implements PrintService {
 	 * @param jrxmlFilePath
 	 * @param otherParameters
 	 * @param connection
+	 * @param dbType
 	 * @throws IOException
 	 * @throws DocumentException
 	 * @throws SQLException
 	 */
-	private void processFields(String jrxmlFilePath, String otherParameters, Connection connection)
+	private void processFields(String jrxmlFilePath, String otherParameters, Connection connection, String dbType)
 			throws IOException, DocumentException, SQLException {
 		XMLWriter xmlWriter = null;
 		try {
@@ -346,7 +351,13 @@ public class PrintServiceImpl implements PrintService {
 			String queryString = queryStringElement.getText();
 			// 如果查询语句中含有WHERE_CONDITION参数,需将其替换掉
 			if (queryString.contains("$P!{WHERE_CONDITION}")) {
-				queryString = queryString.replace("$P!{WHERE_CONDITION}", "where rownum = 0");
+				if (JdbcUtils.MYSQL.equals(dbType)) {
+					queryString = queryString.replace("$P!{WHERE_CONDITION}", "limit 0, 1");
+				} else if (JdbcUtils.ORACLE.equals(dbType)) {
+					queryString = queryString.replace("$P!{WHERE_CONDITION}", "where rownum = 0");
+				} else{
+					throw new IllegalStateException("不支持的数据库类型:" + dbType);
+				}
 			}
 			queryString = replaceOtherParameters(queryString, otherParameters);
 			// 因为获取列名只需要在编译模版时执行一次,所以这里获取数据速度虽然较慢,但是可以接受,

+ 6 - 7
src/main/java/com/uas/report/util/MasterManager.java

@@ -7,7 +7,6 @@ import com.uas.report.model.Master;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -20,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * 账套及其数据源管理
- * 
+ *
  * @author sunyj
  * @since 2016年11月4日 下午4:33:56
  */
@@ -59,7 +58,7 @@ public class MasterManager {
 
 	/**
 	 * 获取账套对应的数据源
-	 * 
+	 *
 	 * @param userName
 	 *            账套名称
 	 * @param profile
@@ -67,7 +66,7 @@ public class MasterManager {
 	 * @return 数据源
 	 * @throws SQLException
 	 */
-	public static DataSource getDataSource(String userName, String profile) throws SQLException {
+	public static DruidDataSource getDataSource(String userName, String profile) throws SQLException {
 		if (StringUtils.isEmpty(userName)) {
 			return null;
 		}
@@ -95,11 +94,11 @@ public class MasterManager {
 
 	/**
 	 * 从主账套的MA_USER表中获取子账套数据源
-	 * 
+	 *
 	 * @param mainMaster
 	 * @param userName
 	 * @return
-	 * @throws SQLException 
+	 * @throws SQLException
 	 */
 	private static DruidDataSource getDataSourceFromMainMaster(DruidDataSource mainMaster, String userName) throws SQLException {
 		Connection connection = null;
@@ -160,7 +159,7 @@ public class MasterManager {
 
 	/**
 	 * 根据账套名获取账套
-	 * 
+	 *
 	 * @param userName
 	 *            账套名称
 	 * @return 账套