2.9.6 将jt项目改造为mybatis-plus后台

本文详细介绍了如何将JT项目的后台改造为使用Mybatis-Plus,包括导入相关jar包,编辑POJO类,定义Mapper接口,修改YML配置文件,以及编写Controller和Service。同时,文章还提到了商品分类页面的跳转设置,涉及Vue.js的路由配置。
摘要由CSDN通过智能技术生成


1.将jt项目改造为mybatis-plus后台

因为mybatis-plus的升级并不会影响mybatis的正常使用,所以后续我们的商品模块使用mybatis-plus来进行实现,并且此前用户模块还可以正常使用并不对其产生影响。

1.1 导入jar包

 		<!--导入MP的jar包文件-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>


1.2 编辑POJO ItemCat

@Data
@Accessors(chain = true)
@TableName("item_cat") //关联数据表
public class ItemCat extends BasePojo{

    @TableId(type = IdType.AUTO)//主键自增
    private Integer id;         //定义主键
    private Integer parentId;   //定义父级菜单 开启驼峰规则映射
    private String name;        //分类名称
    private Boolean status;     //分类状态 0 停用 1 正常
    private Integer level;      //商品分类等级  1 2 3
    @TableField(exist = false)  //属性不是表字段
    private List<ItemCat> children;
}

这里我们还要额外使用 @TableField注解来标注children属性,因为其不是数据库表中的字段,无法与表中字段进行自动匹配,使用exist = false标注其不是表字段,到时候MP进行操作时就不会自动去匹配映射此字段,从而避免找不到字段而报错。

1.3 编辑ItemCatMapper接口

public interface ItemCatMapper extends BaseMapper<ItemCat> {

}

ItemCatMapper 接口和此前一样要继承 BaseMapper 接口并添加抽象类ItemCat。

1.4 修改YML文件

#配置端口号
server:
  port: 8091

#管理数据源
spring:
  datasource:
    #高版本驱动使用
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    #设定用户名和密码
    username: root
    password: root

#SpringBoot整合Mybatis-plus
mybatis-plus:
  #指定别名包
  type-aliases-package: com.jt.pojo
  #扫描指定路径下的映射文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true
  # 一二级缓存默认开始 所以可以简化

#打印mysql日志
logging:
  level:
    com.jt.mapper: debug

配置文件中将mybatis变更为mybatis-plus即可。

1.7 Controller及Service

package com.jt.controller;

import com.jt.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/itemCat")
public class ItemCatController {
    
    @Autowired
    private ItemCatService itemCatService;
}

package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemCatServiceImpll implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper;
}

1.6 层级代码结构

在这里插入图片描述

2.商品分类页面跳转

说明: 编辑路由 index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import ItemCat from '../components/items/ItemCat.vue'
//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path: '/home', component: Home, children: [
    {path: '/user', component: User},
     {path: '/itemCat', component: ItemCat}
  ]}
]

页面效果展现
请添加图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值