一 前臺界面:java
前臺界面須要顯示一個商品類型管理的按鈕選項,此項目中是在menuBar中添加的按鈕鏈接git
點擊商品類型管理的按鈕進入一個列表界面,顯示數據庫中已經存在的商品類型列表,本頁面的功能有編輯,刪除,追加和取消。web
點擊編輯按鈕進入對商品類型的編輯,輸入內容爲空或添加商品類型與已有類型相同時提示錯誤信息。spring
點擊追加按鈕進入追加頁面添加時,一樣,輸入內容爲空或添加商品類型與已有類型相同時提示錯誤信息。
sql
二 後臺功能實現:數據庫
數據庫中添加一個商品類型的表commodity_type,並添加三個元素分別爲ID,類型名和備註:commodity_type_id,commodity_type_name,commodity_type_note;apache
在CommodityTypeForm .java中定義一下這三個變量,並使用標籤@Data直接引用Get,Set方法,不用再寫方法體。爲須要進行vailudation的屬性添加相應的標籤好比這裏的Name須要設置爲不能爲空或者不能與數據庫中已有的數據重複:session
@NotEmpty(field="商品類型", message="{errors.required}")app
@Digits ( message="{errors.similar}",fraction=0,integer=0)ui
private String commodityTypeName;
3.爲每一個頁面中的按鈕編寫實現功能的代碼,主要涉及到CommodityTypeController.java(主要涉及各頁面的跳轉和一些功能),CommodityTypeService.java(都是調用存放數據庫中數據的一些方法),CommodityTypeSqlMap.java(數據庫中所要進行的一些操做,如查詢,插入,更新數據的語句) 詳細代碼以下
CommodityTypeForm .java
package cn.agriculture.web.form; import cn.agriculture.common.validator.constraints.NotEmpty; import lombok.Data; @Data ---------------引用一個數據標籤,能夠不用寫GET,SET方法 public class CommodityTypeForm { private String commodityTypeId; @NotEmpty(field="商品類型", message="{errors.required}") private String commodityTypeName; private String commodityTypeNote; private String updateTime; }
CommodityTypeService.java
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.CommodityTypeForm; @Service public class CommodityTypeService { @Autowired QueryDAO queryDao; @Autowired UpdateDAO updateDao; public List<CommodityTypeForm> searchCommodityTypeList() { List<CommodityTypeForm> result = queryDao.executeForObjectList("Commodity_type.selectCommodityTypeList", null); return result; } public CommodityTypeForm searchCommodityType(CommodityTypeForm frm) { CommodityTypeForm result = queryDao.executeForObject("Commodity_type.selectCommodityType", frm, CommodityTypeForm.class); return result; } public CommodityTypeForm searchCommodityTypeName(CommodityTypeForm frm) { CommodityTypeForm result = queryDao.executeForObject("Commodity_type.selectCommodityTypeName", frm, CommodityTypeForm.class); return result; } public boolean addCommodityType(CommodityTypeForm frm) { // Integer sequee = queryDao.executeForObject("Commodity_type.getSeq", null, Integer.class); String commoditytypeId = frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee); frm.setCommodityTypeId(commoditytypeId); int result = updateDao.execute("Commodity_type.addCommodityType", frm); if (result == 1) { return true; } return false; } public boolean editCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("Commodity_type.updateCommodityType", frm); if (result == 1) { return true; } return false; } public boolean delCommodityType(CommodityTypeForm frm) { int result = updateDao.execute("Commodity_type.delCommodityType", frm); if (result == 1) { return true; } return false; } }
ItemListComponent.java
public class ItemListComponent { @Autowired QueryDAO queryDao; //設置下拉列表項----下拉列表中的標籤爲Label,顯示的值爲從列表中select出的數據,做爲value值 public List<Item> getCommodityTypeList() { return queryDao.executeForObjectList("Common.selectCommodityType", null); } public String getCommodityTypeLabel(String commoditytypeValue) { return queryDao.executeForObject("Common.selectCommodityTypeLabel", commoditytypeValue,String.class); } }
CommodityTypeSqlMap.java
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <!-- SqlMap定義 --> <sqlMap namespace="Commodity_type"> <select id="selectCommodityTypeList" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type.commodity_type_id as commodityTypeId, commodity_type.commodity_type_name as commodityTypeName, commodity_type.commodity_type_note as commodityTypeNote FROM commodity_type </select> <select id="selectCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm" resultClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type.commodity_type_id as commodityTypeId, commodity_type.commodity_type_name as commodityTypeName, commodity_type.commodity_type_note as commodityTypeNote FROM commodity_type WHERE commodity_type.commodity_type_id = #commodityTypeId# </select> <!-- 設定 commodityTypeId 自增1 --> <select id="getSeq" resultClass="java.lang.Integer"> SELECT _nextval('commodityTypeId') </select> <insert id="addCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> INSERT INTO commodity_type(commodity_type_id,commodity_type_name,commodity_type_note) VALUES(#commodityTypeId#, #commodityTypeName#, #commodityTypeNote#) *********//(檢驗輸入內容是否已存在數據庫中,不存在時能夠插入到數據庫中????)********* WHERE no exists (select * from commodity_type where commodity_type_name=commodity_type_name) </insert> <update id="updateCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> UPDATE commodity_type SET commodity_type_name= #commodityTypeName#, commodity_type_note= #commodityTypeNote# WHERE commodity_type_id = #commodityTypeId# </update> <select id="selectCommodityTypeName"parameterClass="cn.agriculture.web.form.CommodityTypeForm"> SELECT commodity_type.commodity_type_name as commodityTypeName FROM commodity_type </select> <delete id="delCommodityType" parameterClass="cn.agriculture.web.form.CommodityTypeForm"> DELETE FROM commodity_type WHERE commodity_type_id = #commodityTypeId# </delete> </sqlMap>
CommodityTypeController.java
package cn.agriculture.web.controller; @Slf4j @Controller("CommodityTypeController") @RequestMapping("/") @PropertySource("classpath:system.properties") public class CommodityTypeController { @Autowired CommodityTypeService commodityTypeService; @Autowired ItemListComponent itemListComponent; @Autowired private Environment env; //商品類型列表初始化 @RequestMapping(value = "initCommodityType", method = RequestMethod.GET) public String initCommodityType(Model model) { log.info("商品類型列表初始化"); model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; } @RequestMapping(value = "initAddCommodityType", method = RequestMethod.GET) public String initAddCommodityType(Model model) { log.info("追加商品列表初始化"); CommodityTypeForm commodityTypeForm = new CommodityTypeForm(); model.addAttribute("commodityTypeForm", commodityTypeForm); return "manager/commodityType/addCommodityType"; } @RequestMapping(value = "addCommodityType", method = RequestMethod.POST) public String executeAddCommodityType(Model model, HttpSession session, @Valid @ModelAttribute("commodityTypeForm") CommodityTypeForm commodityTypeForm, BindingResult results) throws SQLException, IOException { log.info("添加商品類型信息"); if(results.hasErrors()){ model.addAttribute("commodityTypeForm", commodityTypeForm); return "manager/commodityType/addCommodityType";} **************//驗證用戶在線,若超時讓用戶從新登陸*************** //UVO uvo = (UVO)session.getAttribute("UVO"); //commodityTypeForm.setUpdateUser(uvo.getUserName()); *************//設置時間更新用於以時間刷新ID ???? Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); commodityTypeForm.setUpdateTime(dateformat.format(date)); boolean result = commodityTypeService.addCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品類型添加失敗!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; } @RequestMapping(value = "initEditCommodityType", method = RequestMethod.GET) public String initEditCommodityType(Model model, CommodityTypeForm commodityTypeForm) { log.info("修改商品類型初始化"); CommodityTypeForm result = commodityTypeService.searchCommodityType(commodityTypeForm); model.addAttribute("commodityTypeForm", result); return "manager/commodityType/editCommodityType"; } @RequestMapping(value = "editCommodityType", method = RequestMethod.POST) public String executeEditCommodityType(Model model,HttpSession session, @Valid @ModelAttribute("commodityTypeForm") CommodityTypeForm commodityTypeForm, BindingResult results) throws SQLException, IOException { log.info("修改商品類型"); if(results.hasErrors()){ return "manager/commodityType/editCommodityType";} //UVO uvo = (UVO)session.getAttribute("UVO"); //commodityTypeForm.setUpdateUser(uvo.getUserName()); Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); commodityTypeForm.setUpdateTime(dateformat.format(date)); boolean result = commodityTypeService.selectCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品類型更新失敗!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; } @RequestMapping(value = "delCommodityType", method = RequestMethod.GET) public String executeDelCommodityType(Model model, CommodityTypeForm commodityTypeForm) throws SQLException { log.info("刪除商品信息"); boolean result = commodityTypeService.delCommodityType(commodityTypeForm); if(!result) { throw new SQLException("商品類型刪除失敗!"); } model.addAttribute("list", commodityTypeService.searchCommodityTypeList()); return "manager/commodityType/commodityTypeList"; } }
SqlMapConfig.xml (此文件主要管理全部的SqlMap文件)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <!-- SqlMapConfig定義 --> <sqlMapConfig> <settings useStatementNamespaces="true" /> <typeHandler jdbcType="BLOB" javaType="[B" callback="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler" /> <sqlMap resource="CommodityTypeSqlMap.xml" />