风萧萧兮易水寒

Spring Cloud 整合mybatis-plus (五)

前言

其实这个整合,和spring 或者 spring boot 整合一样的
其实都是一键生成的,自己把模板配好就行,从controllerservice,dao,实体,增删改查
我只是拷贝过来几个测试这个项目
之前刚接触mybatis-plus的时候也写过自动生成的文章

引入jar

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.7.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-generator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

修改配置文件

mysql数据库8.0以上的,如果你的是5.+的版本,自己该数据源即可

spring:
  application:
    name: order
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/gmaya?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: admin
#mybatis-plus
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  typeAliasesPackage: com.gmaya.order.entity
  global-config:
    db-config:
      id-type: AUTO
      field-strategy: NOT_NULL
      column-underline: true
      logic-delete-value: 1 # 数据库删除字段,1代表删除
      logic-not-delete-value: 0 # 0代表没删除
    banner: false

新增配置类

package com.gmaya.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

/**
 * mybatis-plus配置
 *
 * @author GMaya
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    };

    /**
     * 控制逻辑删除
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    };

    /**
     * 控制版本号
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    };

};

实体类

package com.gmaya.order.entity;

import java.io.Serializable;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;

import lombok.Data;

/**
 * DO
 *
 * @author GMaya
 */
@Data
@TableName("tb_user")
public class UserEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 用户id
     */
    @TableId(value = "id")
    private Integer id;

    /**
     * 用户名称
     */
    private String name;

    /**
     * 用户年龄
     */
    private Integer age;

    /**
     * 是否删除。0未删除,1删除
     */
    @TableLogic
    private Integer isDtl;

    /**
     * 版本号
     */
    @Version
    private Integer version;

};

dao

package com.gmaya.order.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gmaya.order.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

@Mapper
public interface UserDao extends BaseMapper<UserEntity> {

};

service

package com.gmaya.order.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gmaya.order.entity.UserEntity;

/**
 * 服务类
 * @author GMaya
 */
public interface UserService extends IService<UserEntity> {

    int add(JSONObject jsonObject);
};

impl

package com.gmaya.order.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmaya.order.dao.UserDao;
import com.gmaya.order.entity.UserEntity;
import com.gmaya.order.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author GMaya
 */
@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public int add(JSONObject jsonObject) {
        log.info("log...订单开始......");
        UserEntity userEntity = new UserEntity();
        userEntity.setAge(1);
        userEntity.setIsDtl(0);
        userEntity.setVersion(1);
        userEntity.setName("订单模拟");
        int insert = baseMapper.insert(userEntity);
        log.info("log...订单结束......");
        return insert;
    };


};
坚持原创技术分享,您的支持将鼓励我继续创作!