package com.dao;
import com.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface BookDao {
@Insert("inser into tb_book values (null,#{type},#{name} ,#{description})")//book中的属性
public void save(Book book);//存一个
@Update("update tb_book set type =#{type},name =#{name},description=#{description} where id=#{id}")
public void update(Book book);//更新
@Delete("delete from tb_book where id=#{id}")
public void delete(Integer id);//根据id删除
@Select("select *from tb_book where id=#{id}")
public Book getById(Integer id);//根据id查找单个
@Select("select *from tb_book")
public List getAll();//获取全部
}
第二步service包下建立一个BookService接口和一个impl包
BookService
package com.service;
import com.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface BookService {
public boolean save(Book book);//存一个
public boolean update(Book book);//更新
public boolean delete(Integer id);//根据id删除
public Book getById(Integer id);//根据id查找单个
public List getAll();//获取全部
}
impl包建立BookServiceImpl
package com.service.impl;
import com.dao.BookDao;
import com.domain.Book;
import com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
//需要用到BookDao的接口
@Autowired
private BookDao bookDao;//这里是没错的,只是idea为了给我提供帮助,但是我们不需要,只是看着别扭
public boolean save(Book book) {
bookDao.save(book);
return true;
}
public boolean update(Book book) {
bookDao.update(book);
return true;
}
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
public Book getById(Integer id) {
return bookDao.getById(id);
}
public List getAll() {
return bookDao.getAll();
}
}
第三步controller包下新建一个BookController
package com.controller;
import com.dao.BookDao;
import com.domain.Book;
import com.service.BookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;//这里是没错的,只是idea为了给我提供帮助,但是我们不需要,只是看着别扭
@PostMapping
public boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public boolean update(@RequestBody Book book) {
return bookService.update(book);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping
public List getAll() {
return bookService.getAll();
}
}



