通过页面去操作数据库并且在数据库里面去添加数据

来源:互联网 发布:淘宝肖像权侵权 编辑:程序博客网 时间:2024/06/02 00:28

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>


0 0
原创粉丝点击