SSM整合以及相关补充( 五 )

  1. 数据层代码
// 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();}
  1. 业务层代码
// 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();    }}

经验总结扩展阅读