將dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query10。接下來,在JdbcDaoTest類里添加如下方法:
@DataProvider
public Collection<DemoUser> query10(){
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
Demo_USER D";
return this.getJdbcTemplate().query(sql, new DemoUserRowMapper());
}
1.3.2.帶Map參數(shù)的NamedParameterJdbcTemplate條件查詢
首先,在JdbcDaoTest.view.xml視圖中,添加一個id為將textUsername的TextEditor和一個查詢按鈕,并在查詢按鈕的onClick事件中添加如下代碼:
var username=view.get("#textUsername.value");
var dataSetDemoUser=view.get("#dataSetDemoUser");
if(username){
dataSetDemoUser.set("parameter",{username:username}).flushAsync();
}
dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query11。接下來,在JdbcDaoTest類里添加如下方法:
@DataProvider
public Collection<DomeUser> query11(String username){
Map<String,Object> map =new HashMap<String,Object>();
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
DOME_USER D ";
if(username!=null&&!"".equals(username)){
map.put("username", "%"+username+"%");
sql+="WHERE D.USERNAME_ LIKE :username";
}
return this.getNamedParameterJdbcTemplate().query(sql, map, new DomeUserRowMapper());
}
1.3.3.無查詢條件的分頁查詢
將dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query12,并將dataSetDemoUser的pageSize設(shè)置為10,再添加一個分頁工具DataPilot,并設(shè)置dataSet屬性為dataSetDemoUser,接下來,在JdbcDaoTest類里添加如下方法:
@DataProvider
public Collection<DemoUser> query12(){
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
Demo_USER D";
return this.getJdbcTemplate().query(sql, new DemoUserRowMapper());
}
1.3.4.帶Map參數(shù)的條件分頁查詢
dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query13。接下來,在JdbcDaoTest類里添加如下方法:
@DataProvider
public void query13(Page<DemoUser> page,String username){
Map<String,Object> map =new HashMap<String,Object>();
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
DOME_USER D ";
if(username!=null&&!"".equals(username)){
map.put("username", username);
sql+=" WHERE D.USERNAME_=:username";
}
this.pagingQuery(page, sql, new DomeUserRowMapper(),map);
}
代碼說明:JdbcDao對象的pagingQuery(Page<?>page,Stringsql,RowMapper<?>mapper,Map<String,Object>parameters)方法,可以實現(xiàn)帶參數(shù)的查詢,參數(shù)放到Map集合當(dāng)中,可以放入多查詢參數(shù)。
1.3.5.帶數(shù)組參數(shù)的條件分頁查詢
將dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query14。接下來,在JdbcDaoTest類里添加如下方法:
@DataProvider
public void query14(Page<DemoUser> page,String username){
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
DOME_USER D ";
if(username!=null&&!"".equals(username)){
sql+=" WHERE D.USERNAME_=?";
this.pagingQuery(page, sql,new Object[]{username},new DomeUserRowMapper());
}else{
this.pagingQuery(page, sql,new DomeUserRowMapper());
}
}
代碼說明:此段代碼的功能和上一節(jié)代碼的功能是一樣的,只是這里傳遞查詢參數(shù)是通過Object數(shù)組來實現(xiàn)。但是這種方式對查詢參數(shù)的順序有嚴(yán)格的要求,不推薦使用。
1.3.6. 結(jié)合DataGrid的過濾工具欄實現(xiàn)分頁查詢
將dataSetDemoUser的dataProvider屬性設(shè)置為jdbcDaoTest#query15,并將gridDemoUser的filterMode和showFilterBar屬性分別設(shè)置為serverSide和true。接下來,在 JdbcDaoTest類里添加如下方法:
@DataProvider
public void query15(Page<DemoUser> page,Criteria criteria){
String sql="SELECT D.USERNAME_,D.MALE_,D.BIRTHDAY_,D.MOBILE_,D.ADDRESS_,D.EMAIL_ FROM
Demo_USER D ";
ParseResult result=this.parseCriteria(criteria,false,"D");
if(result!=null){
StringBuffer sb=result.getAssemblySql();
Map<String,Object> valueMap=result.getValueMap();
sql+=" WHERE "+sb.toString();
this.pagingQuery(page,sql,valueMap.values().toArray(),new DemoUserRowMapper());
}else{
this.pagingQuery(page,sql,new DemoUserRowMapper());
}
}
@Override
protected String buildFieldName(String name) {
if(name==null)return name;
StringBuffer sb=new StringBuffer();
for(char ch:name.toCharArray()){
boolean upper=Character.isUpperCase(ch);
sb.append(Character.toUpperCase(ch));
if(upper){
sb.append("_");
}
}
sb.append("_");
return sb.toString();
}
代碼說明:parseCriteria方法的作用是將Criteria對象的相應(yīng)成分映射到ParseResult的assemblySql和valueMap屬性中,以方便我們使用JdbcDao的相關(guān)API,其中assemblySql屬性是查詢語句的where子句部分,且不包括where關(guān)鍵字。而valueMap則是對應(yīng)的查詢參數(shù)。buildFieldName方法是對父類AbstractDao中的buildFieldName模板方法的重寫,此模板方法是parseCriteria方法內(nèi)部在拼湊查詢語句的where子句時調(diào)用的,作用是根據(jù)實體類的屬性名生成對應(yīng)的數(shù)據(jù)庫字段名,默認(rèn)實現(xiàn)是生成的數(shù)據(jù)庫字段名和屬性名相同,通過在子類對其重寫,可以實現(xiàn)自定義的從實體屬性名到數(shù)據(jù)庫字段名的映射。
更多建議: