springboot+mybatisplus+vue一个页面显示两个表
时间: 2023-09-16 21:12:58 浏览: 215
好的,你可以按照以下步骤来实现:
1. 首先需要确保你的SpringBoot项目中已经集成了MyBatisPlus,并且已经连接了对应的数据库。
2. 然后创建对应的实体类,分别对应两个表的结构。
3. 接着在MyBatisPlus的Mapper层中编写对应的SQL语句,可以使用MyBatisPlus提供的注解或者XML文件来实现。
4. 在SpringBoot的Controller层中编写对应的接口方法,通过MyBatisPlus的Service层调用Mapper层的方法,获取对应的数据。
5. 最后在前端使用Vue来渲染页面,通过调用后端接口获取数据并显示在页面上。
需要注意的是,在前端渲染页面的时候,需要根据对应的数据结构来进行渲染,如果两个表的结构不同,需要进行数据的合并和处理。
相关问题
springboot+mybatisplus+vue一个页面显示两个表示例代码
好的,以下是一个简单的示例代码:
1. 实体类
```
@TableField(exist = false)
private String name; // 表示该字段不是数据库表中的字段
@TableField(exist = false)
private Integer age; // 表示该字段不是数据库表中的字段
```
2. Mapper层
```
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT id, name, age FROM user")
List<User> getUserList();
}
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
@Select("SELECT id, user_id, product_name, price FROM order")
List<Order> getOrderList();
}
```
3. Service层
```
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.getUserList();
}
}
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Override
public List<Order> getOrderList() {
return orderMapper.getOrderList();
}
}
```
4. Controller层
```
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/userList")
public List<User> getUserList() {
return userService.getUserList();
}
}
@RestController
@RequestMapping("/api")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/orderList")
public List<Order> getOrderList() {
return orderService.getOrderList();
}
}
```
5. 前端页面
```
<template>
<div>
<table>
<thead>
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr v-for="user in userList" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>订单ID</th>
<th>用户ID</th>
<th>商品名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<tr v-for="order in orderList" :key="order.id">
<td>{{ order.id }}</td>
<td>{{ order.user_id }}</td>
<td>{{ order.product_name }}</td>
<td>{{ order.price }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
userList: [],
orderList: []
}
},
mounted() {
this.getUserList()
this.getOrderList()
},
methods: {
getUserList() {
axios.get('/api/userList')
.then(res => {
this.userList = res.data
})
.catch(err => {
console.error(err)
})
},
getOrderList() {
axios.get('/api/orderList')
.then(res => {
this.orderList = res.data
})
.catch(err => {
console.error(err)
})
}
}
}
</script>
```
以上代码仅供参考,实际应用时需要根据具体的业务场景进行调整。
springboot+mybatisplus+vue怎么在一个页面显示两个表
在Spring Boot + Mybatis Plus + Vue的组合中,你可以通过以下步骤来在一个页面显示两个表:
1. 在后端Java代码中,使用Mybatis Plus的Mapper接口操作数据库来查询两个表的数据,然后将数据以JSON格式返回给前端Vue页面。
2. 在前端Vue页面中,通过Vue的异步组件和axios库来发送请求,获取后端返回的数据,然后使用Vue的模板语法和数据绑定来渲染页面,显示两个表的数据。
下面是一个简单的示例代码:
在Java代码中,使用Mybatis Plus的Mapper接口操作数据库来查询两个表的数据:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RoleMapper roleMapper;
@Override
public List<UserRoleVO> getUserRoleList() {
List<UserRoleVO> userRoleList = new ArrayList<>();
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
UserRoleVO userRoleVO = new UserRoleVO();
BeanUtils.copyProperties(user, userRoleVO);
Role role = roleMapper.selectById(user.getRoleId());
userRoleVO.setRoleName(role.getRoleName());
userRoleList.add(userRoleVO);
}
return userRoleList;
}
}
```
在前端Vue页面中,使用异步组件和axios库来发送请求,获取后端返回的数据:
```vue
<template>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in userRoleList" :key="index">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.roleName}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'UserList',
data () {
return {
userRoleList: []
}
},
created () {
this.getUserRoleList()
},
methods: {
getUserRoleList () {
axios.get('/api/userRoleList').then(response => {
this.userRoleList = response.data
}).catch(error => {
console.log(error)
})
}
}
}
</script>
```
在这个示例中,我们通过Java代码实现了查询用户信息和用户角色信息的逻辑,并将查询结果转换为`UserRoleVO`对象列表返回给前端。然后在前端Vue页面中,我们通过异步组件和axios库发送请求,获取后端返回的`UserRoleVO`对象列表,并使用Vue的模板语法和数据绑定来渲染页面,显示用户信息和用户角色信息的表格。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)