| public class Book { |
| |
| private Integer id; |
| |
| private String type; |
| |
| private String name; |
| |
| private String 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Book{" + |
| "id=" + id + |
| ", type='" + type + '\'' + |
| ", name='" + name + '\'' + |
| ", description='" + description + '\'' + |
| '}'; |
| } |
| |
| } |
| public interface BookDao { |
| |
| |
| @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(); |
| |
| } |
| @Transactional |
| public interface BookService { |
| |
| |
| |
| |
| |
| |
| public boolean save(Book book); |
| |
| |
| |
| |
| |
| |
| public boolean update(Book book); |
| |
| |
| |
| |
| |
| |
| public boolean delete(Integer id); |
| |
| |
| |
| |
| |
| |
| public Book getById(Integer id); |
| |
| |
| |
| |
| |
| public List<Book> getAll(); |
| |
| } |
| @Service |
| public class BookServiceImpl implements BookService { |
| |
| @Autowired |
| private BookDao bookDao; |
| |
| 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<Book> getAll() { |
| return bookDao.getAll(); |
| } |
| |
| } |
| @RestController |
| @RequestMapping("/books") |
| public class BookController { |
| |
| @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(); |
| } |
| } |
| # spring配置类中添加如下 |
| @EnableTransactionManagement |
| |
| # jdbc配置类中添加如下 |
| @Bean |
| public PlatformTransactionManager transactionManager(DataSource dataSource){ |
| DataSourceTransactionManager ds = new DataSourceTransactionManager(); |
| ds.setDataSource(dataSource); |
| return ds; |
| } |
| |
| # 业务层添加注解 |
| @Transactional |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术