package com.itheima.domain; public class Book { private Integer id; private String type; private String name; private String description; @Override public String toString() { return "Book{" + "id=" + id + ", type='" + type + '\'' + ", name='" + name + '\'' + ", description='" + description + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
BookDao
package com.itheima.dao; import com.itheima.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 { //这两种方法都可以使用 //表后面跟的是字段名称,values后面跟的是Book里的属性名 // @Insert("insert into tbl_book values (null, #{type}, #{name}, #{description})") @Insert("insert into tbl_book (type, name, description) values (#{type},#{name}#{description})") public void save(Book book); @Update("update tbl_book set type = #{type},name = #{name},description = #{description} where id = #{id}") public void update(Book book); @Delete("delete from tbl_book where id = #{id}") public void delete(Integer id); @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); @Select("select * from tbl_book") public List<Book> getAll(); }
BookService
package com.itheima.service; import com.itheima.domain.Book; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Select; import java.util.List; public interface BookService { /** * 保存 * @param book * @return */ public boolean save(Book book); /** * 修改 * @param book * @return */ public boolean update(Book book); /** * 根据id删除 * @param id * @return */ public boolean delete(Integer id); /** * 根据id查询 * @param id * @return */ public Book getById(Integer id); /** * 查询所有 * @return */ public List<Book> getAll(); }
BookServiceImpl
package com.itheima.service.impl; import com.itheima.dao.BookDao; import com.itheima.domain.Book; import com.itheima.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service//配置bean public class BookServiceImpl implements BookService{ @Autowired//自动装配 private BookDao bookDao; @Override public boolean save(Book book) { bookDao.save(book); return true; } @Override public boolean update(Book book) { bookDao.update(book); return true; } @Override public boolean delete(Integer id) { bookDao.delete(id); return true; } @Override public Book getById(Integer id) { return bookDao.getById(id); } @Override public List<Book> getAll() { return bookDao.getAll(); } }
BookController
package com.itheima.controller; import com.itheima.domain.Book; import com.itheima.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/books") public class BookController { /** * @RequestParam 用于接收url地址传参或者表单传参 * @RequestBody 用于接收json数据 * @PathVariable 用于接收路径参数,使用{参数名称}描述路径参数 * * 应用 * 后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广 * 如果发送非json格式数据,选用@RequestParam接收请求参数 * 采用RESTful进行开发,当参数较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值 */ @Autowired private BookService bookService; @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<Book> getAll() { return bookService.getAll(); } }
标签:功能模块,return,Book,id,SSM,book,整合,import,public From: https://www.cnblogs.com/Karl-hut/p/17560656.html