1、数据库中表的创建(id自增)
agriculture.commodity_type
commodity_type_id VARCHAR(45) Primary Key Not Null
commodity_type_name VARCHAR(45)
commodity_type_note VARCHAR(45)
agriculture.tb_sequence(name中的commodityTypeId为commodity_type_id )
name current_value _increment
注意:此处的id不能设置为int,因为此处的id为自增,自增的值为 添加日期(4位)+添加记录的条数(11位)
2、获取自增id的方法
(CommodityTypeController.java CommodityTypeService.java CommodityTypeForm.java CommodityTypeSqlMap)
CommodityTypeController.java
Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); commodityTypeForm.setUpdateTime(dateformat.format(date));
CommodityTypeService.java
Integer sequee = queryDao.executeForObject("CommodityType.getSeq", null, Integer.class); String commodity_typeId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee); frm.setCommodity_typeId(commodity_typeId);
CommodityTypeSqlMap.xml
<select id="getSeq" resultClass="java.lang.Integer"> SELECT _nextval('commodityTypeId') ------与tb_sequence相联系 </select>
CommodityTypeForm.java
private String updateTime;
3、列表显示的相关操作(以editCommodityType.jsp为例)
editCommodityType.jsp
<td> <div class="controls"> <form:select path="commodity_typeId" itemValue="${commodityTypeForm.commodity_typeId}"> <form:options id="commodity_typeId" items="${commodityTypeList}" itemLabel="label" itemValue="value" /> </form:select> </div> </td>
ItemListComponent.java
public List<Item> getCommodityTypeList() { return queryDao.executeForObjectList("Common.selectCommodityType", null); } public String getCommodityTypeLabel(String commodityTypeValue) { return queryDao.executeForObject("Common.selectCommodityTypeLabel", commodityTypeValue, String.class); }
CommonSqlMap.xml
<select id="selectCommodityType" parameterClass="java.lang.String" resultClass="cn.agriculture.web.form.Item"> SELECT commodity_type_name as label,commodity_type_id as value FROM commodity_type </select> <select id="selectCommodityTypeLabel" parameterClass="java.lang.String" resultClass="java.lang.String"> SELECT commodity_type_name as label FROM commodity_type WHERE commodity_type_id = #value# </select>