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.ResponseBody;
import com.project.service.IProductService;
@Controller这是注解的意思访问业务层
public class ProductController {
@Autowired
private IProductService productService;
private int price;
@RequestMapping(value="/addProduct")
@ResponseBody
public String addProduct(){
productService.addProduct();
return null;
这里是写一个addProduct()的方法
package com.project.dal.mapper;import com.project.dal.entity.Product;
public interface ProductMapper {
public void addProduct(Product product);这个是去掉方法体的方式(product product)
是参数的类型和参数值
处理addproduct();方法的数据
package com.project.service;
public interface IProductService {
void addProduct();
给方法体内设置参数
<?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>
插入数据
</mapper>
给属性赋值操作
package com.project.service.impl;
import javax.xml.ws.ServiceMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.dal.entity.Product;
import com.project.dal.mapper.ProductMapper;
import com.project.service.IProductService;
@Service
public class ProductService implements IProductService{
@Autowired
private ProductMapper productMapper;
@Override
public void addProduct() {
// TODO Auto-generated method stub
Product product = new Product();
product.setAddTime(0);
product.setBrief("brief");
product.setPid(10);
product.setPrice(13);
product.setStatus(0);
product.setTitle("duo");
productMapper.addProduct(product);