欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM案例-企业权限系统(5)- 添加产品

程序员文章站 2024-03-19 19:35:04
...

1 添加产品

SSM案例-企业权限系统(5)- 添加产品

  • 添加 日志配置文件
    SSM案例-企业权限系统(5)- 添加产品

1.1 DAO

package com.tzb.dao;

import com.tzb.domain.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IProductDao {

    @Select("select * from product")
    public List<Product> findAll();

    @Insert("insert into " +
            "product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus)" +
            " values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice}," +
            "#{productDesc},#{productStatus})")
    void save(Product product);
}

1.2 Service

package com.tzb.service;

import com.tzb.domain.Product;

import java.util.List;

public interface IProductService {
    public List<Product> findAll();

    void save(Product product);
}

package com.tzb.service.impl;

import com.tzb.dao.IProductDao;
import com.tzb.domain.Product;
import com.tzb.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("productService")
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductDao productDao;

    @Override
    public List<Product> findAll() {
        return productDao.findAll();
    }

    @Override
    public void save(Product product) {
        productDao.save(product);
    }
}

1.4 日期类型转换

spring mvc 参数转换之类型绑定有三种方式
SSM案例-企业权限系统(5)- 添加产品

1.5 Controller

package com.tzb.controller;

import com.tzb.domain.Product;
import com.tzb.service.IProductService;
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 java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<Product> ps = productService.findAll();
        mv.addObject("productList", ps);
        mv.setViewName("product-list");
        return mv;
    }

    /**
     * 产品添加
     *
     * @param product
     */
    @RequestMapping("/save.do")
    public String save(Product product) {
        System.out.println("Controller: " + product);
        productService.save(product);
        return "redirect:findAll.do";

    }
    
}

SSM案例-企业权限系统(5)- 添加产品
SSM案例-企业权限系统(5)- 添加产品
SSM案例-企业权限系统(5)- 添加产品