栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用springboot把后台数据库分类数据显示给前台

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用springboot把后台数据库分类数据显示给前台

根据sorted进行了排序 效果如下:

使用vue实现,index.html核心代码

全部
{{index+1}}/{{blogcate.categoryTitle}}

index.js核心代码:

var vue=new Vue({
    el:"#app",
    data:{
        
        blogCategories:[],
       
    },
//数据和方法加载完毕的钩子
created:function () {

        //用ajax异步执行获取博客分类内容
        this.loadCategory()
     
    },
        
methods:{
     loadCategory:function () {
         	//this是指vue实例
            var that =this
			//用mybatisplus插件获取后端数据库数据res
            axios.get("/api/blogCategory/load").then(function
                (res) {
                //res不知道返回什么,可以去打印用浏览器查看数据
                that.blogCategories = res.data
                。。。
            })
        },
}
    。。。

BlogCategoryController的java代码返回json数据给前台,用axios接受,实现异步处理!

@Controller
public class BlogCategoryController {
    @Autowired
    private IBlogCategoryService blogCategoryService;

    @ResponseBody
    @GetMapping("/api/blogCategory/load")
    public List findCategories(){
        return blogCategoryService.findBlogCategies();
    }
}

IBlogCategoryService接口 ,简单的增删改查接口用IService封装好了,实现复用!

public interface IBlogCategoryService extends IService {

    
    List findBlogCategies();
}

BlogCategoryServiceImpl实现类,ServiceImpl实现了简单的增删改查!

@Service
@Slf4j
public class BlogCategoryServiceImpl extends ServiceImpl implements IBlogCategoryService {
    
    @Override
    public List findBlogCategies() {
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogCategory::getStatus,1);
//        queryWrapper.orderByAsc(BlogCategory::getSorted);
        List blogCategories = this.list(queryWrapper);
        //用stream流排序去取代SQL排序
        if(!CollectionUtils.isEmpty(blogCategories)){
            blogCategories = blogCategories
                    .stream()
                    .sorted((a,b)->a.getSorted()-b.getSorted())
                    .collect(Collectors.toList());
        }


        return blogCategories;


    }
}

至此前后端分类动态生成已经打通关!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/708793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号