Skip to content

Commit 66ce407

Browse files
committed
分页插件和Mapper升级到最新版本,增加分页插件4.0.3版本特性示例。
1 parent f16eb06 commit 66ce407

File tree

10 files changed

+196
-2
lines changed

10 files changed

+196
-2
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<!-- 编译jdk版本 -->
2323
<jdk.version>1.6</jdk.version>
2424
<!-- 依赖版本 -->
25-
<mapper.version>3.2.3-SNAPSHOT</mapper.version>
26-
<pagehelper.version>4.0.0</pagehelper.version>
25+
<mapper.version>3.3.0</mapper.version>
26+
<pagehelper.version>4.0.3</pagehelper.version>
2727
<mysql.version>5.1.29</mysql.version>
2828
<spring.version>4.1.2.RELEASE</spring.version>
2929
</properties>

src/main/java/com/isea533/mybatis/controller/demo/CountryController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.pagehelper.PageInfo;
44
import com.isea533.mybatis.model.Country;
5+
import com.isea533.mybatis.model.CountryQueryModel;
56
import com.isea533.mybatis.service.CountryService;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Controller;
@@ -39,6 +40,24 @@ public ModelAndView getList(Country country,
3940
return result;
4041
}
4142

43+
/**
44+
* 测试pageinfo,参数必须包含pageNum和pageSize
45+
* <p/>
46+
* 这个方法以及调用的相关方法看起来可能很难理解,如果不理解,就不要使用这种方式!
47+
* <p/>
48+
* 例如http://[baseurl]/pageInfo?pageNum=1&pageSize=20&countryname=Ch
49+
*
50+
* @param queryModel
51+
* @return
52+
*/
53+
@RequestMapping(value = "pageInfo", produces = "application/json")
54+
public ModelAndView pageInfo(CountryQueryModel queryModel) {
55+
ModelAndView result = new ModelAndView();
56+
PageInfo<Country> pageInfo = countryService.selectByCountryQueryModel(queryModel);
57+
result.addObject("pageInfo", pageInfo);
58+
return result;
59+
}
60+
4261
@RequestMapping(value = "view", method = RequestMethod.GET)
4362
public ModelAndView view(Country country) {
4463
ModelAndView result = new ModelAndView();

src/main/java/com/isea533/mybatis/mapper/CountryMapper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@
2424

2525
package com.isea533.mybatis.mapper;
2626

27+
import com.github.pagehelper.PageInfo;
2728
import com.isea533.mybatis.model.Country;
29+
import com.isea533.mybatis.model.CountryQueryModel;
2830
import com.isea533.mybatis.util.MyMapper;
2931

3032
public interface CountryMapper extends MyMapper<Country> {
33+
34+
/**
35+
* 分页查询
36+
*
37+
* @param queryModel
38+
* @return
39+
*/
40+
PageInfo<Country> selectByCountryQueryModel(CountryQueryModel queryModel);
3141
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 abel533@gmail.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.isea533.mybatis.model;
26+
27+
public class CountryQueryModel extends QueryModel {
28+
29+
private Country country;
30+
31+
public CountryQueryModel() {
32+
this(new Country());
33+
}
34+
35+
public CountryQueryModel(Country country) {
36+
this.country = country;
37+
}
38+
39+
public Integer getId() {
40+
return country.getId();
41+
}
42+
43+
public void setId(Integer id) {
44+
country.setId(id);
45+
}
46+
47+
public String getCountrycode() {
48+
return country.getCountrycode();
49+
}
50+
51+
public void setCountrycode(String countrycode) {
52+
country.setCountrycode(countrycode);
53+
}
54+
55+
public String getCountryname() {
56+
return country.getCountryname();
57+
}
58+
59+
public void setCountryname(String countryname) {
60+
country.setCountryname(countryname);
61+
}
62+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.isea533.mybatis.model;
2+
3+
/**
4+
* @author liuzh_3nofxnp
5+
* @since 2015-11-09 22:25
6+
*/
7+
public class QueryModel {
8+
private Integer pageNum;
9+
private Integer pageSize;
10+
private String orderBy;
11+
private Boolean countSql;
12+
private Boolean pageSizeZero;
13+
private Boolean reasonable;
14+
15+
public Integer getPageNum() {
16+
return pageNum;
17+
}
18+
19+
public void setPageNum(Integer pageNum) {
20+
this.pageNum = pageNum;
21+
}
22+
23+
public Integer getPageSize() {
24+
return pageSize;
25+
}
26+
27+
public void setPageSize(Integer pageSize) {
28+
this.pageSize = pageSize;
29+
}
30+
31+
public String getOrderBy() {
32+
return orderBy;
33+
}
34+
35+
public void setOrderBy(String orderBy) {
36+
this.orderBy = orderBy;
37+
}
38+
39+
public Boolean getCountSql() {
40+
return countSql;
41+
}
42+
43+
public void setCountSql(Boolean countSql) {
44+
this.countSql = countSql;
45+
}
46+
47+
public Boolean getPageSizeZero() {
48+
return pageSizeZero;
49+
}
50+
51+
public void setPageSizeZero(Boolean pageSizeZero) {
52+
this.pageSizeZero = pageSizeZero;
53+
}
54+
55+
public Boolean getReasonable() {
56+
return reasonable;
57+
}
58+
59+
public void setReasonable(Boolean reasonable) {
60+
this.reasonable = reasonable;
61+
}
62+
}

src/main/java/com/isea533/mybatis/service/CountryService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.isea533.mybatis.service;
22

3+
import com.github.pagehelper.PageInfo;
34
import com.isea533.mybatis.model.Country;
5+
import com.isea533.mybatis.model.CountryQueryModel;
46

57
import java.util.List;
68

@@ -20,4 +22,12 @@ public interface CountryService extends IService<Country> {
2022
*/
2123
List<Country> selectByCountry(Country country, int page, int rows);
2224

25+
/**
26+
* 分页查询,分页插件4.0.3版本特性演示
27+
*
28+
* @param queryModel
29+
* @return
30+
*/
31+
PageInfo<Country> selectByCountryQueryModel(CountryQueryModel queryModel);
32+
2333
}

src/main/java/com/isea533/mybatis/service/impl/BaseService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public abstract class BaseService<T> implements IService<T> {
3838
@Autowired
3939
protected Mapper<T> mapper;
4040

41+
public Mapper<T> getMapper() {
42+
return mapper;
43+
}
44+
4145
@Override
4246
public T selectByKey(Object key) {
4347
return mapper.selectByPrimaryKey(key);

src/main/java/com/isea533/mybatis/service/impl/CountryServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.isea533.mybatis.service.impl;
22

33
import com.github.pagehelper.PageHelper;
4+
import com.github.pagehelper.PageInfo;
5+
import com.isea533.mybatis.mapper.CountryMapper;
46
import com.isea533.mybatis.model.Country;
7+
import com.isea533.mybatis.model.CountryQueryModel;
58
import com.isea533.mybatis.service.CountryService;
69
import org.springframework.stereotype.Service;
710
import tk.mybatis.mapper.entity.Example;
@@ -34,4 +37,8 @@ public List<Country> selectByCountry(Country country, int page, int rows) {
3437
return selectByExample(example);
3538
}
3639

40+
@Override
41+
public PageInfo<Country> selectByCountryQueryModel(CountryQueryModel queryModel) {
42+
return ((CountryMapper) getMapper()).selectByCountryQueryModel(queryModel);
43+
}
3744
}

src/main/resources/applicationContext.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@
7474
<property name="plugins">
7575
<array>
7676
<bean class="com.github.pagehelper.PageHelper">
77+
<!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
7778
<property name="properties">
7879
<value>
7980
dialect=mysql
8081
reasonable=true
82+
supportMethodsArguments=true
83+
returnPageInfo=check
84+
params=count=countSql
8185
</value>
8286
</property>
8387
</bean>

src/main/resources/mapper/CountryMapper.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,20 @@
3333
<result column="countryname" property="countryname" jdbcType="VARCHAR" />
3434
<result column="countrycode" property="countrycode" jdbcType="VARCHAR" />
3535
</resultMap>
36+
37+
<select id="selectByCountryQueryModel" resultMap="BaseResultMap">
38+
select id,countryname,countrycode from country
39+
<where>
40+
<if test="countryname != null and countryname != ''">
41+
countryname like concat('%',concat(#{countryname}, '%'))
42+
</if>
43+
<if test="countrycode != null and countrycode != ''">
44+
and countrycode like concat('%',concat(#{countrycode}, '%'))
45+
</if>
46+
<if test="id != null">
47+
and id = #{id}
48+
</if>
49+
</where>
50+
51+
</select>
3652
</mapper>

0 commit comments

Comments
 (0)