从数据库中查询一系列数据的方法。
1.1 SQL语句
SELECT
express_price_id as expressPriceId,
express_id as expressId,
province_id as provinceId,
separate_weight as separateWeight,
first_heavy_price as firstHeavyPrice,
continued_heavy_price as continuedHeavyPrice,
status as status,
note as note,
update_time as updateTime,
update_user as updateUser
FROM express_price
WHERE express_price_id = #expressPriceId#
<sqlMap namespace="Common">
<select id="selectProvince"
resultClass="cn.agriculture.web.form.Item">
SELECT province_name as label,
province_id as value
FROM province
</select>
2. 2Java方法
a. 在controller中
@RequestMapping(value = "initEditExpressPrice", method = RequestMethod.GET)
public String initEditExpressPrice(Model model, ExpressPriceForm expressPriceForm) {
log.info("修改快递商所属价格初始化");
List<Item> provinceList = itemListComponent.getProvinceList();
model.addAttribute("provinceList", provinceList);
model.addAttribute("expressPriceForm", expressPriceService.searchExpressPrice(expressPriceForm));
return "manager/expressPrice/editExpressPrice";
}
b.在expressPriceForm.java (当然是要写在包中) 中
package cn.agriculture.web.form; import javax.validation.constraints.Digits; import cn.agriculture.common.validator.constraints.NotEmpty; import lombok.Data; @Data public class ExpressPriceForm { private String expressPriceId; private String expressId; private String provinceId; private String provinceName; @Digits(fraction = 2, integer = 10,message="{errors.separateWeight}") @NotEmpty(field="分隔重量g",message="{errors.required}") private String separateWeight; @Digits(fraction = 2, integer = 10,message="{errors.firstHeavyPrice}") @NotEmpty(field="首重费(元)",message="{errors.required}") private String firstHeavyPrice; @Digits(fraction = 2, integer = 10,message="{errors.continuedHeavyPrice}") @NotEmpty(field="续重费(元)",message="{errors.required}") private String continuedHeavyPrice; private String status; private String note; private String updateTime; private String updateUser; }
c.(Service中)在searchExpressPrice方法中
Common.selectProvince
getProvinceList()
package cn.agriculture.web.service; import java.util.List; import jp.terasoluna.fw.dao.QueryDAO; import jp.terasoluna.fw.dao.UpdateDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.agriculture.web.form.ExpressPriceForm; @Service public class ExpressPriceService { @Autowired QueryDAO queryDao; @Autowired UpdateDAO updateDao; public ExpressPriceForm searchExpressPrice(ExpressPriceForm frm) { ExpressPriceForm result = queryDao.executeForObject("ExpressPrice.selectExpressPrice ", frm, ExpressPriceForm.class); return result; } public List<Item> getProvinceList() { return queryDao.executeForObjectList("Common.selectProvince", null); } }
d.在manager/expressPrice/editExpressPrice.jsp 页面中
<form:select path="provinceId" itemValue="${expressPriceForm.provinceId}">
<form:options items="${provinceList}" itemLabel="label" itemValue="value" />
</form:select>
<form:input path="ProvinceName" value="${expressPriceForm.ProvinceName}" />
<div class="box-content"> <form:form modelAttribute="expressPriceForm" action="editExpressPrice" method="post"> <form:hidden path="expressId" value="${expressPriceForm.expressId}" /> <form:hidden path="expressPriceId" value="${expressPriceForm.expressPriceId}" /〉 <div class="alert alert-info">${message}<form:errors path="*"></form:errors></div> <div class="controls"> <form:select path="provinceId" itemValue="${expressPriceForm.provinceId}"> <form:options items="${provinceList}" itemLabel="label" itemValue="value" /> </form:select> <form:input path="ProvinceName" value="${expressPriceForm.ProvinceName}" /> </div> </form:form>
2.怎么用一个表的Id查询另一个表中的属性(两表关联)
2.1 SQL 语句如下所示
SELECT
express_price.express_price_id as expressPriceId,
express_price.express_id as expressId,
express_price.province_id as provinceId,
province.province_name as provinceName,
express_price.separate_weight as separateWeight,
express_price.first_heavy_price as firstHeavyPrice,
express_price.continued_heavy_price as continuedHeavyPrice,
express_price.status as status,
express_price.note as note,
express_price.update_time as updateTime,
express_price.update_user as updateUser
FROM express_price,province
WHERE
province.province_id=express_price.province_id
AND express_price_id = #expressPriceId#
2.2 Service controller Form都同上
在就jsp中用EL表达式显示
<div class="controls">
${expressPriceForm.provinceName}
</div>