jeesite使用心得(二)
生活随笔
收集整理的這篇文章主要介紹了
jeesite使用心得(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按照上一篇的內容,有一些缺陷的地方。
分頁對象傳什么都可以,但是返回的是list<HashMap<String,Object>>集合的話,分頁方法就是無效了。
獲取前臺傳過來的頁碼和分頁數,這樣再set到page對象中,前臺就可以顯示正常的分頁和頁碼了。
jsp頁面,沒改過,就用他生成的就可以。
再說一下shiro的自定義標簽:
<shiro:hasAnyPermissions name="contract:busMddkApplication:edit,contract:busMddkApplication:add"></shiro:hasAnyPermissions>
<shiro:hasAnyPermissions ></shiro:hasAnyPermissions>這個標簽的意思是有任一權限就可以 <shiro:hasPermission name="contract:busMddkApplication:edit"><shiro:hasAnyPermissions ></shiro:hasAnyPermissions>這個標簽的意思是有某個權限
上面的意思是有任一權限就可以進入這個方法。
這是說一下分頁的問題,可以用他的page對象,會進入pageInterceptor的攔截器。感覺他查詢總數的方法有點問題。貼一下原來的和我改動的代碼。
//得到總記錄數
貼一下他原來的getCount()方法。
public static int getCount(final String sql, final Connection connection,final MappedStatement mappedStatement, final Object parameterObject,final BoundSql boundSql, Log log) throws SQLException {String dbName = Global.getConfig("jdbc.type");final String countSql;if("oracle".equals(dbName)){countSql = "select count(1) from (" + sql + ") tmp_count";//需要優化的sql}else{countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";}Connection conn = connection;PreparedStatement ps = null;ResultSet rs = null;try {if (log.isDebugEnabled()) {log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[]{"\n","\t"}, new String[]{" "," "}));}if (conn == null){conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();}ps = conn.prepareStatement(countSql);BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,boundSql.getParameterMappings(), parameterObject);//解決MyBatis 分頁foreach 參數失效 startif (Reflections.getFieldValue(boundSql, "metaParameters") != null) {MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");Reflections.setFieldValue(countBS, "metaParameters", mo);}//解決MyBatis 分頁foreach 參數失效 end SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);rs = ps.executeQuery();int count = 0;if (rs.next()) {count = rs.getInt(1);}return count;} finally {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (conn != null) {conn.close();}}}/** * 去除hql的orderBy子句。 * @param hql * @return */ @SuppressWarnings("unused")private static String removeOrders(String qlString) { Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(qlString); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); }m.appendTail(sb);return sb.toString(); }
?這樣生成的sql就是正常的條件查詢語句外面包了一層select count(1) from(原sql)。
? ? 但是在實際使用過程中,數據量比較大(百萬級別),并且查詢條件和參數很多的時候,這樣會很慢。不如直接select count(1) from (***) 這樣的快。
? ? 所以改寫了他的getCount()方法,在需要優化的sql的where后邊加上'needOptimization' = 'needOptimization'。
public static int getCount(final String sql, final Connection connection,final MappedStatement mappedStatement, final Object parameterObject,final BoundSql boundSql, Log log) throws SQLException {String dbName = Global.getConfig("jdbc.type");final String countSql;if("oracle".equals(dbName)){countSql = "select count(1) from (" + sql + ") tmp_count";//需要優化的sql}else if(sql.contains("needOptimization")){countSql = "select count(1) from "+ removeContent(sql);}else{countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";}Connection conn = connection;PreparedStatement ps = null;ResultSet rs = null;try {if (log.isDebugEnabled()) {log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[]{"\n","\t"}, new String[]{" "," "}));}if (conn == null){conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();}ps = conn.prepareStatement(countSql);BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,boundSql.getParameterMappings(), parameterObject);//解決MyBatis 分頁foreach 參數失效 startif (Reflections.getFieldValue(boundSql, "metaParameters") != null) {MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");Reflections.setFieldValue(countBS, "metaParameters", mo);}//解決MyBatis 分頁foreach 參數失效 end SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);rs = ps.executeQuery();int count = 0;if (rs.next()) {count = rs.getInt(1);}return count;} finally {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (conn != null) {conn.close();}}}/*** 將第一個from前的內容刪除* @Description:TODO* @author:gmwang* @time:2017年8月29日 上午10:16:28*/private static String removeContent(String sql) {String sqlAfter = StringUtils.substringAfter(sql,"FROM");return sqlAfter;}
如果原來是select count(1) from (
select a.*,b.* from aa a?
left join bb b on a.id = b.id
)
現在就改成了 select count(1) from aa a left join bb b on a.id = b.id
原來的sql執行24秒,現在大約是6秒。
分頁對象傳什么都可以,但是返回的是list<HashMap<String,Object>>集合的話,分頁方法就是無效了。
這里把我寫的貼一下:
Controller:
獲取前臺傳過來的頁碼和分頁數,這樣再set到page對象中,前臺就可以顯示正常的分頁和頁碼了。
jsp頁面:
jsp頁面,沒改過,就用他生成的就可以。
再說一下shiro的自定義標簽:
<shiro:hasAnyPermissions name="contract:busMddkApplication:edit,contract:busMddkApplication:add"></shiro:hasAnyPermissions>
<shiro:hasAnyPermissions ></shiro:hasAnyPermissions>這個標簽的意思是有任一權限就可以 <shiro:hasPermission name="contract:busMddkApplication:edit"><shiro:hasAnyPermissions ></shiro:hasAnyPermissions>這個標簽的意思是有某個權限
Controller:
上面的意思是有任一權限就可以進入這個方法。
這是說一下分頁的問題,可以用他的page對象,會進入pageInterceptor的攔截器。感覺他查詢總數的方法有點問題。貼一下原來的和我改動的代碼。
//得到總記錄數
common.persistence.interceptor.SQLHelper包下面,在PaginationInterceptor里邊先調用?
page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log));貼一下他原來的getCount()方法。
public static int getCount(final String sql, final Connection connection,final MappedStatement mappedStatement, final Object parameterObject,final BoundSql boundSql, Log log) throws SQLException {String dbName = Global.getConfig("jdbc.type");final String countSql;if("oracle".equals(dbName)){countSql = "select count(1) from (" + sql + ") tmp_count";//需要優化的sql}else{countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";}Connection conn = connection;PreparedStatement ps = null;ResultSet rs = null;try {if (log.isDebugEnabled()) {log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[]{"\n","\t"}, new String[]{" "," "}));}if (conn == null){conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();}ps = conn.prepareStatement(countSql);BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,boundSql.getParameterMappings(), parameterObject);//解決MyBatis 分頁foreach 參數失效 startif (Reflections.getFieldValue(boundSql, "metaParameters") != null) {MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");Reflections.setFieldValue(countBS, "metaParameters", mo);}//解決MyBatis 分頁foreach 參數失效 end SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);rs = ps.executeQuery();int count = 0;if (rs.next()) {count = rs.getInt(1);}return count;} finally {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (conn != null) {conn.close();}}}/** * 去除hql的orderBy子句。 * @param hql * @return */ @SuppressWarnings("unused")private static String removeOrders(String qlString) { Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(qlString); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); }m.appendTail(sb);return sb.toString(); }
?這樣生成的sql就是正常的條件查詢語句外面包了一層select count(1) from(原sql)。
? ? 但是在實際使用過程中,數據量比較大(百萬級別),并且查詢條件和參數很多的時候,這樣會很慢。不如直接select count(1) from (***) 這樣的快。
? ? 所以改寫了他的getCount()方法,在需要優化的sql的where后邊加上'needOptimization' = 'needOptimization'。
public static int getCount(final String sql, final Connection connection,final MappedStatement mappedStatement, final Object parameterObject,final BoundSql boundSql, Log log) throws SQLException {String dbName = Global.getConfig("jdbc.type");final String countSql;if("oracle".equals(dbName)){countSql = "select count(1) from (" + sql + ") tmp_count";//需要優化的sql}else if(sql.contains("needOptimization")){countSql = "select count(1) from "+ removeContent(sql);}else{countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";}Connection conn = connection;PreparedStatement ps = null;ResultSet rs = null;try {if (log.isDebugEnabled()) {log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[]{"\n","\t"}, new String[]{" "," "}));}if (conn == null){conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();}ps = conn.prepareStatement(countSql);BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,boundSql.getParameterMappings(), parameterObject);//解決MyBatis 分頁foreach 參數失效 startif (Reflections.getFieldValue(boundSql, "metaParameters") != null) {MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");Reflections.setFieldValue(countBS, "metaParameters", mo);}//解決MyBatis 分頁foreach 參數失效 end SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);rs = ps.executeQuery();int count = 0;if (rs.next()) {count = rs.getInt(1);}return count;} finally {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (conn != null) {conn.close();}}}/*** 將第一個from前的內容刪除* @Description:TODO* @author:gmwang* @time:2017年8月29日 上午10:16:28*/private static String removeContent(String sql) {String sqlAfter = StringUtils.substringAfter(sql,"FROM");return sqlAfter;}
如果原來是select count(1) from (
select a.*,b.* from aa a?
left join bb b on a.id = b.id
)
現在就改成了 select count(1) from aa a left join bb b on a.id = b.id
原來的sql執行24秒,現在大約是6秒。
總結
以上是生活随笔為你收集整理的jeesite使用心得(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSON.stringify的认知历程
- 下一篇: AngularJs $anchorScr