基于Spring+SpringMVC+MyBatis开发书评网(四)首页-图书分类展示模块

一、 前提 

1、 数据库建表

 

2、 Bootstrap引入

响应式布局

根据不同的设备动态调节不同的屏幕设置

 

 

3、 效果图

 

 

二、 实现

1、 创建实体类

package com.imooc.reader.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

/**
 * @ClassName Category
 * @Description 创建图书分类实体类
 * @date 2021/5/8 20:58
 * @Param
 * @return
 */
// 映射对应的数据库
@TableName("category")
public class Category {
    // 映射对应的主键(id),类型自增
    @TableId(type = IdType.AUTO)
    private Long categoryId;

    // 映射对应的属性
    @TableField("category_name")
    private String categoryName;

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
}

2、 创建对应Mapper接口

package com.imooc.reader.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.Category;

/**
 * @ClassName CategoryMapper
 * @Description 图书分类Mapper接口
 * @date 2021/5/8 21:07
 * @Param
 * @return
 */
// BaseMapper核心父接口,内有增删改查的方法
public interface CategoryMapper extends BaseMapper<Category> {

}

 

3、 创建对应mapper.xml

<?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.imooc.reader.mapper.CategoryMapper">
<!-- 是否要添加子标签,要看Mapper接口中是否有方法的定义 -->
</mapper>

 

4、 service层创建服务接口

package com.imooc.reader.service;

import com.imooc.reader.entity.Category;

import java.util.List;

/**
 * @ClassName CategoryService
 * @Description 图书分类服务接口
 * @date 2021/5/8 21:33
 * @Param
 * @return
 */
public interface CategoryService {
    // list集合存储
    public List<Category> selectAll();
}

 

5、  service层实现服务接口

package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Category;
import com.imooc.reader.mapper.CategoryMapper;
import com.imooc.reader.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CategoryServiceImpl
 * @Description 图书分类服务接口实现类
 * @date 2021/5/8 21:34
 * @Param
 * @return
 */
// 注意一下 bean的ID,保持与服务接口一致
// 面向接口编程,隐藏底层类的创建
@Service("categoryService")
// 事务设置,readOnly 只读,表明不需要事务处理
// 事务的开启取决与业务: 查询可以不需要,插入需要
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class CategoryServiceImpl implements CategoryService {

    // 注入前面写好的接口
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询所有图书分类
     *
     * @return 图书分类List
     */
    public List<Category> selectAll() {
        // selectList: 查询多个列表,返回多个数据
        // QueryWrapper: 条件构造器
        List<Category> list = categoryMapper.selectList(new QueryWrapper<Category>());
        return list;
    }
}

 

6、 生成测试用例

package com.imooc.reader.service.impl;

import com.imooc.reader.entity.Category;
import com.imooc.reader.service.CategoryService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author lsy
 * @version 1.0
 * @description: selectAll()测试用例
 * @date: 2021/5/8 21:50
 * @param: null
 * @return:
 */

// 声明IOC容器的加载
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class CategoryServiceImplTest {
    // 注入
    @Resource
    private CategoryService categoryService;

    @Test
    public void selectAll() {
        List<Category> list = categoryService.selectAll();
        System.out.println(list);
    }
}

 

7、 控制器调用

package com.imooc.reader.controller;

import com.imooc.reader.entity.Category;
import com.imooc.reader.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BookController
 * @Description 图书控制类
 * @date 2021/5/8 21:54
 * @Param
 * @return
 */
@Controller
public class BookController {
    @Resource
    private CategoryService categoryService;

    /**
     * 显示首页
     * @return
     */
    @GetMapping("/")
    public ModelAndView showIndex(){
        ModelAndView mav = new ModelAndView("/index");
        List<Category> categoryList = categoryService.selectAll();
        mav.addObject("categoryList", categoryList);
        return mav;
    }
}

 

8、 组合模板引擎

动态生成数据

<div class="col-8 mt-2">
                <span data-category="-1" style="cursor: pointer" class="highlight  font-weight-bold category">全部</span>
                |
                    <#list  categoryList as category>
                        <a style="..." data-category="${category.categoryId}" class="text-black-50 font-weight-bold category">${category.categoryName}</a>
                        <#if category_has_next>|</#if>
                    </#list>
        </div>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L烧鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值