Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增数据库建表sql,并对config.properties添加注释 #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Country> countryList = countryService.selectCountryByParam(country, order,page, rows);
result.addObject("pageOrderInfo", new PageInfo<Country>(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();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/isea533/mybatis/mapper/CountryMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Country> {

List selectListByParam(@Param("pojo") Country pojo,
@Param("orderObj") Order order, int page, int rows);
}
46 changes: 46 additions & 0 deletions src/main/java/com/isea533/mybatis/model/Order.java
Original file line number Diff line number Diff line change
@@ -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<OrderBy> orders = new ArrayList<OrderBy>();

public List<OrderBy> getOrders() {
return orders;
}

public void setOrders(List<OrderBy> 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;

}
}
31 changes: 31 additions & 0 deletions src/main/java/com/isea533/mybatis/model/OrderBy.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/isea533/mybatis/model/OrderSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/isea533/mybatis/service/CountryService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,4 +22,7 @@ public interface CountryService extends IService<Country> {
*/
List<Country> selectByCountry(Country country, int page, int rows);

List selectCountryByParam(@Param("pojo") Country pojo,
@Param("orderObj") Order order, int page, int rows);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +20,9 @@
@Service("countryService")
public class CountryServiceImpl extends BaseService<Country> implements CountryService {

@Autowired
CountryMapper countryMapper;

@Override
public List<Country> selectByCountry(Country country, int page, int rows) {
Example example = new Example(Country.class);
Expand All @@ -34,4 +41,9 @@ public List<Country> 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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
30 changes: 30 additions & 0 deletions src/main/resources/mapper/CountryMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
<result column="countrycode" property="countrycode" jdbcType="VARCHAR" />
</resultMap>

<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Sat Jul 02 19:32:40 CST 2016.
-->
Id,countryname,countrycode
</sql>

<select id="selectByCountryQueryModel" resultMap="BaseResultMap">
select id,countryname,countrycode from country
<where>
Expand All @@ -49,4 +58,25 @@
</where>

</select>

<select id="selectListByParam" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from country
where 1 = 1
<if test="pojo != null and pojo.countryname != null and pojo.countryname != ''">
and countryname like concat('%',concat(#{countryname}, '%'))
</if>
<if test="pojo != null and pojo.countrycode != null and pojo.countrycode != ''">
and countrycode like concat('%',concat(#{countrycode}, '%'))
</if>
<if test="pojo != null and pojo.id != null">
and id = #{id}
</if>
<if test="orderObj != null">
order by
<foreach collection="orderObj.orders" item="obj" separator=",">
${obj.orderName} ${obj.orderSort}
</foreach>
</if>
</select>
</mapper>
69 changes: 67 additions & 2 deletions src/main/webapp/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
});
});
});

</script>
Expand All @@ -28,14 +39,18 @@
<div class="middle">
<h1 style="padding: 50px 0 20px;">国家(地区)列表</h1>

<form action="${pageContext.request.contextPath}/list" method="post">
<form id="forminfo" action="${pageContext.request.contextPath}/list" method="post">
<table class="gridtable" style="width:100%;">
<tr>
<th>国家(地区)名称:</th>
<td><input type="text" name="countryname" value="${queryParam.countryname}"/></td>
<th>国家(地区)代码:</th>
<td><input type="text" name="countrycode" value="${queryParam.countrycode}"/></td>
<td rowspan="2"><input type="submit" value="查询"/></td>
<td rowspan="2"><input type="submit" value="查询"/>
<br>
<button id="DescSearch">倒序查询</button>
</td>

</tr>
<tr>
<th>页码:</th>
Expand Down Expand Up @@ -157,6 +172,56 @@
</c:if>
</tr>
</table>
<table class="gridtable" style="width:100%;">
<thead>
<tr>
<th colspan="4">排序后查询结果 - [<a href="${pageContext.request.contextPath}/view">新增国家(地区)</a>]</th>
</tr>
<tr>
<th>ID</th>
<th>国家(地区)名</th>
<th>国家(地区)代码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageOrderInfo.list}" var="country">
<tr>
<td>${country.id}</td>
<td>${country.countryname}</td>
<td>${country.countrycode}</td>
<td style="text-align:center;">[<a
href="${pageContext.request.contextPath}/view?id=${country.id}">修改</a>] -
[<a href="${pageContext.request.contextPath}/delete?id=${country.id}">删除</a>]
</td>
</tr>
</c:forEach>
</tbody>
</table>
<table class="gridtable" style="width:100%;text-align: center;">
<tr>
<c:if test="${pageOrderInfo.hasPreviousPage}">
<td>
<a href="${pageContext.request.contextPath}/orderlist?page=${pageOrderInfo.prePage}&rows=${pageOrderInfo.pageSize}&countryname=${queryParam.countryname}&countrycode=${queryParam.countrycode}">前一页</a>
</td>
</c:if>
<c:forEach items="${pageOrderInfo.navigatepageNums}" var="nav">
<c:if test="${nav == pageOrderInfo.pageNum}">
<td style="font-weight: bold;">${nav}</td>
</c:if>
<c:if test="${nav != pageOrderInfo.pageNum}">
<td>
<a href="${pageContext.request.contextPath}/orderlist?page=${nav}&rows=${pageOrderInfo.pageSize}&countryname=${queryParam.countryname}&countrycode=${queryParam.countrycode}">${nav}</a>
</td>
</c:if>
</c:forEach>
<c:if test="${pageOrderInfo.hasNextPage}">
<td>
<a href="${pageContext.request.contextPath}/orderlist?page=${pageOrderInfo.nextPage}&rows=${pageOrderInfo.pageSize}&countryname=${queryParam.countryname}&countrycode=${queryParam.countrycode}">下一页</a>
</td>
</c:if>
</tr>
</table>
</c:if>
</div>
<div class="push"></div>
Expand Down