1、异常:Field bookMapper in com.aaa.lee.springboot.service.BookService required a bean of type 'com.aaa.lee.springboot.mapper.BookMapper' that could not be found.
原因:在service中使用@Autowired注解注入了mapper,最终mapper没有注入成功,也就是mapper并没有以bean的形式配置进application.xml配置文件(没有mapper的扫描器)
需要添加@Mapper注解;
2、使用注解的形式写上SQL语句,实体类的属性和数据库的字段映射问题:
当使用@Select @Update @Delete @Insert来替代mapper.xml的时候,需要把数据库表中字段名和实体类的属性要完全一致!
1 package com.aaa.liu.springboot.mapper;
2
3 import com.aaa.liu.springboot.model.Book;
4 import org.apache.ibatis.annotations.Delete;
5 import org.apache.ibatis.annotations.Insert;
6 import org.apache.ibatis.annotations.Select;
7 import org.apache.ibatis.annotations.Update;
8
9 import java.util.List;
10
11
12 /**
13 * @Author 刘其佳
14 * 2019/8/16 -- 20:48
15 * @Version 1.0
16 */
17 public interface BookMapper {
18 //查询
19 @Select("select id,book_name bookName,book_price bookPrice from book ")
20 List<Book> selectAll();
21
22 //添加
23 @Insert("insert into book(book_name,book_price) values(#{bookName},#{bookPrice})")
24 int addBook(Book book);
25
26 //根据ID进行查询
27 @Select("select id,book_name bookName,book_price bookPrice from book where id=#{id}")
28 List<Book> selectById(int id);
29
30 //修改
31 @Update("update book set book_name=#{bookName},book_price=#{bookPrice} where id=#{id}")
32 int updateBook(Book book);
33
34 //删除
35 @Delete("delete from book where id=#{id}")
36 int deleteBook(int id);
37 }