SSM目录结构

写在前面的话

在确定需求时书写书写实体类,然后项目一般都是从设计数据库开始所以也就是从dao层开始书写,接口的编写方法的确定(Name+Mapper.java接口文件),然后编写sql语句于Name+Mapper.xml测试,测试成功后就开始根据接口的方法来实现service业务逻辑层的书写,在这一层所关注的参数值和返回值,而方法体内就是要对数据的处理成我们想要的结果,返回到controller层最后和前端交互。一直到前端页面。

在这里插入图片描述

看一个实例目录结构

在这里插入图片描述

前端页面

1、保存图片

<%--
  Created by IntelliJ IDEA.
  User: hello
  Date: 2021/10/15
  Time: 11:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>图片上传ing</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/save.do" method="post" enctype="multipart/form-data">
    图片:<input type="file" name="file"><br>
    <input type="submit" value="提交">
</form>
</body>

</html>

2、展示图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <title>list</title>
    <style type="text/css">
        #images{
            width: 250px;
            height: 250px;
        }
    </style>
</head>
<body>

<table class="table table-bordered table-hover">
    <tr>
        <th>序号</th>
        <th>图片</th>
    </tr>
    <c:forEach items="${lists}" var="product">
        <tr>
            <td>${product.pid}</td>
            <td>
                <c:if test="${product.pimage != null}">
                    <!--docBase 为文件保存路径, path 为文件访问路径。-->
                    <!-- <Context docBase="D:/upload" path="/image" />-->
                    <img alt="" src="/image/${product.pimage}" id="images">
                </c:if>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

controller层


//图片上传
@Controller
public class ProductController {
    
    
    //controller调service层
        @Autowired
        private ProductService productService;

        @RequestMapping("/listImages")
        public ModelAndView list(Model model) {
    
    
            List<Product> lists = productService.list();
            ModelAndView mav = new ModelAndView();
            mav.addObject("lists", lists);
            mav.setViewName("listImages");
            System.out.println(lists);
            return mav;
        }

        /**
         * 保存图片
         * @param file
         * @param product
         * @param map
         * @return
         */
        @RequestMapping("/save")
        public String save(MultipartFile file, Product product, ModelMap map) {
    
    
            try {
    
    
                return productService.save(file, product, map);
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            return null;
        }
    }

service层

service层中的接口

@Service
public interface ProductService {
    
    
    /**
     * 查询所有的图片
     * @return
     */

    List<Product> list();
    /**
     * 上传一张图片
     * @param product
     * @return
     */
    String save(MultipartFile file, Product product, ModelMap map) throws IOException;
}

service层中的实现类

@Service
public class ProductServiceIml implements ProductService {
    
    
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> list() {
    
    
        return productMapper.list();
    }
    @Override
    @Transactional
    public String save(MultipartFile file, Product product, ModelMap map) throws IOException {
    
    
        // 保存图片的路径,图片上传成功后,将路径保存到数据库
        String filePath = "D:\\upload";
        // 获取原始图片的扩展名
        String originalFilename = file.getOriginalFilename();
        // 生成文件新的名字
        String newFileName = UUID.randomUUID() + originalFilename;
        // 封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);
        // 保存到数据库
        product.setPimage(newFileName);
        productMapper.save(product);
        return "redirect:listImages.do";
    }
}

dao层

dao层接口


//存照片
public interface ProductMapper {
    
    
        /**
         * 查询所有的图片
         * @return
         */

        List<Product> list();
        /**
         * 上传一张图片
         * @param product
         * @return
         */
        Integer save(Product product);
    }

dao层实现SQl语句

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.ProductMapper">

    <!-- 查询所有图片 -->
    <select id="list" resultType="product">
        select pid, pimage
        from product
    </select>
    <!--添加-->
    <insert id="save" parameterType="product">
        insert into product(pimage)
        values (#{pimage})
    </insert>

</mapper>


欢迎提问!

猜你喜欢

转载自blog.csdn.net/weixin_45662838/article/details/120811231
今日推荐