通過頁面去操作資料庫並且在資料庫裡面去新增資料
ProductController 是 用來接收引數 返回引數和跳轉頁面的作用 ,————————ProductService 作用是被controllerd呼叫裡面的方法去處理資料並呼叫Mapper來通過Mapper去呼叫資料庫資料而它只是呼叫Mapper裡面的方法————————IProductService 是作為父介面被ProductService 實現的————————ProductMapper裡面寫的是介面的
方法介面沒有方法體是介面中的方法是抽象的抽象方法是沒有方法體的最後通過ProductMapper。xml下面的進行增刪改查的操作。
ProductController
package com.project.portal.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.dal.entity.Product;
import com.project.service.IProductService;
@Controller
public class ProductController {
@Autowired
private IProductService productService;
@RequestMapping(value="/addProduct",method=RequestMethod.POST)
@ResponseBody
public String addProduct(@RequestParam("title") String title,
@RequestParam("price") int price
,@RequestParam("status") int status,
@RequestParam("brief") String brief,@RequestParam("addTime") long addTime ){
productService.addProduct (title,price,status,brief,addTime);
return null;
}
@RequestMapping(value="/addProduct",method=RequestMethod.GET)
public String addProduct(){
return "product/addProduct";
}
@RequestMapping(value="/getProduct")
@ResponseBody
public List<Product> getProduct(){
return productService.getProduct();
}
@RequestMapping(value="/getProductCount")
@ResponseBody
public int getProductCount(){
return productService.getProductCount();
}
@RequestMapping(value="/getProductByPrice")
@ResponseBody
public List<Product> getProductByPrice(
@RequestParam("price") int price){
return productService.getProductByPrice(price) ;
}
2 ProductService
用來別contoNr所呼叫的方法並且裡面的Mapper是去呼叫資料庫的方法
public class ProductService implements IProductService{
@Autowired
private ProductMapper productMapper;
@Override
public void addProduct() {
// TODO Auto-generated method stub
Product product = new Product();
product.setAddTime(12L);
product.setBrief("brief");
product.setPid(10);
product.setPrice(13);
product.setStatus(0);
product.setTitle("duo");
productMapper.addProduct(product);
}
@Override
public List<Product> getProduct() {
return productMapper.getProduct();//注意要加 productMapper.方法
// TODO Auto-generated method stub
}
public int getProductCount(){
return productMapper.getProductCount();//注意要加 productMapper.方法
}
@Override
public void addProduct(String title, int price, int status, String brief,
long addTime) {
Product product = new Product();
product.setTitle(title);
product.setPrice(price);
product.setStatus(status);
product.setBrief(brief);
product.setAddTime(addTime);
productMapper.addProduct(product);
// TODO Auto-generated method stub
}
@Override
public List<Product> getProductByPrice() {
return productMapper.getProductByPrice();
}
@Override
public List<Product> getProductByPrice(int price) {//要把引數型別定義好
// TODO Auto-generated method stub
return productMapper.getProductByPrice( price);//傳入引數price
}
}
3IProductService
ackage com.project.service;
import java.util.List;
import com.project.dal.entity.Product;
public interface IProductService {
void addProduct();
List<Product> getProduct ();
int getProductCount();
void addProduct(String title, int price, int status, String brief,
long addTime);
List<Product> getProductByPrice();
List<Product> getProductByPrice(int price);//是定義一個LIST集合是product型別
4product Mapper .xml裡面寫的關於頁面和資料庫增刪改查的操作的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.project.dal.mapper.ProductMapper">
<resultMap id="ProductMap" type="Product">
<id property="pid" column="pid" />
<result property="title" column="title"/>
<result property="price" column="price"/>
<result property="status" column="status"/>
<result property="brief" column="brief"/>
<result property="addTime" column="add_time"/>
</resultMap>
<insert id="addProduct" statementType="PREPARED">
insert into product(title,price,status,brief,add_time) values(#{title},#{price},#{status},#{brief},#{addTime})
</insert>
<select id="getProduct" statementType="PREPARED" resultMap="ProductMap">
select * from product
</select>
<select id="getProductCount" statementType="PREPARED" resultType="Integer">
select count(*) from product
</select>
<select id="getProductByPrice" statementType="PREPARED" resultMap="ProductMap">
select * from product where price=#{price}
</select>
</mapper>