Springboot整合mybatis-plus

第一步:pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.vn</groupId>
    <artifactId>vnmybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>vnmybatis</name>
    <description>vnmybatis</description>

    <properties>
        <!-- ali 连接池 -->
        <druid.version>1.1.9</druid.version>
        <java.version>1.8</java.version>

        <!-- mybatis -->
        <mybatis-plus-boot-starter.version>3.0-RELEASE</mybatis-plus-boot-starter.version>
        <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
        <mybatis.ehcache.version>1.1.0</mybatis.ehcache.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- ali 连接池依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- hutool 工具包 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.20</version>
        </dependency>

        <!-- mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>${mybatis.ehcache.version}</version>
        </dependency>

        <!-- mysql 依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <!--
       项目打包时会将java目录中的*.xml文件也进行打包

       此处必须添加项目打包,不然会报错:Invalid bound statement (not found): XXXXXXX
    -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>

 项目打包时会将java目录中的*.xml文件也进行打包:作用见下图

第二部:配置文件

server:
  port: 8071
# 数据源
spring:
  application:
    name: stock-seata
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.43.197:3306/seata_stock?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.43.197:8848
        username: nacos
        password: nacos
        namespace: public
    alibaba:
      seata:
        tx-service-group: my_test_tx_group
# mapper文件位置
mybatis-plus:
  mapper-locations: classpath:com/stock/seata/mapper/xml/*.xml
  type-aliases-package: com.stock.seata.entity
  configuration:
    map-underscore-to-camel-case: true        #column-underline: true 会自动将下划线格式的表字段,转换为以驼峰格式命名的属性

第三步代码:

Controller 层:

package com.stock.seata.controller;

import com.stock.seata.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    StockService stockService;

    @DeleteMapping("/reduct")
    public String reduct(@RequestParam(value="productId") Integer productId){
        stockService.reduct(productId);
        return "扣减库存";
    }

}

Service层:

package com.stock.seata.service;


public interface StockService extends IService<Stock>{

    void reduct(Integer productId);
}



package com.stock.seata.service.impl;

import com.stock.seata.mapper.StockMapper;
import com.stock.seata.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/***
 * @author vn
 *
 *  service实现类   继承mp提供通用的service基类
 *  ServiceImpl<StockMapper, Stock>
 *      2个泛型 1.StockMapper  Mapper接口
 *              2.Stock  对应Pojo
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper,Stock> implements StockService {

    @Autowired
    StockMapper stockMapper;

    @Override
    public void reduct(Integer productId) {
        try{
            System.out.println("更新商品:"+productId);
            stockMapper.reduct(productId);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

Mapper层:

package com.stock.seata.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.stock.seata.entity.Stock;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StockMapper extends BaseMapper<Stock> {

    int deleteByPrimaryKey(Integer id);

    int insert(Stock record);

    Stock selectByPrimaryKey(Integer id);

    List<Stock> selectAll();

    int updateByPrimaryKey(Stock record);

    void reduct(@Param("productId") Integer productId);
}

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.stock.seata.mapper.StockMapper">
  <resultMap id="BaseResultMap" type="com.stock.seata.entity.Stock">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="product_id" jdbcType="INTEGER" property="productId" />
    <result column="count" jdbcType="INTEGER" property="count" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from stock_tbl
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.stock.seata.entity.Stock">
    insert into stock_tbl (id, product_id, count
      )
    values (#{id,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{count,jdbcType=INTEGER}
      )
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.stock.seata.entity.Stock">
    update stock_tbl
    set product_id = #{productId,jdbcType=INTEGER},
      count = #{count,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id, product_id, count
    from stock_tbl
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="selectAll" resultMap="BaseResultMap">
    select id, product_id, count
    from stock_tbl
  </select>

  <update id="reduct">
    update stock_tbl set `count`=`count`-1
    where product_id=#{productId}
  </update>
</mapper>

 

posted @ 2022-03-06 17:31  VNone  阅读(128)  评论(0)    收藏  举报