- 数据层代码
// BookDaopackage 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 { // 采用Mapper代理开发 // 采用#{}匹配参数 @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();}
- 业务层代码
// BookServicepackage com.itheima.service;import com.itheima.domain.Book;import org.springframework.transaction.annotation.Transactional;import java.util.List;// 给出事务开启标志@Transactionalpublic 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();}// BookServiceImplpackage 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;// 标记为Bean@Servicepublic 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(); }}
经验总结扩展阅读
-
玩具批发从哪里进货,东莞玩具批发城在哪里拿货最便宜
-
-
-
在与同事、朋友的交往中|如何拒绝借钱给别人,这几个理由你可以用上
-
-
-
不要对退休生活有歧视,我们误解了,退休不再工作到底意味了什么
-
-
-
-
-
-
金建希 布里吉特欢迎韩国第一夫人!穿白裙扮嫩也输了,金建希幼态脸太嫩
-
-
-
-
-
-
2023年2月15日适合盖楼吗 2023年2月15日盖楼吉日一览表
-