前后端-SpringBoot-JPA的简单写法(配合前端vue)
一、JAP的简单使用
1、实体类
@Entity
@Data
public class Book {
@Id
@GeneratedValue ( strategy = GenerationType. IDENTITY )
private Integer id;
private String name;
private String author;
private String publish;
private long pages;
private double price;
@Column ( name = "bookcaseid" )
private long bookCaseId;
@Column ( name = "abled" )
private String able;
}
2、接口
public interface IBook extends JpaRepository < Book, Integer> { }
3、Mapper
后面再解释吧,这个要配合前端使用
@RestController
@RequestMapping ( "/book" )
public class BookController {
@Autowired
private IBook iBook;
@GetMapping ( "/findAll/{page}/{size}" )
public Page< Book> findAll ( @PathVariable ( "page" ) Integer page, @PathVariable ( "size" ) Integer size) {
PageRequest request = PageRequest. of ( page, size) ;
Page< Book> books = iBook. findAll ( request) ;
for ( Book i: books) {
if ( i. getAble ( ) == null ) {
i. setAble ( "状态未知" ) ;
} else {
if ( i. getAble ( ) . equals ( "1" ) ) {
i. setAble ( "可以借出" ) ;
} else {
i. setAble ( "不可借出" ) ;
}
}
}
return books;
}
@PostMapping ( "/save" )
public String saveBook ( @RequestBody Book book) {
Book returnBook = iBook. save ( book) ;
if ( returnBook != null ) {
return "success" ;
} else {
return "error" ;
}
}
@GetMapping ( "/findById/{id}" )
public Book findById ( @PathVariable ( "id" ) Integer id) {
Book book = iBook. findById ( id) . get ( ) ;
return book;
}
@GetMapping ( "/deleteById/{id}" )
public void deleteById ( @PathVariable ( "id" ) Integer id) {
iBook. deleteById ( id) ;
}
@PostMapping ( "/update" )
public String updateById ( @RequestBody Book book) {
Book result = iBook. save ( book) ;
if ( result != null ) {
return "success" ;
} else {
return "error" ;
}
}
}
二、配合前端的axios的写法
这里专门记录一下和前端配合的写法
第一种:配合分页机制
@GetMapping ( "/findAll/{page}/{size}" )
public Page< Book> findAll ( @PathVariable ( "page" ) Integer page, @PathVariable ( "size" ) Integer size) {
PageRequest request = PageRequest. of ( page, size) ;
Page< Book> books = iBook. findAll ( request) ;
for ( Book i: books) {
if ( i. getAble ( ) == null) {
i. setAble ( "状态未知" ) ;
} else {
if ( i. getAble ( ) . equals ( "1" ) ) {
i. setAble ( "可以借出" ) ;
} else {
i. setAble ( "不可借出" ) ;
}
}
}
return books;
}
created ( ) {
const _this = this ;
axios. get ( 'http://localhost:8181/book/findAll/0/8' ) . then ( function ( resp) {
console. log ( resp) ;
_this. tableData = resp. data. content;
_this. pageSize = resp. data. size;
_this. pageTotal = resp. data. totalElements;
} )
} ,
data ( ) {
return {
pageSize: 0 ,
pageTotal: 0 ,
tableData: [ ] ,
}
} ,
< el- pagination
align= "center"
background
layout= "prev, pager, next"
: page- size= "pageSize"
: total= "pageTotal"
@current- change= "page_change" >
< / el- pagination>
第二种:一般使用方法
@GetMapping ( "/findById/{id}" )
public Book findById ( @PathVariable ( "id" ) Integer id) {
Book book = iBook. findById ( id) . get ( ) ;
return book;
}
created ( ) {
const _this = this ;
axios. get ( "http://localhost:8181/book/findById/" + this . $route. query. id) . then ( function ( res) {
_this. ruleForm = res. data;
} )
} ,
methods: {
editBook ( row) {
this . $router. push ( {
path: '/BookUpdate' ,
query: {
id: row. id
}
} ) ;
} ,
第三种:使用Book这种对象的传参
@PostMapping ( "/update" )
public String updateById ( @RequestBody Book book) {
Book result = iBook. save ( book) ;
if ( result != null) {
return "success" ;
} else {
return "error" ;
}
}
submitForm ( formName) {
const _this = this ;
this . $refs[ formName] . validate ( ( valid) => {
if ( valid) {
console. log ( _this. ruleForm) ;
axios. post ( 'http://localhost:8181/book/update' , this . ruleForm) . then ( function ( resp) {
if ( resp. data === "success" ) {
console. log ( "成功!" ) ;
_this. success ( ) ;
_this. $router. push ( '/bookManage' ) ;
} else {
console. log ( "失败!" ) ;
_this. error ( ) ;
}
} )
} else {
return false ;
}
} ) ;
} ,