SpringCloud集成MyBatis

首先创建Maven顶级项目,再创建好Eureka服务注册中心后,创建Eureka客户端

一:引入依赖

<dependencies>
    <!--引入Eureka的客户端依赖,不包含springboot-start-web,所以要单独引入-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--引入springboot-start-web依赖,必须引入-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- Mybatis依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

其实关于MyBatis的jar包就引入了两个

二:application.yml配置数据库的四大金刚和MyBatis的公共包

spring:
  application:
    name: MyBatisServer #服务的应用名称
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///springcloudmybatis?serverTimezone=GMT
    username: root
    password: root
mybatis:
  type-aliases-package: cn.ybl.pojo

server:
  port: 10002 # user服务端口号

eureka:
  client:
    serviceUrl: # Eureka客户端配置,指向注册中心地址
      defaultZone: http://localhost:10001/eureka/
    registry-fetch-interval-seconds: 10  # 修改从客户端拉取信息的时间10s
  instance:
    prefer-ip-address: true #使用ip+端口号进行注册
    instance-id: MyBatisServer-10002 #实例id,一般为服务名+端口号

如此便完成了SpringCloud与MyBatis的集成,直接像写Springboot一样去写mapper层service层controller层pojo层以及xml映射即可

posted @ 2022-08-20 15:40  yyybl  阅读(105)  评论(0)    收藏  举报