java分页插件valuelist
在這里給大家分享一個java的分頁插件,valuelist,具體源代碼可以查看附件。
首先介紹下該插件有什么功能:
1、不用我們在sql中寫具體的分頁語句,如mysql,我們不必去寫limit ?,?這樣的代碼,這些代碼組件會自動拼裝上
2、支持自定義多條件查詢,即如果我參數(shù)中不傳要查詢的字段值,在查詢時會去掉該查詢條件,當(dāng)然這要在sql語句中進(jìn)行一個簡單的配置。
?
基本功能就是上述兩種,個人認(rèn)為還是比較好用的,尤其是第二點,當(dāng)我們綜合查詢時,不確定哪些字段要查詢時,是非常方便的。在網(wǎng)上搜valuelist,可能還會有文章介紹它還帶有相應(yīng)的頁面標(biāo)簽,我們使用的是不支持標(biāo)簽的,僅用來方便查詢。
?
下面介紹使用方法:首先說明下該組件是將sql寫到xml文件中的
1、首先這個組件可能依賴的jar包有以下幾個,以gradle語法列出:
compile ("commons-beanutils:commons-beanutils:1.9.1")compile ("org.springframework:spring-jdbc:4.0.0.RELEASE")
compile ("org.springframework:spring-context:4.0.0.RELEASE")
compile ("org.slf4j:slf4j-api:1.7.7") //同時加上你項目中l(wèi)og的實現(xiàn),如log4j
compile("mysql:mysql-connector-java:5.1.29")
compile("com.alibaba:druid:1.0.15") //也可以換成別的連接池,如c3p0、dbcp等
因為該組件是依賴于spring的,所以需要引入spring相關(guān)jar包。
2、將該組件源代碼(見附件)復(fù)制進(jìn)入項目中,或者自己打個jar包放入項目中
3、配置數(shù)據(jù)庫連接池,以下僅為參考,自己可配置自己項目使用的連接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"destroy-method="close" init-method="init"><property name="driverClassName" value="${datasource.driverClassName}" /><property name="url" value="${datasource.url}" /><property name="username" value="${datasource.username}" /><property name="password" value="${datasource.password}" /><property name="initialSize" value="${datasource.initial.size}" /><property name="minIdle" value="${datasource.min.idle}" /><property name="maxActive" value="${datasource.max.active}" /><!-- 配置獲取連接等待超時的時間 --><property name="maxWait" value="${datasource.max.wait}" /><!-- 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="${datasource.time.eviction}" /><!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --><property name="minEvictableIdleTimeMillis" value="${datasource.min.eviction}" /><property name="validationQuery" value="${datasource.validation.query}" /><property name="testWhileIdle" value="${datasource.test.while.idle}" /><property name="testOnBorrow" value="${datasource.test.borrow}" /><property name="testOnReturn" value="${datasource.test.return}" /></bean>?4、配置valuelist用到的類,可以新建一個valuelist.xml放入你項目spring掃描的配置文件路徑下
<bean id="rowMapper" class="org.springframework.jdbc.core.ColumnMapRowMapper" /><bean id="sqlPagingSupport" class="net.mlw.vlh.adapter.jdbc.util.SqlPagingSupport"><!--配置使用的數(shù)據(jù)庫,只測試過mysql,其余數(shù)據(jù)庫未測試,可修改源代碼增加自己實現(xiàn)--><property name="database" value="mysql" /> </bean><!--下面配置具體查詢的類,也是配置sql語句的地方--> <bean id="getShareListByUserIdAndCate"class="net.mlw.vlh.adapter.jdbc.spring.SpringDaoValueListAdapter"><!--指定數(shù)據(jù)庫連接池--><property name="dataSource" ref="dataSource" /><property name="rowMapper" ref="rowMapper" /><property name="sqlPagingSupport" ref="sqlPagingSupport" /><!--默認(rèn)每頁記錄數(shù)--><property name="defaultNumberPerPage" value="20" /><!--執(zhí)行查詢時,是否打印sql語句,調(diào)試時可打開該選項--><property name="showSql" value="true" /><!--配置要查詢的sql語句--><property name="sql"><value> <!--可以看到以下條件都被/~~/包住了,這里面的條件即你傳入該參數(shù)就根據(jù)該參數(shù)查詢,你不傳的話就不根據(jù)該條件查詢。請注意:冒號前面的名字,如authorId,要和大括號里面的名字保持一致--><![CDATA[select `id`,`content`,`tag`,`type`,`pId`,`pData`,`authorId`,`authorName`,`createTime`,cmtCnt as cmtCount from share_[table] where 1=1 /~authorId: and authorId = {authorId} ~//~cateId: and cateId = {cateId} ~/order by createTime desc]]></value></property></bean> <!--若有另外查詢,則再定義個類--><bean id="getUserByUserName"class="net.mlw.vlh.adapter.jdbc.spring.SpringDaoValueListAdapter"><property name="dataSource" ref="dataSource" /><property name="rowMapper" ref="rowMapper" /><property name="sqlPagingSupport" ref="sqlPagingSupport" /><property name="defaultNumberPerPage" value="20" /><property name="showSql" value="true" /><property name="sql"><value><!--像這種不/~~/里面的條件,則為每次都會執(zhí)行的條件,所以realName這個參數(shù)是必須傳入的,而userName則可以不傳入,不傳即不根據(jù)此條件查詢--><![CDATA[select `id`,`userName`,`realName`,`idNum`,`signature`,`desc` from user where 1=1 realName={realName} /~userName: AND `userName` like '%[userName]%' ~/ ]]></value></property></bean><!--配置完了sql語句,還需要有以下的一個小小的配置--> <bean id="valueListHandler" class="net.mlw.vlh.DefaultValueListHandlerImpl"><property name="config.adapters"><map><!-- 將剛才配置的sql的bean注入到這里,在代碼調(diào)用時,則需要使用這里配置的key值 --><entry key="getUserByUserName" value-ref="getUserByUserName" /><entry key="getShareListByUserIdAndCate" value-ref="getShareListByUserIdAndCate" /></map></property></bean>?
?以上即為配置文件,請保證項目啟動時,spring能夠掃描到以上配置,我們配置的net.mlw.vlh.adapter.jdbc.spring.SpringDaoValueListAdapter類可以想象為一個個的查詢方法,不同的sql則定義多個這樣的類。
5、最后,我們看代碼的使用
如我們使用第一個getShareListByUserIdAndCate查詢:
/*** 注入要使用的類,即我們在配置文件中配置的 net.mlw.vlh.DefaultValueListHandlerImpl*/@Autowiredprivate ValueListHandler handler;public void getShare(int pageIndex,int pageSize){//定義map存放查詢參數(shù)Map<String,Object> paras=new HashMap<String, Object>();//設(shè)置查詢頁碼,須將pageIndex轉(zhuǎn)換為stringparas.put("pagingPage", String.valueOf(pageIndex));//設(shè)置每頁記錄數(shù)paras.put("pagingNumberPer",String.valueOf(pageSize));//設(shè)置sql中的查詢參數(shù),key即配置文件sql語句中{}中的值,如果sql配置中查詢條件沒有在/~~/之內(nèi),則必須傳入;paras.put("authorId","123");paras.put("cateId","567");//執(zhí)行查詢,第一個參數(shù)即我們最后配置的valueListHandler中注入的map中對應(yīng)getShareListByUserIdAndCate這個sql bean配置的key值//即 <entry key="getShareListByUserIdAndCate" value-ref="getShareListByUserIdAndCate" /> 這里對應(yīng)的keyValueList list=handler.getValueList("getUserByUserName", new ValueListInfo(paras));//以上ValueList對象里包含很多字段,如記錄總數(shù),查詢條件,查詢結(jié)果等//查詢結(jié)果,每條記錄以Map形式返回List<Map<String,Object>> res=list.getList();//記錄總數(shù)int count=list.getValueListInfo().getTotalNumberOfEntries();}?
以上就是該組件的使用方式,在進(jìn)行很多個條件查詢時,確實帶來了很大的方便;若是以上有什么敘述的不到位的,或者不明白的,請指出,我進(jìn)行改正。
?
?
?
- valuelist.zip (77 KB)
- 描述: valuelist源代碼
- 下載次數(shù): 13
總結(jié)
以上是生活随笔為你收集整理的java分页插件valuelist的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android .9图片使用报错...报
- 下一篇: break的用法