java中使用POI生成Excel并导出过程

2025-03-31 14:50

本文主要是介绍java中使用POI生成Excel并导出过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教...

注:本文章中代码均为本地Demo版本,若后续代码更新将不会更新文章

需求说明及实现方式

1.根据从数据库查询出的数据,将其写入excel表并导出

  • 我的想法是通过在实体属性上写自定义注解的方式去完成。因为我们在代码中可以通过反射的方式去获取实体类中全部的注解及属性名称等等。
  • 我们可以在自定义注解中声明一个参数value,这里面就存储其标题,这样我们

2.数据查询type不同,则显示的标题数量不同

  • 在注解类中增加type参数
  • 只有满足对应type的属性会被导出至excel中

3.数据查询type不同,则显示的标题不同(同一个字段)

  • 优化参数value,判断传入的value是否为json字符串,如果是json字符串则找到其与type对应的value

4.数据的格式化(时间类型格式化、数据格式化显示)

  • 数据格式化显示通过在注解类中增加dict参数,该参数传入json字符串。

本来我是想着通过easyExcel来完成这些功能,但是由于项目中已经引入了POI的3.9版本依赖,然后easyExcel中POI的依赖版本又高于该版本,而且不管是版本升级还是版本排除降级,总会有一个出现问题,最终也只能通过最基础的POI编写代码实现。

需求完成

依赖引入:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooXML</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>

java中使用POI生成Excel并导出过程

通用代码

1.ExcelExport

  • value:标题,也可为json字符串
  • dict:json字符串格式的字典,格式如User中所示
  • type:数组类型,查询数据type类型是什么值时这个字段会写入excel中。如我type = {"a"},则我在查询数据时传入的type为b则不会将这个字段写入excel中,如果传入的是a则会正常写入。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelExport {
    String value();
    String dict() default "";
    String[] type() default {};
}

2.User

  • @ExcelExport:即自定义的注解,其中值的含义在上面已经说清楚了
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class User {
    @ExcelExport(value = "用户名",type = {"a"})
    private String userName;
    @ExcelExport(value = "{a: '年龄',b: '年纪'}",type = {"a","b"})
    private Integer age;
    @ExcelExport(value = "性别",
            dict = "[{ value: \"0\", label: \"女\" }," +
                    "{ value: \"1\", label: \"男\" }]",
            type = {"a","b"})
    private Integer sex;
    @ExcelExport(value = "生日",type = {"b"})
    private Date birthday;
}

版本1

版本1中未实现数据查询type不同,则显示的标题不同(同一个字段)这一功能,如需要加请看PoiExcelUtil中writeTitleCellData方法。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import com.lzj.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;

import Java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author:雷子杰
 * @date:2023/7/4
 */
public class One {
    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<>();
        userList.add(new User("lzj1",1,1,new Date()));
        userList.add(new User("lzj2",2,0,new Date()));
        userList.add(new User("lzj3",3,1,new Date()));
        userList.add(new User("lzj4",4,0,new Date()));

        //声明XSSF对象
        XSSFWorkbook xssfSheets = new XSSFWorkbook();
        //创建sheet
        XSSFSheet userSheet = xssfSheets.createSheet("user");

        //创建标题字体
        XSSFFont titleFont = xssfSheets.createFont();
        titleFont.setBold(true);//加粗
        titleFont.setFontName("微软雅黑");
        titleFont.setFontHeightInPoints((short) 12);//字体大小
        //创建通用字体
        XSSFFont commonFont = xjsssfSheets.createFont();
        commonFont.setBold(false);//加粗
        commonFont.setFontName("微软雅黑");
        commonFont.setFontHeightInPoints((short) 12);//字体大小

        // 创建标题行单元格样式
        CellStyle titleCellStyle = xssfSheets.createCellStyle();
        titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框线
        titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平对齐方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直对齐方式
        titleCellStyle.setFont(titleFont);//字体样式
        titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//单元格前景色
        titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格

        //创建通用行单元格样式
        CellStyle commonCellStyle = xssfSheets.createCellStyle();
        commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        commonCellStyle.setFont(commonFont);
        commonCellStyle.setWrapText(true);//自动换行

        //获取实体类中全部属性
        Field[] fields = User.class.getDeclaredFields();
        //当前行
        int currentRow javascript= 0;
        //当前列
        int currentColumn = 0;
        //行高
        float rowHeight = 40.1f;
        //列宽
        int columnWidth = 33 * 256;
        //创建行
        XSSFRow row = userSheet.createRow(currentRow);
        当前行+1
        //currentRow++;

        //创建标题行
        // 遍历每个字段
        for (Field field : fields) {
            // 检查字段是否带有Explanation注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 获取Explanation注解实例
                ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                // 获取注解中的解释
                String value = explanation.value();

                //创建单元格,传入值,设置单元格样式
                XSSFCell cell = row.createCell(currentColumn);
                cell.setCellValue(value);
                cell.setCellStyle(titleCellStyle);

                //设置行高度
                row.setHeightInPoints(rowHeight);
                //设置列的宽度
                userSheet.setColumnWidth(currentColumn,columnWidth);
                //当前列+1
                currentColumn++;
            }
        }
        //重置当前列
        currentColumn = 0;

        //创建数据行
        for (User user : userList) {
            //每次循环时重置列
            currentColumn = 0;
            //当前行+1
            currentRow++;
            //创建行
            row = userSheet.createRow(currentRow);
            for (Field field : fields) {
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    try {
                        //解除private限制
                        field.setAccessible(true);

                        // 获取Explanation注解实例
                        ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                        // 获取属性的值
                        Object value = field.get(user);

                        //日期类型格式化
                        if (value != null && field.getType() == Date.class){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            value = sdf.format(value);
                        }
                        //获取对应字典
                        String dict = explanation.dict();
                        if (!StringUtils.isEmpty(dict) && value != null){
                            //JSONObject jsonObject = JSON.parseobject(dict);
                            List<String> list = JSON.parseArray(dict, String.class);
                            for (String item : list) {
                                JSONObject jsonObject = JSON.parseObject(item);
                                if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                    value = jsonObject.getString("label");
                                    break;
                                }
                            }
                            //value = jsonObject.get(value.toString());
                        }
                        //创建单元格,传入值,设置单元格样式
                        XSSFCell cell = row.createCell(currentColumn);
                        cell.setCellValue(value == null?"":value.toString());
                        cell.setCellStyle(commonCellStyle);

                        //设置行高度
                        row.setHeightInPoints(rowHeight);
                        //当前列+1
                        currentColumn++;

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        // 将生成的excel文件输出流转为字节数组
        byte[] bytes = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        xssfSheets.write(outputStream);
        outputStream.close();
        bytes = outputStream.toByteArray();

        //读取字节数组为文件输入流
        InputStream inputStream = new ByteArrayInputStream(bytes);
        inputStream.close();


        //在声明一个输出流将文件下载到本地
        File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        //将bytes中的内容写入
        bufferedOutputStream.write(bytes);
        //刷新输出流,否则不会写出数据
        bufferedOutputStream.flush();
        bufferedOutputStream.close();


    }
}

版本2

版本二相比与版本1,其主要优势是将POI相关操作都封装进了PoiExcelUtil中。

  • PoiExcelUtil
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @author:雷子杰
 * @date:2023/7/6
 */
public class PoiExcelUtil {

    /**
     * 获取标题字体
     * @param xssfWorkbook
     * @return
     */
    public static XSSFFont getTitleFont(XSSFWorkbook xssfWorkbook){
        //创建标题字体
        XSSFFont titleFont = xssfWorkbook.createFont();
        titleFont.setBold(true);//加粗
        titleFont.setFontName("微软雅黑");
        titleFont.setFontHeightInPoints((short) 12);//字体大小

        return titleFont;
    }

    /**
     * 获取通用字体
     * @param xssfWorkbook
     * @return
     */
    public static XSSFFont getCommonFont(XSSFWorkbook xssfWorkbook){
        //创建通用字体
        XSSFFont commonFont = xssfWorkbook.createFont();
        commonFont.setBold(false);//加粗
        commonFont.setFontName("微软雅黑");
        commonFont.setFontHeightInPoints((short) 12);//字体大小

        return commonFont;
    }

    /**
     * 获取标题单元格样式
     * @param xssfWorkbook
     * @param xssfFont
     * @return
     */
    public static CellStyle getTitleCellStyle(XSSFWorkbook xssfWorkbook , XSSFFont xssfFont){
        // 创建标题行单元格样式
        CellStyle titleCellStyle = xssfWorkbook.createCellStyle();
        titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框线
        titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平对齐方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直对齐方式
        titleCellStyle.setFont(xssfFont);//字体样式
        titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//单元格前景色
        titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格

        return titleCellStyle;
    }

    /**
     * 获取通用单元格样式
     * @param xssfWorkbook
     * @param xssfFont
     * @return
     */
    public static CellStyle getCommonCellStyle(XSSFWorkbook xssfWorkbook, XSSFFont xssfFont){
        //创建通用行单元格样式
        CellStyle commonCellStyle = xssfWorkbook.createCellStyle();
        commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        commonCellStyle.setFont(xssfFont);
        commonCellStyle.setWrapText(true);//自动换行

        return commonCellStyle;
    }

    /**
     * 写入单个单元格数据
     * @param row 行对象
     * @param xssfSheet sheet对象
     * @param value 单元格的值
     * @param cellStyle 单元格样式
     * @param rowHeight 行高
     * @param columnWidth 列宽
     */
    public static void writeCellData(XSSFRow row, XSSFSheet xssfSheet , Object value ,CellStyle cellStyle,Integer currentColumn,Float rowHeight,Integer columnWidth){

        //创建单元格,传入值,设置单元格样式
        XSSFCell cell = row.createCell(currentColumn);
        cell.setCellValue(value == null ? "" : value.toString());
        cell.setCellStyle(cellStyle);
        //设置行高度
        row.setHeightInPoints(rowHeight);
        //设置列的宽度
        xssfSheet.setColumnWidth(currentColumn,columnWidth);
    }

    /**
     *
     * @param row 行对象
     * @param xssfSheet sheet对象
     * @param cellStyle 单元格样式
     * @param fields 反射获取得到的实体对象的全部属性
     * @param currentColumn 当前列
     * @param rowHeight 行高
     * @param columnWidth 列宽
     * @param type 类型
     */
    public static void writeTitleCellData(XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Integer currentColumn,Float rowHeight,Integer columnWidth,String type){
        //创建标题行
        // 遍历每个字段
        for (Field field : fields) {
            // 检查字段是否带有ExcelExport注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 获取Explanation注解实例
                ExcelExport explanation = field.getAnnotation(ExcelExport.class);

                //判断是否是需要写入的数据类型
                String[] typeArray = explanation.type();
                Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                if (!set.contains(type)){
                 continue;
                }
                // 获取注解中的解释
                String value = explanation.value();
                //判断value是否是json格式数据
                boolean isJson = true;
                try{
                    Object parse = JSON.parse(value);
                }catch (Exception e){
                    isJson = false;
                }
                if (isJson == true){//如果是json格式数据,则给他对应对应类型的值
                    JSONObject jsonObject = JSON.parseObject(value);
                    value = jsonObject.getString(type);
                }

                //写入单元格数据
                PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                //当前列+1
                currentColumn++;
            }
        }
    }

    /**
     * 将集合数据全部写入单元格
     * @param list 需要写入excel的集合数据
     * @param currentRow 当前行
     * @param currentColumn 当前列
     * @param row 行对象
     * @param xssfSheet sheet对象
     * @param cellStyle 单元格样式
     * @param fields 反射获取得到的实体对象的全部属性
     * @param rowHeight 行高
     * @param columnWidth 列宽
     * @param type 类型
     * @param <T>
     */
    public static <T> void writeCommonRowCellData(List<T> list,Integer currentRow ,Integer currentColumn, XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Float rowHeight,Integer columnWidth,String type){
        //创建数据行
        for (T obj : list) {
  China编程          //每次循环时重置列
            currentColumn = 0;
            //当前行+1
            currentRow++;
            //创建行
            row = xssfSheet.createRow(currentRow);
            for (Field field : fields) {
                // 检查字段是否带有ExcelExport注解
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    try {
                        //解除private限制
                        field.setAccessible(true);
                        // 获取Explanation注解实例
                        ExcelExport explanation = field.getAnnotation(ExcelExport.class);

                        //判断是否是需要写入的数据类型
                        String[] typeArray = explanation.type();
                        Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                        if (!set.contains(type)){
                            continue;
                        }

                        // 获取属性的值
               http://www.chinasem.cn         Object value = field.get(obj);
                        //日期类型格式化
                        if (value != null && field.getType() == Date.class){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            value = sdf.format(value);
                        }
                        //获取对应字典
                        String dict = explanation.dict();
                        if (!StringUtils.isEmpty(dict) && value != null){
                            List<String> parseArray = JSON.parseArray(dict, String.class);
                            for (String item : parseArray) {
                                JSONObject jsonObject = JSON.parseObject(item);
                                if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                    value = jsonObject.gandroidetString("label");
                                    break;
                                }
                            }
                        }
                        //写入单元格数据
                        PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                        //当前列+1
                        currentColumn++;

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
  • Two
import com.lzj.entity.User;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author:雷子杰
 * @date:2023/7/4
 */
public class Two {
    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<>();
        userList.add(new User("lzj1",1,1,new Date()));
        userList.add(new User("lzj2",2,0,new Date()));
        userList.add(new User("lzj3",3,1,new Date()));
        userList.add(new User("lzj4",4,0,new Date()));

        //声明XSSF对象
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //创建sheet
        XSSFSheet userSheet = xssfWorkbook.createSheet("user");

        //创建标题字体
        XSSFFont titleFont = PoiExcelUtil.getTitleFont(xssfWorkbook);
        //创建通用字体
        XSSFFont commonFont = PoiExcelUtil.getCommonFont(xssfWorkbook);
        // 创建标题行单元格样式
        CellStyle titleCellStyle = PoiExcelUtil.getTitleCellStyle(xssfWorkbook,titleFont);
        //创建通用行单元格样式
        CellStyle commonCellStyle = PoiExcelUtil.getCommonCellStyle(xssfWorkbook,commonFont);
        //获取实体类中全部属性
        Field[] fields = User.class.getDeclaredFields();
        //当前行
        int currentRow = 0;
        //当前列
        int currentColumn = 0;
        //行高
        float rowHeight = 40.1f;
        //列宽
        int columnWidth = 33 * 256;
        //创建行
        XSSFRow row = userSheet.createRow(currentRow);
        //创建标题行
        PoiExcelUtil.writeTitleCellData(row,userSheet,titleCellStyle,fields,currentColumn,rowHeight,columnWidth,"b");
        //重置当前列
        currentColumn = 0;
        //创建数据行
        PoiExcelUtil.writeCommonRowCellData(userList,currentRow,currentColumn,row,userSheet,commonCellStyle,fields,rowHeight,columnWidth,"b");


        // 将生成的excel文件输出流转为字节数组
        byte[] bytes = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        xssfWorkbook.write(outputStream);
        outputStream.close();
        bytes = outputStream.toByteArray();

        //读取字节数组为文件输入流
        InputStream inputStream = new ByteArrayInputStream(bytes);
        inputStream.close();


        //在声明一个输出流将文件下载到本地
        File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        //将bytes中的内容写入
        bufferedOutputStream.write(bytes);
        //刷新输出流,否则不会写出数据
        bufferedOutputStream.flush();
        bufferedOutputStream.close();

    }
}

结果展示

这是我初始化时的数据

java中使用POI生成Excel并导出过程

下面的是我type参数不同时的数据,均是以版本2来进行的写入导出。

type参数修改位置如下:

java中使用POI生成Excel并导出过程

type参数为a

java中使用POI生成Excel并导出过程

type参数为b

java中使用POI生成Excel并导出过程

总结

在项目开发过程中总会遇到各式各样的问题,只有不断的学习,不断的积累,自身水平才能提高。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程China编程(www.chinasem.cn)。

这篇关于java中使用POI生成Excel并导出过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154031

相关文章

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建