spring boot+mybatisplus+vue+elementui+easypoi实现excel导出,都需要操作一对多形式的列表,请给我前后端详细的步骤和代码
时间: 2023-12-18 15:04:52 浏览: 99
信息管理系统(SpringBoot+Mybatis+POI+Vue+ElementUI)
好的,您需要在后端使用Mybatis-Plus来操作数据库,同时使用Spring Boot框架来搭建后端接口。在前端,您需要使用Vue框架和ElementUI组件库进行开发,同时使用Easypoi来实现Excel导出。
以下是详细步骤和代码:
后端:
1. 在pom.xml中添加Mybatis-Plus和Easypoi的依赖
```
<!-- Mybatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- Easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.1</version>
</dependency>
```
2. 定义实体类和Mapper类
```
// 实体类,对应数据库中的一张表
@Data
public class User {
private Long id;
private String name;
private Integer age;
private List<Role> roles; // 一对多关系,一个用户对应多个角色
}
@Data
public class Role {
private Long id;
private String name;
}
// Mapper类
public interface UserMapper extends BaseMapper<User> {
List<User> selectUsers(); // 查询所有用户和对应的角色
}
```
3. 在ServiceImpl类中实现查询方法
```
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> getUsersWithRoles() {
return baseMapper.selectUsers();
}
}
```
4. 在Controller类中定义接口
```
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> getUsers() {
return userService.getUsersWithRoles();
}
}
```
5. 在Controller类中实现Excel导出接口
```
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
...
@GetMapping("/export")
public void exportUsers(HttpServletResponse response) throws IOException {
// 查询所有用户和对应的角色
List<User> userList = userService.getUsersWithRoles();
// 定义表头和数据
ListExportParams params = new ListExportParams("用户列表", "Sheet1");
LinkedHashMap<String, String> headers = new LinkedHashMap<>();
headers.put("name", "名称");
headers.put("age", "年龄");
headers.put("roles.name", "角色"); // 用"."来表示一对多关系
params.setHeaders(headers);
List<Map<String, Object>> data = EasyPoiUtils.list2Map(userList);
// 导出Excel
Workbook workbook = ExcelExportUtil.exportExcel(params, headers, data);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=users.xlsx");
response.flushBuffer();
workbook.write(response.getOutputStream());
}
}
```
前端:
1. 安装Vue和ElementUI
```
npm install vue
npm install element-ui
```
2. 在组件中使用ElTable和ElTableColumn来展示数据
```
<template>
<div>
<el-table :data="users">
<el-table-column prop="name" label="名称"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column label="角色">
<template slot-scope="scope">
<span v-for="(role, index) in scope.row.roles" :key="index">{{ role.name }}</span>
</template>
</el-table-column>
</el-table>
<el-button type="primary" @click="exportUsers">导出Excel</el-button>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
users: []
}
},
created() {
this.getUsers()
},
methods: {
getUsers() {
axios.get('/user/list').then(response => {
this.users = response.data
})
},
exportUsers() {
window.open('/user/export', '_blank')
}
}
}
</script>
```
以上就是详细的步骤和代码,希望能帮助到您。
阅读全文