Book
public class Book{
private Integer bookId;
private String bookName;
private Double bookPrice;
private Date groundingDate;
private Integer inventory;//存货
private String author;
private Integer saleNum;//销量
private String messages;//评价
private String dangPrice;//当当价
private String productImage;
}
BookMapper&&BookMapper.xml
public interface BookMapper {
//编辑推荐
List<Book> selectBooksByComment();
//热卖图书
List<Book> selectBooksByHotBuy();
//最新上架
List<Book> selectBooksByNewDate();
//新书热卖
List<Book>selectBooksByNewDateAndHotBuy();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.baizhi.mapper.BookMapper">
<resultMap id="bookResultMap" type="book">
<id column="book_id" property="bookId"/>
<result column="book_name" property="bookName"/>
<result column="book_price" property="bookPrice"/>
<result column="groundingDate" property="groundingDate"/>
<result column="inventory" property="inventory"/>
<result column="author" property="author"/>
<result column="salenum" property="saleNum"/>
<result column="messages" property="messages"/>
<result column="Dangprice" property="dangPrice"/>
<result column="product_image" property="productImage"/>
</resultMap>
<!-- 作者推荐-->
<select id="selectBooksByComment" resultMap="bookResultMap">
select * from d_book order by salenum asc limit 2
</select>
<!-- 热销推荐-->
<select id="selectBooksByHotBuy" resultMap="bookResultMap">
select * from d_book order by salenum desc limit 3
</select>
<!-- 最新上架-->
<select id="selectBooksByNewDate" resultMap="bookResultMap">
select * from d_book order by groundingDate desc limit 3
</select>
<!--新书热卖-->
<select id="selectBooksByNewDateAndHotBuy" resultMap="bookResultMap">
select * from d_book order by groundingDate desc ,salenum desc limit 2
</select>
</mapper>
遇到错误分析
Mapper.xml映射异常。
错误原因:使用MybatisX插件 直接生成了方法为resultType映射,忘记修改。
错误代码
<mapper namespace="com.baizhi.mapper.BookMapper"> <resultMap id="bookResultMap" type="book"></resultMap> <select id="selectBooksByComment" resultType="com.baizhi.entity.Book"> select * from d_book order by salenum asc limit 2 </select>
正确代码
<select id="selectBooksByNewDateAndHotBuy" resultMap="bookResultMap"> select * from d_book order by groundingDate desc ,salenum desc limit 2 </select>
Service&&ServiceImpl
public interface BookService {
//编辑推荐
List<Book> selectBooksByComment();
//热卖图书
List<Book> selectBooksByHotBuy();
//最新上架
List<Book> selectBooksByNewDate();
//新书热卖
List<Book>selectBooksByNewDateAndHotBuy();
}
@Service
@Transactional(readOnly = true)
public class BookServiceImpl implements BookService{
@Autowired
private BookMapper bookMapper;
@Override
public List<Book> selectBooksByComment() {return bookMapper.selectBooksByComment();}
@Override
public List<Book> selectBooksByHotBuy() {
return bookMapper.selectBooksByHotBuy();
}
@Override
public List<Book> selectBooksByNewDate() {
return bookMapper.selectBooksByNewDate();
}
@Override
public List<Book> selectBooksByNewDateAndHotBuy() {
return bookMapper.selectBooksByNewDateAndHotBuy();
}
}
Controller
@Controller
@RequestMapping("book")
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("showAll.do")
private String showAll(Model model){
List<Book> books4 = bookService.selectBooksByComment();
List<Book> books1 = bookService.selectBooksByNewDateAndHotBuy();
List<Book> books2 = bookService.selectBooksByHotBuy();
List<Book> books3 = bookService.selectBooksByNewDate();
model.addAttribute("books1",books1);
model.addAttribute("books2",books2);
model.addAttribute("books3",books3);
model.addAttribute("books4",books4);
return "/main/main";
}
}
TestMapper
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestBookMapper {
@Autowired
private BookMapper bookMapper;
@Test
public void TestShow(){
List<Book> books2 = bookMapper.selectBooksByComment();
System.out.println("根据编辑查询------------------------------------------------");
books2.forEach(System.out::println);
}
}
服务端页面效果