- 数据层代码
// 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(); }}
经验总结扩展阅读
-
-
-
床底原来这就是养金毛和养柯基的区别?哈哈哈哈哈画风真的差好多!
-
-
-
-
-
好听有内涵的代账公司名字320个 代理记账公司名字大全参考
-
-
-
-
2022年10月29日适合打官司吗 2022年10月29日打官司好吗
-
-
刘小明出生在一个贫困家庭|从一无所有到身价千万,这个穷小子仅仅用了5年时间
-
-
-
-
-
文物 上古时代的逆天文物,酷似汽车方向盘,众人大呼:穿越时空
-