后台代码:
实体类:
@ToString
public class Book {
private Integer id;
private String bookname;
private Float price;
private String booktype;
public Book(Integer id, String bookname, Float price, String booktype) {
this.id = id;
this.bookname = bookname;
this.price = price;
this.booktype = booktype;
}
public Book() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
}
数据访问层(接口):
@Repository
public interface BookMapper {
int deleteByPrimaryKey(Integer id);
int insert(Book record);
List all(Book book);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
}
接口实现类:
id, bookname, price, booktype
业务逻辑层(接口):
public interface IBookService {
int insert(Book record);
List all(Book book);
}
实现类:
@Service
public class BookServiceImpl implements IBookService {
@Autowired
private BookMapper bookMapper;
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public List all(Book book) {
return bookMapper.all(book);
}
}
控制层:
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private IBookService iBookService;
@RequestMapping("/add")
public ResponseData> add(Book book){
try {
iBookService.insert(book);
return new ResponseData<>(200,"新增成功!",null);
} catch (Exception e) {
e.printStackTrace();
return new ResponseData<>(500,"新增失败!",null);
}
}
@RequestMapping("/all")
public ResponseData> all(Book book){
try {
List all = iBookService.all(book);
return new ResponseData<>(200,"查询成功!",all);
} catch (Exception e) {
e.printStackTrace();
return new ResponseData<>(500,"查询失败!",null);
}
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class ResponseData {
private int code;
private String msg;
private T data;
}
前端代码:
export default {
//服务器
'SERVER': 'http://localhost:8080/spboot',
'ADD': '/book/add',
'ALL': '/book/all',
//获得请求的完整地址,用于mockjs测试时使用
'getFullPath': k => {
return this.SERVER + this[k];
}
}
页面:
SpringBoot阶段机试,ts={{ts}}
查询
添加
取 消
确 定



