maven
继承spring-boot
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.6.RELEASEversion>
<relativePath/>
parent>
jdk
版本和字符集<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-textartifactId>
<version>1.2version>
dependency>
dependencies>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
src/main/resources/application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/react
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
jpa:
show-sql: true
hibernate:
ddl-auto: update
database: mysql
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
package com.example.react;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReactApplication {
public static void main(String[] args) {
SpringApplication.run(ReactApplication.class, args);
}
}
package com.example.react.model;
import lombok.*;
import lombok.experimental.Accessors;
import javax.persistence.*;
/**
* 用户类
*/
@Table(name = "t_user")
@Entity
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Accessors(chain = true)
public class User {
/**
* 用户ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* 用户名
*/
private String name;
}
package com.example.react.dao;
import com.example.react.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends JpaRepository<User,Long> {
}
package com.example.react.controller;
import com.example.react.model.User;
import com.example.react.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
/**
* 查询所有用户
* @return
*/
@GetMapping
public List<User> all(){
return this.userDao.findAll();
}
/**
* 保存用户
* 新增或更新
* @param user
* @return
*/
@PostMapping
public Object save(@RequestBody User user){
this.userDao.save(user);
return true;
}
/**
* 根据ID删除用户
* @param id
* @return
*/
@DeleteMapping("/{id}")
public Object delete(@PathVariable Long id){
this.userDao.deleteById(id);
return true;
}
}
create-react-app
npx create-react-app web
给命令会在当前目录下使用create-react-app
创建一个react
单页项目
web
目录,添加依赖库 npm install axios [email protected] --save
package.json
中增加前后端交互代理"proxy": "http://localhost:8080"
src
目录下无用的文件,只保留index.js
和App.js
,并修改文件使其能够运行import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
App.js
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
</div>
);
}
}
export default App;
index.js
中引入bootstrap
样式文件css
文件即可import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
接下来进行页面布局,这是一个简单的增删改查功能,所以只需要在一个页面编写全部功能即可,左侧为一个表格,右侧为一个表单,如下图
首先利用bootstrap
中提供的栅格模式,将页面分为左右两栏,两栏中分别有一个panel
render() {
return (
<div className="container-fluid" style={{marginTop: '20px'}}>
<div className="row">
<div className="col-xs-4 col-xs-offset-1">
<div className="panel panel-default">
<div className="panel-body">
表格区域
div>
div>
div>
<div className="col-xs-3 col-xs-offset-1">
<div className="panel panel-default">
<div className="panel-body">
表单区域
div>
div>
div>
div>
div>
);
}
<table className="table table-bordered">
<thead>
<tr>
<th>IDth>
<th>用户名th>
<th>操作th>
tr>
thead>
<tbody>
tbody>
table>
<form className="form-horizontal">
<div className="form-group">
<label htmlFor="name" className="col-xs-3">用户名label>
<div className="col-xs-8">
<input type="text" id="name" className="form-control"/>
div>
div>
<div className="form-group">
<div className="col-sm-offset-2 col-sm-10">
<button className="btn btn-default">提交button>
div>
div>
form>
state
constructor(props) {
super(props);
this.state = {
id:'',
name:'',
list:[]
}
}
App
组件挂载渲染完成后执行查询函数axios
import axios from 'axios';
query = () =>{
axios.get('/user').then(({data})=>{
this.setState({
list:data
});
});
}
componentDidMount(){
this.query();
}
<tbody>
{
this.state.list.map(item=>{
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>
<button className="btn btn-primary">修改</button>
<button className="btn btn-danger" style={{marginLeft:'5px'}}>删除</button>
</td>
</tr>
)
})
}
</tbody>
<input type="text" id="name" className="form-control" value={this.state.name} onChange={
(e)=>{
this.setState({
name:e.target.value
})
}
}/>
<button className="btn btn-default" onClick={this.handleFormSubmit}>提交</button>
handleFormSubmit = (e) => {
e.preventDefault();
if (this.state.name != '') {
axios.post('/user', {
id: !this.state.id ? '' : this.state.id,
name: this.state.name
}).then(({data}) => {
this.setState({
id: '',
name: ''
});
this.query();
})
}
}
<button className="btn btn-primary" onClick={() => {
this.setState({id: item.id, name: item.name})
}}>修改
</button>
<button className="btn btn-danger" style={{marginLeft: '5px'}}
onClick={() => {
this.deleteItem(item)
}}>删除
</button>
deleteItem = (item) => {
axios.delete(`/user/${item.id}`).then(({data}) => {
this.query();
})
}
npm start
启动前端App.css
.table th, .table td {
text-align: center;
vertical-align: middle!important;
}
App.js
中引入App.css
import './App.css'
源码地址:
https://gitee.com/qinaichen/react-crud.git