基础备忘录

一 dto/vo/do/pojo的使用

参考: https://www.cnblogs.com/qixuejia/p/4390086.html

1、pojo

POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。
使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter setter方法的类,没有业务逻辑,有时可以作为VO(value -object)或dto(Data Transform Object)来使用.当然,如果你有一个简单的运算属性也是可以的,但不允许有业务方法,也不能携带有connection之类的方法。

2、vo/dto

二 swagger注解的使用

1、注解分类

@ApiModel(“sss”)
类上面
@ApiModelProperty(value = “xxx”)
属性上面

三 springboot profile多环境配置

方式一:一个配置文件

spring:
  profiles:
    active: test	#激活(选择)环境test
---
spring:
  profiles: dev		#指定环境名字dev
server:
  port: 9999
---
spring:
  profiles: test	#指定环境名字test
server:
  port: 8888

方式二:多个配置文件

通过配置文件的名字来识别环境

application-dev.yml

server:
  port: 9999

application-test.yml

server:
  port: 8888

实际使用:
application.yml

spring:
  profiles:
    active: test 
    #根据文件名字配置 application-dev.properties

四、springboot集成 poi操作excel

1、导包

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.15</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml-schemas</artifactId>
	<version>3.15</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.15</version>
</dependency>

2、创建excel文件、添加样式

public static void main(String[] args) {
        //创建工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        //创建一个sheet
        XSSFSheet sheet = wb.createSheet();

        // 创建单元格样式
        XSSFCellStyle style =  wb.createCellStyle();
        style.setFillForegroundColor((short)42); //设置要添加表格北京颜色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //solid 填充
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER); //文字水平居中
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//文字垂直居中
        style.setBorderBottom(BorderStyle.THIN); //底边框加黑
        style.setBorderLeft(BorderStyle.THIN);  //左边框加黑
        style.setBorderRight(BorderStyle.THIN); // 有边框加黑
        style.setBorderTop(BorderStyle.THIN); //上边框加黑
        //为单元格添加背景样式
        for (int i = 0; i < 6; i++) { //需要6行表格
            Row row =	sheet.createRow(i); //创建行
            for (int j = 0; j < 6; j++) {//需要6列
                row.createCell(j).setCellStyle(style);
            }
        }


        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));//合并单元格,cellRangAddress四个参数,第一个起始行,第二终止行,第三个起始列,第四个终止列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));

        //tian入数据
        XSSFRow row = sheet.getRow(0); //获取第一行
        row.getCell(1).setCellValue("期末考试成绩"); //在第一行中创建一个单元格并赋值
        XSSFRow row1 = sheet.getRow(1); //获取第二行,为每一列添加字段
        row1.getCell(1).setCellValue("体育");
        row1.getCell(2).setCellValue("音乐");
        row1.getCell(3).setCellValue("计算机");
        row1.getCell(4).setCellValue("历史");
        row1.getCell(5).setCellValue("地理");
        XSSFRow row2 = sheet.getRow(2); //获取第三行
        row2.getCell(0).setCellValue("大毛");
        XSSFRow row3 = sheet.getRow(3); //获取第四行
        row3.getCell(0).setCellValue("二毛");
        XSSFRow row4 = sheet.getRow(4); //获取第五行
        row4.getCell(0).setCellValue("三毛");
        XSSFRow row5 = sheet.getRow(5); //获取第五行
        row5.getCell(0).setCellValue("狗蛋");

        //将数据写入文件
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("test.xlsx");
            wb.write(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3、读取excel

public static void readExcel(String excelName) throws IOException {
    	//将文件读入
    	InputStream in  = new FileInputStream(new File(excelName));
   	    //创建工作簿
    	XSSFWorkbook wb = new XSSFWorkbook(in); 
    	//读取第一个sheet
    	Sheet sheet = wb.getSheetAt(0);
    	//获取第二行
    	Row row = sheet.getRow(1);
    	//循环读取
    	for (int i = 1; i < 6; i++) {
			System.out.println(row.getCell(i));
		}
    }

猜你喜欢

转载自blog.csdn.net/weixin_44671176/article/details/103070070