spring-cloud-alibaba +nacos+fegin +mybatisplus整合

1.实体类工程 pojo

实体类工程pom文件

<parent>
        <artifactId>kfl-cloud</artifactId>
        <groupId>com.kfl</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <version>${pojo.version}</version>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kfl</groupId>
    <artifactId>kfl-common-pojo</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
    </dependencies>

实体类

package com.kfl.pojo.user;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;

import java.util.Date;

@TableName("KFL_USER_INFO")
@Data
public class UserInfo extends Model<UserInfo> {
    /** 主键 */
    @TableId
    private Long id ;
    /** 手机号 */
    private String phoneNum ;
    /** 密码 */
    private String pwd ;
    /** 昵称 */
    private String name ;
    /** 创建时间 */
    private Date createdTime ;
    /** 更新时间 */
    private Date updatedTime ;
    /** 身份证号 */
    private String idNum ;
    /** 状态 */
    private Integer status ;
    /** 用户类型 */
    private Integer fType ;
    /** 用户来源 */
    private Integer source ;
    /** 用户头像 */
    private String headImg ;
}

2.mapper工程

<parent>
        <artifactId>kfl-cloud</artifactId>
        <groupId>com.kfl</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kfl</groupId>
    <artifactId>kfl-common-mapper</artifactId>
    <version>${mappper.version}</version>
    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
        <dependency>
            <groupId>com.kfl</groupId>
            <artifactId>kfl-common-pojo</artifactId>
            <version>${pojo.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
    </dependencies>

mapper类代码

public interface UserInfoMapper extends BaseMapper<UserInfo> {
}

以上这些都没有太多好说的重点来了

3.service接口工程

接口类

package com.kfl.service.api.user;

import com.kfl.pojo.user.UserInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "service-user")
public interface IUserInfoService {

    @GetMapping("/userinfo/{id}")
    public UserInfo getUser(@PathVariable("id") Long id);
}

注意这里并没有集成IService类,如果加了会包以下错误
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com…IUserInfoService’: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method remove not annotated with HTTP method type (ex. GET, POST)

4.服务提供者工程

package com.kfl.service.user;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kfl.mapper.user.UserInfoMapper;
import com.kfl.pojo.user.UserInfo;
import com.kfl.service.api.user.IUserInfoService;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService, IService<UserInfo> {

    @Override
    public UserInfo getUser(Long id) {
        return super.getById(id);
    }
}

实现类工程中只要继承IUserInfoService ,IService两个类就可以解决上述异常

启动类代码

@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.kfl.mapper.user")
@EnableFeignClients
public class ServiceUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceUserApplication.class, args);
    }
}

配置文件

server:
  port: 8101
spring:
  application:
    name: service-user
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.2.253:3306/kfl?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
  mybatis-plus:
    type-aliases-package: com.kfl.pojo
    configuration:
      map-underscore-to-camel-case: true

你可能感兴趣的:(spring-cloud-alibaba +nacos+fegin +mybatisplus整合)