日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jeesite使用心得(二)

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jeesite使用心得(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
按照上一篇的內容,有一些缺陷的地方。
分頁對象傳什么都可以,但是返回的是list<HashMap<String,Object>>集合的話,分頁方法就是無效了。

這里把我寫的貼一下:

Controller:

// 帶條件分頁Integer pageNo = null;Integer pageSize = null;if (request.getParameter("pageNo") != null) {pageNo = Integer.parseInt((String) request.getParameter("pageNo"));} else {page.setPageNo(1);pageNo = 1;}if (request.getParameter("pageSize") != null) {pageSize = Integer.parseInt((String) request.getParameter("pageSize"));} else {pageSize = 30;}page.setPageNo(pageNo);page.setPageSize(pageSize);//根據條件查到的集合List<Map<String, Object>> list = busMddkApplicationService.findPage(page, params);page.setList(list);model.addAttribute("page", page);
獲取前臺傳過來的頁碼和分頁數,這樣再set到page對象中,前臺就可以顯示正常的分頁和頁碼了。

jsp頁面:

<form:form id="searchForm" modelAttribute="baseBusMddkApplication" action="${ctx}/contract/busMddkApplication/" method="post" class="breadcrumb form-search"><input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/><input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/> </form:form>
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:

@RequiresPermissions(value= {"contract:busMddkApplication:active","contract:busMddkApplication:edit","contract:busMddkApplication:add","contract:busMddkApplication:info"}) @RequestMapping(value = "form")protected String form(HttpServletRequest request, BusMddkApplicationVo busMddkApplicationVo2, Model model,HttpServletResponse response, RedirectAttributes redirectAttributes) { }
上面的意思是有任一權限就可以進入這個方法。
這是說一下分頁的問題,可以用他的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使用心得(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。