diff --git a/src/main/java/com/isea533/mybatis/controller/demo/CountryController.java b/src/main/java/com/isea533/mybatis/controller/demo/CountryController.java index 028970c..264d452 100644 --- a/src/main/java/com/isea533/mybatis/controller/demo/CountryController.java +++ b/src/main/java/com/isea533/mybatis/controller/demo/CountryController.java @@ -2,6 +2,8 @@ import com.github.pagehelper.PageInfo; import com.isea533.mybatis.model.Country; +import com.isea533.mybatis.model.Order; +import com.isea533.mybatis.model.OrderSort; import com.isea533.mybatis.service.CountryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -39,6 +41,24 @@ public ModelAndView getList(Country country, return result; } + @RequestMapping(value = "orderlist") + public ModelAndView getOrderList(Country country, + @RequestParam(required = false, defaultValue = "1") int page, + @RequestParam(required = false, defaultValue = "10") int rows) { + ModelAndView result = new ModelAndView(page_list); + + //按照countryname降序排序 + Order order = new Order(); + order.addOrder("countryname", OrderSort.DESC); + + List countryList = countryService.selectCountryByParam(country, order,page, rows); + result.addObject("pageOrderInfo", new PageInfo(countryList)); + result.addObject("queryParam", country); + result.addObject("page", page); + result.addObject("rows", rows); + return result; + } + @RequestMapping(value = "view", method = RequestMethod.GET) public ModelAndView view(Country country) { ModelAndView result = new ModelAndView(); diff --git a/src/main/java/com/isea533/mybatis/mapper/CountryMapper.java b/src/main/java/com/isea533/mybatis/mapper/CountryMapper.java index 6b9b997..dbe8baf 100644 --- a/src/main/java/com/isea533/mybatis/mapper/CountryMapper.java +++ b/src/main/java/com/isea533/mybatis/mapper/CountryMapper.java @@ -25,8 +25,14 @@ package com.isea533.mybatis.mapper; import com.isea533.mybatis.model.Country; +import com.isea533.mybatis.model.Order; import com.isea533.mybatis.util.MyMapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; public interface CountryMapper extends MyMapper { + List selectListByParam(@Param("pojo") Country pojo, + @Param("orderObj") Order order, int page, int rows); } \ No newline at end of file diff --git a/src/main/java/com/isea533/mybatis/model/Order.java b/src/main/java/com/isea533/mybatis/model/Order.java new file mode 100644 index 0000000..533e109 --- /dev/null +++ b/src/main/java/com/isea533/mybatis/model/Order.java @@ -0,0 +1,46 @@ +package com.isea533.mybatis.model; + +import org.apache.commons.lang.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2016/7/27. + */ +public class Order { + + private List orders = new ArrayList(); + + public List getOrders() { + return orders; + } + + public void setOrders(List orders) { + this.orders = orders; + } + + public void addOrder(String orderName, OrderSort orderSort) { + orders.add(new OrderBy(orderName, orderSort.getType())); + } + + //每个排序都有个index。没用到 +// public void addIndexOrder(int index, String orderName, OrderSort orderSort) { +// int c = orders.size(); +// if (index < 0 || (index != 0 && index > c - 1)) +// return; +// orders.add(index, new OrderBy(orderName, orderSort.getType())); +// } + + @Override + public String toString() { + String orderStr = ""; + for (OrderBy o : orders) { + orderStr += o.getOrderName() + " " + o.getOrderSort() + ","; + } + + orderStr = StringUtils.substringBeforeLast(orderStr, ","); + return orderStr.length() < 1 ? "" : " order by " + orderStr; + + } +} diff --git a/src/main/java/com/isea533/mybatis/model/OrderBy.java b/src/main/java/com/isea533/mybatis/model/OrderBy.java new file mode 100644 index 0000000..806d995 --- /dev/null +++ b/src/main/java/com/isea533/mybatis/model/OrderBy.java @@ -0,0 +1,31 @@ +package com.isea533.mybatis.model; + +/** + * Created by Administrator on 2016/7/27. + */ +public class OrderBy { + + private String orderName; + private String orderSort; + + public OrderBy(String orderName, String orderSort) { + this.orderName = orderName; + this.orderSort = orderSort; + } + + public String getOrderName() { + return orderName; + } + + public void setOrderName(String orderName) { + this.orderName = orderName; + } + + public String getOrderSort() { + return orderSort; + } + + public void setOrderSort(String orderSort) { + this.orderSort = orderSort; + } +} diff --git a/src/main/java/com/isea533/mybatis/model/OrderSort.java b/src/main/java/com/isea533/mybatis/model/OrderSort.java new file mode 100644 index 0000000..797e358 --- /dev/null +++ b/src/main/java/com/isea533/mybatis/model/OrderSort.java @@ -0,0 +1,20 @@ +package com.isea533.mybatis.model; + +/** + * Created by Administrator on 2016/7/27. + */ +public enum OrderSort { + ASC("asc"), + + DESC("desc"); + + private String orderType; + + private OrderSort(String orderType) { + this.orderType = orderType; + } + + public String getType() { + return orderType; + } +} diff --git a/src/main/java/com/isea533/mybatis/service/CountryService.java b/src/main/java/com/isea533/mybatis/service/CountryService.java index 67bb6bf..899f74f 100644 --- a/src/main/java/com/isea533/mybatis/service/CountryService.java +++ b/src/main/java/com/isea533/mybatis/service/CountryService.java @@ -1,6 +1,8 @@ package com.isea533.mybatis.service; import com.isea533.mybatis.model.Country; +import com.isea533.mybatis.model.Order; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -20,4 +22,7 @@ public interface CountryService extends IService { */ List selectByCountry(Country country, int page, int rows); + List selectCountryByParam(@Param("pojo") Country pojo, + @Param("orderObj") Order order, int page, int rows); + } diff --git a/src/main/java/com/isea533/mybatis/service/impl/CountryServiceImpl.java b/src/main/java/com/isea533/mybatis/service/impl/CountryServiceImpl.java index 2b4ba11..5f25fc4 100644 --- a/src/main/java/com/isea533/mybatis/service/impl/CountryServiceImpl.java +++ b/src/main/java/com/isea533/mybatis/service/impl/CountryServiceImpl.java @@ -1,8 +1,12 @@ package com.isea533.mybatis.service.impl; import com.github.pagehelper.PageHelper; +import com.isea533.mybatis.mapper.CountryMapper; import com.isea533.mybatis.model.Country; +import com.isea533.mybatis.model.Order; import com.isea533.mybatis.service.CountryService; +import org.apache.ibatis.annotations.Param; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.util.StringUtil; @@ -16,6 +20,9 @@ @Service("countryService") public class CountryServiceImpl extends BaseService implements CountryService { + @Autowired + CountryMapper countryMapper; + @Override public List selectByCountry(Country country, int page, int rows) { Example example = new Example(Country.class); @@ -34,4 +41,9 @@ public List selectByCountry(Country country, int page, int rows) { return selectByExample(example); } + @Override + public List selectCountryByParam(Country country, Order order, int page, int rows) { + PageHelper.startPage(page, rows); + return countryMapper.selectListByParam(country, order, page, rows); + } } diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index 92559c2..bc1bd68 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -24,6 +24,7 @@ # \u6570\u636E\u5E93\u914D\u7F6E jdbc.driverClass = com.mysql.jdbc.Driver +#дԼݿַʺź jdbc.url = jdbc:mysql://192.168.16.137:3306/test jdbc.user = root jdbc.password = diff --git a/src/main/resources/mapper/CountryMapper.xml b/src/main/resources/mapper/CountryMapper.xml index 4d718ae..27136d8 100644 --- a/src/main/resources/mapper/CountryMapper.xml +++ b/src/main/resources/mapper/CountryMapper.xml @@ -34,6 +34,15 @@ + + + Id,countryname,countrycode + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp index 4723224..ac26a62 100644 --- a/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -19,6 +19,17 @@ $('#list').click(function () { $('.pageDetail').toggleClass('show'); }); + $("#DescSearch").click(function () { + $.ajax({ + type: "POST", + url: "${pageContext.request.contextPath}/orderlist", + data: $("#forminfo").serialize(), + dataType:"json", + success:function(data) { + window.location.href ="${pageContext.request.contextPath}/orderlist"; + } + }); + }); }); @@ -28,14 +39,18 @@

国家(地区)列表

-
+ - + + @@ -157,6 +172,56 @@
国家(地区)名称: 国家(地区)代码: +
+ +
页码:
+ + + + + + + + + + + + + + + + + + + + + + +
排序后查询结果 - [新增国家(地区)]
ID国家(地区)名国家(地区)代码操作
${country.id}${country.countryname}${country.countrycode}[修改] - + [删除] +
+ + + + + + + + + + + + + + + + + +
+ 前一页 + ${nav} + ${nav} + + 下一页 +