vue ui
命令可视化创建工具Vue前端项目。vue -v
命令查看版本号),你需要更新vue版本,操作如下:npm uninstall vue-cli -g
命令,全局卸载vue-cli旧版本(不先卸载直接安装的话,有可能会失败)npm install vue-cli -g
命令,重新下载。vue ui
命令,如下图操作:vue ui
命令后,会自动启动浏览器,进入下图界面:ctrl+c
——》y——》关闭npm run serve
启动,访问localhost:8080进行测试。Employees.vue
<template>
<div>
<table>
<thead>
<tr>
<td>编号td>
<td>姓名td>
<td>性别td>
<td>部门td>
tr>
thead>
<tbody>
<tr v-for="item in employees">
<th>{{item.id}}th>
<th>{{item.name}}th>
<th>{{item.sex}}th>
<th>{{item.department}}th>
tr>
tbody>
table>
div>
template>
<script>
export default {
name: "Employees",
data(){
return {
employees: [
{
id: 1,
name: '张三',
sex: '男',
department: '运维部'
},
{
id: 2,
name: '李四',
sex: '男',
department: '销售部'
},
{
id: 3,
name: '张红',
sex: '男',
department: '开发部'
}
]
}
}
}
script>
<style scoped>
style>
data里面伪造了的三个数据进行测试,配置好路由进行测试:/src/router/index.js
{
path: '/employees',
name: 'Employees',
component: () => import('../views/Employees')
}
输入npm run serve
启动项目,访问localhost:8080/employees
到这里前端可以暂时停一下,开始搭建后端项目
pom.xml
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.0version>
dependency>
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8181
CREATE TABLE employees
(
id INT(10) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
sex VARCHAR(5) NULL DEFAULT NULL COMMENT '性别',
department VARCHAR(10) NULL DEFAULT NULL COMMENT '部门',
PRIMARY KEY (id)
);
INSERT into employees VALUES (1,'李莉','女','设计部');
INSERT into employees VALUES (2,'王波','男','开发部');
INSERT into employees VALUES (3,'张美','女','运维部');
INSERT into employees VALUES (4,'李晓勇','男','销售部');
@Data
public class Employees {
private Integer id;
private String name;
private String sex;
private String department;
}
@Mapper
@Repository
public interface EmployeeMapper extends BaseMapper<Employees> {
}
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/all_employees")
public List<Employees> allEmployees(){
return employeeMapper.selectList(null);
}
}
现在前端测试页面已经写好,后端的数据接口也已经写好:
vue add axios
执行,安装axios插件,可以直接使用。Employees.vue
中加上:created()
方法会在页面加载前完成。 created() {
//get获得url里面的JSON数据后,通过then回调,赋值给前面data()中的employees数组
axios.get('http://localhost:8181/employee/all_employees').then((resp)=> {
this.employees = resp.data;
})
}
完整代码:
<template>
<div>
<table>
<thead>
<tr>
<td>编号td>
<td>姓名td>
<td>性别td>
<td>部门td>
tr>
thead>
<tbody>
<tr v-for="item in employees">
<th>{{item.id}}th>
<th>{{item.name}}th>
<th>{{item.sex}}th>
<th>{{item.department}}th>
tr>
tbody>
table>
div>
template>
<script>
export default {
name: "Employees",
data(){
return {
employees: [
{
id: 1,
name: '张三',
sex: '男',
department: '运维部'
},
{
id: 2,
name: '李四',
sex: '男',
department: '销售部'
},
{
id: 3,
name: '张红',
sex: '男',
department: '开发部'
}
]
}
},
created() {
axios.get('http://localhost:8181/employee/all_employees').then((resp)=> {
this.employees = resp.data;
})
}
}
script>
<style scoped>
style>
@CrossOrigin
注解在后端接口:@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/all_employees")
@CrossOrigin
public List<Employees> allEmployees(){
return employeeMapper.selectList(null);
}
}