`
PlayGod1984
  • 浏览: 158215 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【五】

阅读更多
第五篇、各层的结合运用
====================================================================
本人比较懒,所以里面只写了一个jsp页面,测试的时候用来改改form的action,然后看效果。但是java类里面的方法我还是写完了。
production.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html> 

<head><title>添加产品</title> </head> 

<body> 

	<form:form action="addProduction.do" commandName="production">
		<table>
	        <tr>
	            <td>产品ID</td>
	            <td><input type="text" name="productionid"/></td>
	        </tr>
	        <tr>
	            <td>产品名称</td>
	            <td><input type="text" name="productionname" /></td>
	        </tr>
	        <tr>
	            <td>产品价格</td>
	            <td><input type="text" name="price" /></td>
	        </tr>
	        <tr>
	            <td>产品规格</td>
	            <td><input type="text" name="detail" /></td>
	        </tr>
	        <tr>
	            <td colspan="3">
	                <input type="submit" value="DELETE RECORD" />
	            </td>
	        </tr>
	    </table>	
	</form:form>
</body> 

</html> 

用了一些spring的标签,spring:text没用,总是报错误,网上也没有一个合理的答案,我猜可能还是需要form之类的东西吧,等我研究我再补上。
======================空开 for spring 标签问题开始======================
空开 for spring 标签问题
======================空开 for spring 标签问题结束======================


Controller

package com.qdcl.mart.business.web;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qdcl.mart.business.domain.Production;
import com.qdcl.mart.business.service.IProductionService;

@Controller
@RequestMapping("/")
public class ProductionController {
	@Autowired
	private IProductionService productionService;

	@RequestMapping(value="/production",method=RequestMethod.GET)
	public String getProduct(Model model){
		System.out.println("fuck all");
		productionService.getProductionByName("aaa");
		
		model.addAttribute("production", productionService.getProductionByName("bbb"));
		return "production"; 
	}
	
	@RequestMapping(value="/addProduction")
	public String addProduction(Production production){
		
		productionService.saveProduction(production);
		return "production";
	}
	@RequestMapping(value="/editProduction")
	public String editProduction(Model model){
		Production production =  new Production();
		productionService.updateProduction(production);
		
		
		return "production";
	}
	@RequestMapping(value="/deleteProduction")
	public String deleteProduction(String productionid){
		List<String> listIds = new ArrayList<String>();
		listIds.add(productionid);
		productionService.deleteProduction(listIds);
		
		
		return "production";
	}

}

这里面我一直不明白的是,当调用后台处理的方法,直接把参数写到xxx方法中即可,但是用private Production production;然后getter/setter的方法居然是null。费解,因为还没深入研究mvc的种种。所以只好又是一段空白。

Service 以及 ServiceImpl
package com.qdcl.mart.business.service;



import java.util.List;

import com.qdcl.mart.business.domain.Production;


public interface IProductionService {
	
	public Production getProductionByName(String pName);
	
	public boolean saveProduction(Production production);
	
	public boolean updateProduction(Production production);
	
	public boolean deleteProduction(List<String> listIds);
}



package com.qdcl.mart.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qdcl.mart.business.domain.Production;
import com.qdcl.mart.business.persistence.ProductionMapper;
import com.qdcl.mart.business.service.IProductionService;

@Service
public class ProductionServiceImpl implements IProductionService {
	@Autowired
	private ProductionMapper productionMapper;
	/**
	 * 
	 * @param pName
	 * @return
	 */
	public Production getProductionByName(String pName){	
		Production production = productionMapper.selectProductionByName("bbb");
		return production;
	}
	/**
	 * 返回整数
	 */
	public boolean saveProduction(Production production) {
//		productionMapper.insertProduction(production);
//		production.setProductionid("10000001");
//		production.setPrice(new BigDecimal(1.0));
//		production.setProductionname("笔记本");
//		production.setDetail("IBM Thinkpad R400");
		int success = productionMapper.insertProduction(production);
		return success > 0;
	}
	/**
	 * 更新
	 */
	public boolean updateProduction(Production production) {
//		production.setProductionid("10000001");
//		production.setPrice(new BigDecimal(1.0));
//		production.setProductionname("笔记本");
//		production.setDetail("IBM Thinkpad R400");
		int success = productionMapper.updateProduction(production);
		return success > 0;
	}
	/**
	 * 删除
	 */
	public boolean deleteProduction(List<String> listIds) {
		int success = 0;
		for(int i =0; i < listIds.size(); i++){
			success += productionMapper.deleteProduction(listIds.get(i));
		}
		return success > 0;
	}
}



Mapper类
package com.qdcl.mart.business.persistence;



import com.qdcl.mart.business.domain.Production;
 
public interface ProductionMapper {
	/**
	 * 检索一个产品
	 * @param pName
	 */
	public Production selectProductionByName(String pName);
	
	/**
	 * 插入产品
	 * @param p
	 */
	public int insertProduction(Production p);
	/**
	 * 
	 * @param p
	 * @return
	 */
	public int updateProduction(Production p);
	/**
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteProduction(String id);

}


Domain类,看到这个类就知道我的表结构了,太简单,sql语句就不贴了。
package com.qdcl.mart.business.domain;

import java.math.BigDecimal;

public class Production {
	/**
	 * 产品ID
	 */
	private String productionid;
	/**
	 * 产品名称
	 */
	private String productionname;
	/**
	 * 价格
	 */
	private BigDecimal price;
	/**
	 * 明细
	 */
	private String detail;
	public String getProductionid() {
		return productionid;
	}
	public void setProductionid(String productionid) {
		this.productionid = productionid;
	}
	public String getProductionname() {
		return productionname;
	}
	public void setProductionname(String productionname) {
		this.productionname = productionname;
	}
	public BigDecimal getPrice() {
		return price;
	}
	public void setPrice(BigDecimal price) {
		this.price = price;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	
}



好了,至此这些东西都贴完了,一步一步走来,还有很多东西去学习。另外把代码也附上来,如果哪个跟我一样的刚刚入门,可以拿来研究研究。共同进步嘛,呵呵。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics