java mvc增删查改_SpringMVC+Mybatis架构下的增删查改

第一步新建一个Controller,以产品信息为例(ProductController)package com.xcy.ctrl;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

@Controller

@RequestMapping("/Product")

public class ProductController {

@RequestMapping("/index")

public ModelAndView index(HttpServletRequest req, HttpServletResponse res){

ModelAndView mv = new ModelAndView();//mv初始化

mv.setViewName("true");//设定访问页面为 true.jsp

return mv;

}

}

第二步建Service, 它只是一个接口,真正是通过ServiceImpl实现

**在public class productInfoServiceImpl implements ProductInfoService之前要有@Service进行注解package com.xcy.service;

import java.util.List;

import com.xcy.bean.ProductInfo;

public interface ProductInfoService {

public List getAllProduct();//获得所有产品信息列表

}package com.xcy.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.xcy.bean.ProductInfo;

import com.xcy.mapper.ProductInfoMapper;

import com.xcy.service.ProductInfoService;

@Service

public class ProductInfoServiceImpl implements ProductInfoService{

//@Autowired

//public ProductInfoMapper chanpin;

@Override

public List getAllProduct() {

// TODO Auto-generated method stub

//return chanpin.queryAll();

System.out.println("getAllProduct");

return null;

}

}

第三步建一个Mapper,它也是一个接口,在 .xml文件中编辑相应的sql语句package com.xcy.mapper;

import java.util.List;

import com.xcy.bean.ProductInfo;

public interface ProductInfoMapper {

public List queryAll();

}<?xml  version="1.0" encoding="UTF-8"?>

mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from Product

之后要将Product.xml文件引入到mybatisConfig.xml文件中                                                          <?xml  version="1.0" encoding="UTF-8"?>

configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

第四步 查询产品详细信息

for(int i = 0; i< list.size() ;i++){

ProductInfo c = list.get(i);

System.out.println("Id:"+ c.getId() +

"  Name:" + c.getName() + " Spec:"+ c.getSpec()

+ " Price:" + c.getPrice());

}//输出每个产品的编号、名称、规格、价格package com.xcy.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.xcy.bean.ProductInfo;

import com.xcy.service.ProductInfoService;

@Controller

@RequestMapping("/Product")

public class ProductController {

@Autowired

public ProductInfoService cp;

@RequestMapping("/index")

public ModelAndView index(HttpServletRequest req, HttpServletResponse res){

ModelAndView mv = new ModelAndView();//mv初始化

mv.setViewName("true");//设定访问页面为 true.jsp

List list = cp.getAllProduct();//获得所有产品信息列表

for(int i = 0; i

ProductInfo c = list.get(i);

System.out.println("Id:"+ c.getId() +

"  Name:" + c.getName() + " Spec:"+ c.getSpec()

+ " Price:" + c.getPrice());

}//输出每个产品的编号、名称、规格、价格

return mv;

}

}

第五步新增产品信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值