目录
使用到的技术技术
- SpringBoot
- Mybatis-plus(也可用Mybatis,或者麻点用JDBC)
- Thymeleaf(也可用xml或直接高端点用vue)
需要用到的软件
- idea
- Mysql5.7
各位看官,废话不多说直接开始项目
项目结构
mysql
在数据库wshop中创建一个表w_productmodel
CREATE TABLE `w_productmodel` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`image` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
Application.yaml配置文件
server:
port: 9100
spring:
mvc:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
datasource:
username: 自己的sql用户名
password: 自己的sql密码
url: jdbc:mysql://localhost:3306/wshop?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
Maven
maven由于一些是其他类使用的就没有删,多多担待。
<?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.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ImageTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ImageTest</name>
<description>ImageTest</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.7</version>
</dependency>
<!-- 加密-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<!-- 添加提交-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--引入jquery-webjar-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Mybatis-plus
个人偷懒,大家可以用其他的,大致sql语句都差不多
package com.wu.imagetest.Controller;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
/**
* @ClassName CodeGenerator
* @Author AndyWu
* @Date 2023-11-14 10:00
* @Motto 学不死就往死里学
* @Version 1.0
*/
public class CodeGenerator {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/wshop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
String username = "root";
String password = "123456";
String baomidou = "Wuandy";
String moduleName = "sys";
String mapperLocation = "E:\\PublicProject\\SiteMoudle\\ImageTest\\src\\main\\resources\\mapper\\"+moduleName;
String tables = "w_productmodel";
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author(baomidou) // 设置作者
// .enableSwagger() // 开启 swagger 模式
// .fileOverride() // 覆盖已生成文件
.outputDir("E:\\PublicProject\\SiteMoudle\\ImageTest\\src\\main\\java"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.wu.imagetest") // 设置父包名
.moduleName(moduleName) // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation)); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude(tables) // 设置需要生成的表名
.addTablePrefix("w_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wu.imagetest.sys.mapper.ProductmodelMapper">
<insert id="insertImage" parameterType="com.wu.imagetest.sys.entity.Productmodel">
insert into w_productmodel(image) values(#{image})
</insert>
</mapper>
Mapper接口
package com.wu.imagetest.sys.mapper;
import com.wu.imagetest.sys.entity.Productmodel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author Wuandy
* @since 2023-11-21
*/
public interface ProductmodelMapper extends BaseMapper<Productmodel> {
int insertImage(String image);
}
Service层
Sercie接口
package com.wu.imagetest.sys.mapper;
import com.wu.imagetest.sys.entity.Productmodel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author Wuandy
* @since 2023-11-21
*/
public interface ProductmodelMapper extends BaseMapper<Productmodel> {
int insertImage(String image);
}
ServiceImpl类
package com.wu.imagetest.sys.service.impl;
import com.wu.imagetest.sys.entity.Productmodel;
import com.wu.imagetest.sys.mapper.ProductmodelMapper;
import com.wu.imagetest.sys.service.IProductmodelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* <p>
* 服务实现类
* </p>
*
* @author Wuandy
* @since 2023-11-21
*/
@Service
public class ProductmodelServiceImpl extends ServiceImpl<ProductmodelMapper, Productmodel> implements IProductmodelService {
//注入Mapper接口对象
@Resource
private ProductmodelMapper productmodelMapper;
@Override
public int insertImage(String image) {
//image不为空判断
return productmodelMapper.insertImage(image);
}
}
Controller层
都有用注释,大家不用担心。
package com.wu.imagetest.sys.controller;
import com.wu.imagetest.sys.entity.Productmodel;
import com.wu.imagetest.sys.service.IProductmodelService;
import com.wu.imagetest.sys.service.impl.ProductmodelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author Wuandy
* @since 2023-11-21
*/
@Controller
//@RequestMapping("/sys/productmodel")
public class ProductmodelController {
@Autowired
private IProductmodelService productmodelService;
@Autowired
private ProductmodelServiceImpl productmodelServiceImpl;
@RequestMapping("/index")
public String index(){
return "upload";
}
@ResponseBody
@RequestMapping("/upload")
public String upload(MultipartFile file) {
System.out.println("传输文件开始");
if (file.isEmpty()) {
return "图片上传失败1";
}
//file校验重命名(a:1.png b:1.png)
String originalFilename = file.getOriginalFilename();//原来的图片名
String ext = "." + originalFilename.split("\\.")[1];//1.png
String uuid = UUID.randomUUID().toString().replace("-", "");
//地址连接
String fileName = uuid + ext;
productmodelServiceImpl.insertImage(fileName);
ApplicationHome applicationHome = new ApplicationHome(this.getClass());
String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
"\\src\\main\\resources\\static\\images\\";
// String pre = "E:\\PublicProject\\SiteMoudle\\data\\images\\";
String path = pre + fileName;
try {
file.transferTo(new File(path));
System.out.println("传输文件结束");
return path;
} catch (IOException e) {
e.printStackTrace();
}
return "图片上传失败2";
}
// @ResponseBody
// @RequestMapping("/list")
// private String list(Model model){
// List<Productmodel> list = productmodelService.list();
//
ApplicationHome applicationHome = new ApplicationHome(this.getClass());
// String pre = "\\src\\main\\resources\\static\\images\\";
// for (Productmodel productmodel : list) {
// String image = productmodel.getImage();
// String path = pre + image;
// productmodel.setImage(path);
// }
// model.addAttribute("list",list);
// System.out.println(list);
// return "select";
// }
@RequestMapping("/list")
private String list(Model model){
List<Productmodel> list = productmodelService.list();
ApplicationHome applicationHome = new ApplicationHome(this.getClass());
String pre = "/images/";
for (Productmodel productmodel : list) {
String image = productmodel.getImage();
String path = pre + image;
productmodel.setImage(path);
}
model.addAttribute("list",list);
System.out.println(list);
return "select";
}
}
原神,启动!错了错了...项目 , 启动!!启动!!!
进入文件上传页面
点击“选择上传”选择文件,然后点击“上传”;
上传成功后返回下面页面(返回文件保存在本地的位置)
然后我们多存几个图片
本地文件保存
数据库保存文件名或者地址(因为使用了字符串拼接,我这里直接保存文件的文件名)
你以为完了吗?不!不!不!
查询所有图片数据
具体思路:从数据库中读取图片名,然后拼接字符串,然后返回图片路径,实现图片展示;
结束语:
以上就是本章的内容,很高兴大家看完。后期项目回上传到gitee和github。
最近感觉真的Java寒冬,工作也不好找,愿各位小伙伴都能找到自己心仪的工作(或者跳槽到自己喜欢的岗位和公司)。