SpringMVC笔记3--annotation

SpringMVC使用annotation可以减少配置文件的书写。要启动annotation的功能,首先需要在SpringMVC的配置文件中配置下面的标签。

<mvc:annotation-driven />
<context:component-scan base-package="com.my.webstore" />

annotation主要有以下这些:
@Controller 修饰一个Controller类,是表现层组件
@RequestMapping 绑定一个url到controller类或者controller中的函数。
@Repository 表示组件是一个资源类,这个资源类主要是一些CURD操作的函数,直接和数据库交互,为持久层组件
@Service 表明是一个Service组件,其中会依赖到一些Repository 组件,调用Repository的多个CURD操作。表示业务的逻辑,为业务层组件

范例:

@Controller,@RequestMapping

package com.my.webstore.controller;

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

import com.my.webstore.domain.repository.ProductRepository;

@Controller
public class HomeController {
    
    @Autowired
    private ProductRepository repository;
    
    @RequestMapping("/products")
    public String welcome(Model model) {
        model.addAttribute("greeting", "Welcome to  Web Store!");
        model.addAttribute("tagline", "The  one and only    amazing webstore");
        
        model.addAttribute("products", repository.getAllProducts());
        
        return "index";
    }
}

@Repository

package com.my.webstore.domain.repository.impl;

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

import org.springframework.stereotype.Repository;

import com.my.webstore.domain.Product;
import com.my.webstore.domain.repository.ProductRepository;

@Repository
public class ProductRepositoryImpl implements ProductRepository {
    private List<Product> list = new ArrayList<Product>();


    public ProductRepositoryImpl() {
        Product iphone = new Product("P1234", "iPhone   5s", new BigDecimal(500));
        iphone.setDescription("Apple    iPhone  5s  smartphone  with    4.00-inch   640x1136    display and 8-megapixel rear    camera");
        iphone.setCategory("Smart   Phone");
        iphone.setManufacturer("Apple");
        iphone.setUnitsInStock(1000);

        Product laptop_dell = new Product("P1235", "Dell    Inspiron",
                new BigDecimal(700));
        laptop_dell
                .setDescription("Dell   Inspiron    14-inch Laptop  (Black) with    3rd Generation  Intel   Core    processors");
        laptop_dell.setCategory("Laptop");
        laptop_dell.setManufacturer("Dell");
        laptop_dell.setUnitsInStock(1000);

        Product tablet_Nexus = new Product("P1236", "Nexus  7", new BigDecimal(
                300));
        tablet_Nexus
                .setDescription("Google Nexus   7   is  the lightest    7   inch    tablet  With    a   quad-core   Qualcomm    Snapdragon™ S4  Pro processor");
        tablet_Nexus.setCategory("Tablet");
        tablet_Nexus.setManufacturer("Google");
        tablet_Nexus.setUnitsInStock(1000);

        list.add(iphone);
        list.add(laptop_dell);
        list.add(tablet_Nexus);

    }
    
    public List<Product> getAllProducts() {
        // TODO Auto-generated method stub
        return list;
    }

    public Product getProductById(String productId) {
        // TODO Auto-generated method stub
        if (productId == null) {
            throw new IllegalArgumentException("productId can't be null");
        }
        Product tmpProduct = null;
        for (Product product : list) {
            if (product.getProductId().equals(productId)) {
                tmpProduct =  product;
                break;
            }
        }
        
        if (tmpProduct == null) {
            throw new IllegalArgumentException("product can not be found:"+productId);
        }
        
        return tmpProduct;
    }
}

@Service

package com.my.webstore.service;

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

import com.my.webstore.domain.Product;
import com.my.webstore.domain.repository.ProductRepository;

@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private ProductRepository repository;

    public void processOrder(String productId, int count) {
        // ODO Auto-generated method stub
        Product product = repository.getProductById(productId);
        if (product.getUnitsInStock() < count) {
            throw new IllegalArgumentException("");
            
        }
        product.setUnitsInStock(product.getUnitsInStock()- count);
        
    }

}

转载于:https://www.cnblogs.com/xiaozu/p/4639006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值