使用hutool工具包 配置mysql

1.pom.xml 添加

<!-- hutool工具类 -->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>4.1.19</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.14</version>
		</dependency>

 

2.src/main/resources/下配置db.setting 文件 如下:

## db.setting文件
url = jdbc:mysql://localhost:3306/数据库库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
user = 数据库用户名
pass = 数据库密码
driver =com.mysql.cj.jdbc.Driver
## 可选配置
# 是否在日志中显示执行的SQL
showSql = true
# 是否格式化显示的SQL
formatSql = false
# 是否显示SQL参数
showParams = true

3.测试 

import 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,我理解了你的问题。首先,我们需要添加hutool的依赖到pom.xml文件中,如下所示: ``` <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.2</version> </dependency> ``` 然后,在Spring Boot项目中实现导入和导出功能的代码如下所示: ``` import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/user") public class UserController { /** * 导出用户数据到Excel文件 */ @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { // 创建ExcelWriter对象 ExcelWriter writer = ExcelUtil.getWriter(); // 写入表头 writer.addHeaderAlias("id", "ID"); writer.addHeaderAlias("name", "姓名"); writer.addHeaderAlias("phone", "电话"); // 查询用户列表 List<User> userList = userService.list(); // 写入数据 writer.write(userList, true); // 输出Excel文件 String fileName = "user.xlsx"; response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); writer.flush(response.getOutputStream()); writer.close(); } /** * 导入Excel文件中的用户数据 */ @PostMapping("/import") public Map<String, Object> imports(@RequestParam(value = "file") MultipartFile file) throws IOException { // 创建ExcelReader对象 InputStream inputStream = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(inputStream); // 读取Excel文件中的用户数据 List<User> userList = reader.readAll(User.class); // 批量保存用户数据到数据库 userService.saveBatch(userList); // 返回导入结果 Map<String, Object> result = new HashMap<>(); result.put("code", "success"); return result; } // UserService的声明和注入省略 } ``` 在代码中,我们首先在UserController中定义了/export和/import两个接口,分别对应导出和导入功能。在导出接口中,我们使用ExcelWriter对象来生成Excel文件,并将用户列表写入到Excel文件中。在导入接口中,我们使用ExcelReader对象来读取上传的Excel文件,并将用户数据保存到数据库。 以上就是使用hutool工具包在Spring Boot中实现用户数据导入导出的全部内容。如果有任何问题,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值