当当网Day1

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);
    }

}

服务端页面效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值