国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot整合Mybatis-plus實現(xiàn)商品推薦

這篇具有很好參考價值的文章主要介紹了SpringBoot整合Mybatis-plus實現(xiàn)商品推薦。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1. 準備工作

在開始編寫代碼之前,我們需要準備一下環(huán)境:

  • Java 8+
  • IntelliJ IDEA
  • Node.js 和 npm
  • Vue CLI

如果你還沒有安裝Vue CLI,則可以使用以下命令在終端中安裝:

npm install -g @vue/cli

2. 創(chuàng)建Spring Boot項目

首先,我們需要使用Spring Boot創(chuàng)建一個新項目。在IntelliJ IDEA中,選擇“New Project”,然后選擇“Spring Initializr”。

在“New Project”窗口中,選擇“Spring Initializr”,并填寫以下信息:

  • Group:com.example
  • Artifact:spring-boot-mybatis-plus-demo
  • Dependencies:選擇“Web”,“MyBatis-Plus”和“MySQL Driver”

點擊“Next”確認,并在下一個窗口接受默認值。最后,點擊“Finish”完成創(chuàng)建項目。

3. 創(chuàng)建MySQL數(shù)據(jù)庫

我們需要創(chuàng)建一個MySQL數(shù)據(jù)庫來存儲我們的商品數(shù)據(jù)。在這個示例中,我們將創(chuàng)建一個名為“product”的數(shù)據(jù)庫和一個名為“product”表的數(shù)據(jù)表。

首先,打開MySQL控制臺,并運行以下命令來創(chuàng)建數(shù)據(jù)庫:

CREATE DATABASE product;

接下來,我們需要創(chuàng)建一個數(shù)據(jù)表。使用以下命令創(chuàng)建一個名為“product”的數(shù)據(jù)表:

CREATE TABLE product (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(255) DEFAULT NULL,
  price decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在此示例中,我們只需要包含商品名稱和價格兩個字段。如果您要構(gòu)建更實際的使用案例,則可以添加更多的字段。

4. 配置MyBatis-Plus

我們需要添加MyBatis-Plus的依賴,這里我們在pom.xml文件中加入以下代碼:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

在這個示例中,我們沒有使用MyBatis,而是使用MyBatis-Plus。MyBatis-Plus是一個集成了許多MyBatis功能,并且簡化了使用的庫。

接下來,在application.properties中添加以下代碼,配置數(shù)據(jù)庫連接:

spring.datasource.url=jdbc:mysql://localhost:3306/product?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml

這里我們使用了MySQL數(shù)據(jù)庫,如果您的數(shù)據(jù)庫不同,請修改連接URL、用戶名和密碼。

5. 編寫Product實體類

為了讓MyBatis-Plus知道如何映射我們的數(shù)據(jù)庫表,我們需要創(chuàng)建一個Product實體類。在這個示例中,Product實體類包含三個字段:id、name和price。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
    private Long id;
    private String name;
    private BigDecimal price;
}

在這里,我們使用了Lombok注解@Data,可以自動生成getter和setter方法,@NoArgsConstructor和@AllArgsConstructor可以生成無參和全參構(gòu)造器。

6. 創(chuàng)建ProductMapper

接下來,我們將創(chuàng)建一個ProductMapper,用于實現(xiàn)一些操作數(shù)據(jù)庫的方法。在這個示例中,我們將編寫一些包括查詢所有商品、根據(jù)商品名模糊查詢、新增商品、更新商品和刪除商品的方法。

使用MyBatis-Plus,我們只需要繼承BaseMapper就可以實現(xiàn)以上的操作,例如:

public interface ProductMapper extends BaseMapper<Product> {
}

7. 配置Swagger

Swagger是一個流行的API文檔工具,我們可以使用Swagger來記錄和調(diào)試我們的API,方便前端調(diào)用接口。在這個示例中,我們使用Swagger 2.0版本。

添加Swagger的依賴:

<!-- Swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- Swagger UI -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

在Spring Boot的主類上添加@EnableSwagger2注解,開啟Swagger的支持:

@EnableSwagger2
@SpringBootApplication
public class SpringBootMybatisPlusDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisPlusDemoApplication.class, args);
    }
}

在SwaggerConfig.java文件中配置Swagger,示例代碼如下:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.product.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot集成MyBatis-Plus實現(xiàn)商品推薦")
                .description("利用Swagger UI查看和調(diào)試接口")
                .termsOfServiceUrl("http://localhost:8080/")
                .version("1.0")
                .build();
    }
}

8. 編寫商品Controller

我們需要創(chuàng)建一個ProductController類,用于實現(xiàn)與商品相關(guān)的API。在這個示例中,我們將編寫一些包括查詢所有商品、根據(jù)商品名模糊查詢、新增商品、更新商品和刪除商品的API。

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "獲取所有商品列表")
    @GetMapping("/getAll")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    @ApiOperation(value = "根據(jù)商品名模糊查詢")
    @GetMapping("/{name}")
    public List<Product> getProductsByName(@PathVariable String name) {
        return productService.getProductsByName(name);
    }

    @ApiOperation(value = "新增商品")
    @PostMapping("")
    public boolean addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    @ApiOperation(value = "更新商品信息")
    @PutMapping("/{id}")
    public boolean updateProduct(@PathVariable Long id, @RequestBody Product product) {
        return productService.updateProduct(id, product);
    }

    @ApiOperation(value = "刪除商品")
    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable Long id) {
        return productService.deleteProduct(id);
    }
}

在這里,我們使用了Swagger注解@ApiOperation來描述API,方便接口文檔的編寫。

9. 編寫商品Service

我們需要創(chuàng)建一個ProductService類,用于調(diào)用MyBatis-Plus操作數(shù)據(jù)庫。在這個示例中,我們實現(xiàn)了從數(shù)據(jù)庫中查詢所有商品、根據(jù)商品名模糊查詢、新增商品、更新商品和刪除商品的方法。

@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;

    public List<Product> getAllProducts() {
        return productMapper.selectList(null);
    }

    public List<Product> getProductsByName(String name) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return productMapper.selectList(queryWrapper);
    }

    public boolean addProduct(Product product) {
        return productMapper.insert(product) > 0;
    }

    public boolean updateProduct(Long id, Product product) {
        product.setId(id);
        return productMapper.updateById(product) > 0;
    }

    public boolean deleteProduct(Long id) {
        return productMapper.deleteById(id) > 0;
    }
}

10. 整合Vue和Element-UI

接下來,我們將使用Vue結(jié)合Element-UI的組件進行調(diào)用后端接口。首先,我們使用Vue CLI創(chuàng)建一個新的Vue項目:

vue create product-vue

然后在命令行中運行以下命令來安裝Element-UI和Axios:

npm install element-ui --save
npm install axios

在完成安裝后,我們可以開始創(chuàng)建Vue組件。在src/components目錄下創(chuàng)建一個新文件ProductList.vue,代碼如下:

<template>
  <div>
    <el-input v-model="searchName" placeholder="請輸入搜索關(guān)鍵字" class="search-input"></el-input>
    <el-button type="primary" @click="searchProducts">搜索</el-button>
    <el-button type="default" class="add-btn" @click="showAddDialog=true">新增商品</el-button>
    <el-dialog title="新增商品" :visible.sync="showAddDialog">
      <el-form :model="newProduct" label-position="right">
        <el-form-item label="商品名稱">
          <el-input v-model="newProduct.name"></el-input>
        </el-form-item>
        <el-form-item label="價格">
          <el-input v-model="newProduct.price" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addProduct">確定</el-button>
        <el-button @click="showAddDialog=false">取消</el-button>
      </div>
    </el-dialog>
    <el-table :data="products" stripe style="width: 100%">
      <el-table-column type="index" width="50" label="序號"></el-table-column>
      <el-table-column prop="name" label="商品名稱"></el-table-column>
      <el-table-column prop="price" label="價格"></el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="editProduct(scope.row)">編輯</el-button>
          <el-button size="small" type="danger" @click="deleteProduct(scope.row)">刪除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="編輯商品" :visible.sync="showEditDialog">
      <el-form :model="currentProduct" label-position="right">
        <el-form-item label="商品名稱">
          <el-input v-model="currentProduct.name"></el-input>
        </el-form-item>
        <el-form-item label="價格">
          <el-input v-model="currentProduct.price" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateProduct">確定</el-button>
        <el-button @click="showEditDialog=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { Message, Dialog, Form, FormItem, Input, Button, Table, TableColumn } 
  from "element-ui";

export default {
  name: "ProductList",
  components: { ElDialog: Dialog, ElForm: Form, ElFormItem: FormItem, 
    ElInput: Input, ElButton: Button, ElTable: Table, ElTableColumn: TableColumn },
  data() {
    return {
      products: [],
      searchName: '',
      showAddDialog: false,
      newProduct: { name: '', price: null },
      currentProduct: null,
      showEditDialog: false,
      editProductIndex: -1
    }
  },
  created() {
    this.loadProducts();
  },
  methods: {
    loadProducts() {
      axios.get('/product/getAll').then((response) => {
        this.products = response.data;
      }).catch((error) => {
        console.error(error);
        Message.error('加載商品列表失敗');
      });
    },
    searchProducts() {
      axios.get('/product/' + this.searchName).then((response) => {
        this.products = response.data;
      }).catch((error) => {
        console.error(error);
        Message.error('搜索商品失敗');
      });
    },
    addProduct() {
      axios.post('/product', this.newProduct).then((response) => {
        this.showAddDialog = false;
        this.loadProducts();
        Message.success('新增商品成功');
      }).catch((error) => {
        console.error(error);
        Message.error('新增商品失敗');
      });
    },
    editProduct(product) {
      this.currentProduct = Object.assign({}, product);
      this.editProductIndex = this.products.indexOf(product);
      this.showEditDialog = true;
    },
    updateProduct() {
      axios.put('/product/' + this.currentProduct.id, this.currentProduct).then((response) => {
        this.showEditDialog = false;
        this.loadProducts();
        Message.success('更新商品成功');
      }).catch((error) => {
        console.error(error);
        Message.error('更新商品失敗');
      });
    },
    deleteProduct(product) {
      this.$confirm('確定要刪除該商品嗎?', '提示', {
        confirmButtonText: '確定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete('/product/' + product.id).then((response) => {
          Message.success('刪除成功');
          this.loadProducts();
        }).catch((error) => {
          console.error(error);
          Message.error('刪除商品失敗');
        });
      }).catch(() => {
        Message.info('已取消刪除');
      });
    }
  }
};
</script>

<style scoped>
.search-input {
  width: 300px;
  margin-right: 10px;
}
.add-btn {
  margin: 0 10px;
}
</style>

在這里,我們使用了Element-UI的組件,包括Input、Button、Table、Dialog和Form等,用于實現(xiàn)前端的邏輯。同時,我們使用了Axios來調(diào)用后端接口,實現(xiàn)數(shù)據(jù)的讀寫操作。

11. 運行程序

在完成以上工作后,我們就可以運行程序來測試它是否正常工作了。首先,在終端中進入Spring Boot項目目錄,并運行以下命令:

mvn spring-boot:run

然后,在另一個終端中進入Vue項目目錄,并運行以下命令:

npm run serve

現(xiàn)在,在瀏覽器中打開http://localhost:8081(Vue項目的默認端口),即可訪問我們的頁面。

12. 結(jié)語

在本文中,我們介紹了如何使用Spring Boot整合MyBatis-Plus實現(xiàn)商品推薦,并使用Vue結(jié)合Element-UI的組件進行調(diào)用接口。希望這篇文章能夠?qū)δ兴鶐椭x謝閱讀!文章來源地址http://www.zghlxwxcb.cn/news/detail-430134.html

到了這里,關(guān)于SpringBoot整合Mybatis-plus實現(xiàn)商品推薦的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • SpringBoot整合MyBatis-Plus,趕緊整過來!

    提示:以下是本篇文章正文內(nèi)容 MyBatis-Plus官網(wǎng)介紹:MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。 MyBatis-Plus封裝了單表的crud操作,減少基礎(chǔ)代碼編寫,提高開發(fā)效率。 支持自

    2024年02月06日
    瀏覽(17)
  • springboot3.2 整合 mybatis-plus

    springboot3.2 整合 mybatis-plus

    springboot3.2 正式發(fā)布了 迫不及待地的感受了一下 結(jié)果在整個mybatis-plus 的時候遇到了如下報錯 主要是由于 mybatis-plus 中 mybatis 的整合包版本不夠?qū)е碌?排除 mybatis-plus 中自帶的 mybatis 整合包,單獨引入即可 修改依賴后正常

    2024年02月04日
    瀏覽(25)
  • Springboot 整合Mytbatis與Mybatis-Plus

    Springboot 整合Mytbatis與Mybatis-Plus

    目錄 1. springboot整合mybatis?? ?1.1 添加pom.xml依賴 ?1.2 新建jdbc.properties 文件添加以下內(nèi)容 ?1.3 新建generatorConfig.xml 文件添加以下內(nèi)容 (自動生成代碼類)? ?1.4 修改application.properties 文件 添加以下內(nèi)容 ?1.5 修改主類MapperScan ?1.6 編寫接口實現(xiàn)類進行測試? 2. springboot整合mybatis-p

    2024年02月06日
    瀏覽(23)
  • SpringBoot整合JUnit--MyBatis--MyBatis-Plus--Druid

    SpringBoot整合JUnit--MyBatis--MyBatis-Plus--Druid

    文章轉(zhuǎn)自黑馬程序員SpringBoot學(xué)習(xí)筆記,學(xué)習(xí)網(wǎng)址:黑馬程序員SpringBoot2教程 1.整合JUnit ? SpringBoot技術(shù)的定位用于簡化開發(fā),再具體點是簡化Spring程序的開發(fā)。所以在整合任意技術(shù)的時候,如果你想直觀感觸到簡化的效果,你必須先知道使用非SpringBoot技術(shù)時對應(yīng)的整合是如何做

    2023年04月23日
    瀏覽(26)
  • SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)

    SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)

    1.需求分析 2.數(shù)據(jù)庫表設(shè)計 3.數(shù)據(jù)庫環(huán)境配置 1.新建maven項目 2.pom.xml 引入依賴 3.application.yml 配置數(shù)據(jù)源 數(shù)據(jù)庫名 用戶名 密碼 驅(qū)動是mysql8的(因為上面使用了版本仲裁) 4.Application.java 編寫啟動類 5.測試 6.配置類切換druid數(shù)據(jù)源 7.測試數(shù)據(jù)源是否成功切換 4.Mybatis基礎(chǔ)配置 1

    2024年03月20日
    瀏覽(30)
  • Springboot3整合Mybatis-plus3.5.3報錯

    Springboot3整合Mybatis-plus3.5.3報錯

    ?作者簡介:大家好,我是Leo,熱愛Java后端開發(fā)者,一個想要與大家共同進步的男人???? ??個人主頁:Leo的博客 ??當(dāng)前專欄: 報錯以及Bug ?特色專欄: MySQL學(xué)習(xí) ??本文內(nèi)容:記錄一次Docker與Redis沖突 ???個人小站 :個人博客,歡迎大家訪問 ??個人知識庫: 知識庫,

    2024年02月05日
    瀏覽(20)
  • SpringBoot整合Mybatis-Plus、Druid配置多數(shù)據(jù)源

    SpringBoot整合Mybatis-Plus、Druid配置多數(shù)據(jù)源

    目錄 1.初始化項目 1.1.初始化工程 1.2.添加依賴 1.3.配置yml文件 1.4.Spring Boot 啟動類中添加?@MapperScan?注解,掃描 Mapper 文件夾 1.5.配置使用數(shù)據(jù)源 1.5.1.注解方式 1.5.2.基于AOP手動實現(xiàn)多數(shù)據(jù)源原生的方式 2.結(jié)果展示 Mybatis-Plus:簡介 | MyBatis-Plus (baomidou.com) 在正式開始之前,先初始

    2024年02月11日
    瀏覽(24)
  • springboot整合mybatis-plus的sql輸出到日志文件上

    springboot整合mybatis-plus的sql輸出到日志文件上

    springboot整合mybatis-plus的sql輸出到日志文件上 在平時的日常開發(fā)中,我們希望sql打印在控制臺上,只要如下配置即可 但是在生產(chǎn)中如果希望sql輸出到日志文件上,有幾種方式可以實現(xiàn),下面我就用項目中常用的兩種方式(不引入第三方依賴) 一、修改yml文件配置即可 缺點:

    2024年02月01日
    瀏覽(26)
  • java springboot整合MyBatis-Plus 多用點Plus支持一下國人開發(fā)的東西吧

    java springboot整合MyBatis-Plus 多用點Plus支持一下國人開發(fā)的東西吧

    文章java springboot整合MyBatis做數(shù)據(jù)庫查詢操作講述了boot項目整合MyBatis的操作方法 但現(xiàn)在就還有一個 MyBatis-Plus Plus是國內(nèi)整合的一個技術(shù) 國內(nèi)的很多人會喜歡用 特別是一些中小型公司 他們用著會比較舒服 好 然后我們打開idea 創(chuàng)建一個項目 選擇 Spring Initializr 工程 調(diào)一下項目

    2024年02月09日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包