spring boot 实现相册or商城系统 大作业笔记

本文介绍如何在Spring Boot项目中实现图片上传功能,包括表单提交、后台映射、参数传递及图片显示。同时,解决MyBatis模糊查询中出现的异常问题,并展示配置数据源、Thymeleaf缓存、文件大小限制的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目要点以及问题

Form表单提交写法

在这里插入图片描述

写对应后台的mapping

携带参数传参到后台

<a th:href="@{/lookPhoto/}+${photo.id}">

@RequestMapping("/lookPhoto/{id}")

public String lookPhoto(@PathVariable("id") Integer pid, Model model,HttpSession
session)

 

Impl中注释 不然出现nullpoint

在这里插入图片描述

前台请求与视图配置

在这里插入图片描述
在这里插入图片描述

图片无法正常显示

只能写相对路径

<img th:src="@{${photo.address}}"
alt="img">

图片保存到项目

public String upload(@RequestParam(value = “file”) MultipartFile file,String pname, String message,String category,int ispublic,HttpServletRequest request, HttpSession session){


       System.out.println("path"+request.getContextPath());

        //写入硬盘
        System.out.println(file.toString());

        String savePath="src/main/resources/static/imgs";

        File fileDir=new File(savePath);

        System.out.println(fileDir.getAbsolutePath());

        if(!fileDir.exists()){
            // 递归生成文件夹
            fileDir.mkdirs();
        }

//        FileOutputStream fos=null;
//        FileInputStream fis=null;
//        byte[] buffer=new byte[1024];
        String address="/imgs"+File.separator+file.getOriginalFilename();
        File newFile = new File(fileDir.getAbsolutePath()+File.separator+file.getOriginalFilename());

        try {
            file.transferTo(newFile);
        }catch (Exception e){
            e.printStackTrace();
        }
//        int len=0;
//        try{fos=new FileOutputStream(fileDir.getAbsolutePath()+file.separator+file.toString());
//            System.out.println(savePath+file.separator+file.toString());
//            fis = new FileInputStream(file);
//            while ((len = fis.read(buffer)) > 0) {
//                fos.write(buffer, 0, len);
//            }
//        }
//        catch(Exception e){
//
//        }
        //保存地址到数据库
        photo=new Photo();

        photo.setAddress(address);

        if (session.getAttribute("uid")!=null)
            photo.setUid((int)session.getAttribute("uid"));

        System.out.println((int)session.getAttribute("uid"));

//        user=(User) session.getAttribute("user");
//
//        photo.setAuthor(user.getUname());

        photo.setPname(pname);

        System.out.println(message);

        if(message!=null) {
            photo.setMessage(message);
        }else{
            photo.setMessage("这个人很懒,什么也没有留下~");
        }

        photo.setIspublic(ispublic);

        photo.setCategory(category);

        photo.setIspass("no");

        photoService.add(photo);

        System.out.println(photo.getIspass());

        return "redirect:/getphoto";
    }

 

## Mybatis模糊查询

SELECT  *

FROM photo

WHERE  pname like '%${_parameter}%'

出现异常There is no getter for property named ‘pname’ in ‘class java.lang.String’

把#{xxx}修改为 #{_parameter} 即可

也可以在方法中提前定义:

public int methodName(@Param(value=“state”) String state ){

搜索框等设置框内默认值

<input type="text" class="input"
name="pname" value=""
placeholder="在此输入关键字搜索详细图片">

项目配置文件

Properties

##数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver



spring.datasource.username=root



spring.datasource.password=123456



spring.datasource.url=jdbc:mysql://localhost:3306/test

##mybatis配置



mybatis.mapperLocations=classpath:mybatis/mapper/*.xml

mybatis.config-location=classpath:mybatis/mybatis-config.xml

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource



spring.thymeleaf.cache=false



#设置单个文件大小

spring.servlet.multipart.max-file-size= 50MB

 #设置单次请求文件的总大小

spring.servlet.multipart.max-request-size= 50MB

Yaml文件

server:

  port:
8081

spring:
thymeleaf:
 cache:
false

pom文件

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




<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.3.3.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.example</groupId>

    <artifactId>demo</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>

    <description>Demo project for Spring Boot</description>



    <properties>

        <java.version>1.8</java.version>

    </properties>



    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>



        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

            <exclusions>

                <exclusion>

                    <groupId>org.junit.vintage</groupId>

                    <artifactId>junit-vintage-engine</artifactId>

                </exclusion>

            </exclusions>

        </dependency>





        <dependency>



            <groupId>org.springframework.boot</groupId>



            <artifactId>spring-boot-starter-thymeleaf</artifactId>



        </dependency>







        <!--数据连接配置-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.35</version>

        </dependency>

        <!--数据池-->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>1.1.3</version>

        </dependency>



        <!--       
<dependency>

                   
<groupId>org.springframework.boot</groupId>

                   
<artifactId>mybatis-spring-starter</artifactId>

                    <version></version>

                </dependency>-->



        <!--mybatis依赖-->

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>1.1.1</version>

        </dependency>



        <dependency>

            <groupId>commons-io</groupId>

            <artifactId>commons-io</artifactId>

            <version>2.5</version>

        </dependency>







        <dependency>

            <groupId>commons-fileupload</groupId>

            <artifactId>commons-fileupload</artifactId>

            <version>1.2.2</version>

        </dependency>





    </dependencies>





    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

Multipartfile无效问题

导入io包 uploadfile包依赖后等一会即可

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值