SSM案例-企业权限系统(5)- 添加产品
程序员文章站
2024-03-19 19:35:04
...
1 添加产品
- 添加 日志配置文件
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 参数转换之类型绑定有三种方式
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";
}
}
上一篇: 循环链表的插入删除实现
下一篇: LeetCode1221 分隔平衡字符串