溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Springboot中apollo

發(fā)布時間:2020-07-17 11:33:49 來源:億速云 閱讀:223 作者:小豬 欄目:編程語言

小編這次要給大家分享的是詳解Springboot中apollo,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

添加配置

詳解Springboot中apollo

spring-boot中如何使用

pom.xml中添加配置

<dependency>
  <groupId>com.ctrip.framework.apollo</groupId>
  <artifactId>apollo-client</artifactId>
  <version>1.1.0</version>
</dependency>

配置文件中添加apollo地址

app:
 id: komiles
apollo:
 meta: http://106.54.227.205:8080
 bootstrap:
  enabled: true
  namespaces: application

啟動類中添加代碼

添加@EnableApolloConfig注解

package com.example.apollodemo;
 
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@EnableApolloConfig
@MapperScan("com.example.apollodemo.mapper")
public class ApolloDemoApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(ApolloDemoApplication.class, args);
    System.out.println("============ apollo demo application end =============");
  }
}

controller類新增文件

ApolloController.java

package com.example.apollodemo.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author komiles@163.com
 * @date 2020-05-06 17:28
 */
@RestController
@RequestMapping("/apollo")
public class ApolloController {
 
  @Value("${name}")
  private String name;
 
  @GetMapping("/name")
  public String name()
  {
    return name;
  }
}

可以讀取到配置為kongming.

數(shù)據(jù)庫配置如何使用?

同理,generatorConfig.xml中也可以讀取數(shù)據(jù)庫配置

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE generatorConfiguration
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <context id="mysqlTables" targetRuntime="MyBatis3">
    <commentGenerator>
      <property name="suppressDate" value="false"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--目標數(shù)據(jù)庫配置-->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="${spring.datasource.url}"
        userId="${spring.datasource.username}"
        password="${spring.datasource.password}" />
    <!-- 指定生成的類型為java類型,避免數(shù)據(jù)庫中number等類型字段 -->
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!-- 生成model模型,對應的包,存放位置可以指定具體的路徑,如/ProjectName/src,也可以使用MAVEN來自動生成 -->
    <javaModelGenerator targetPackage="com.example.apollodemo.dao" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
      <property name="trimStrings" value="true"/>
      <property name="immutable" value="false"/>
    </javaModelGenerator>
    <!--對應的xml mapper文件 -->

    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
      <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>
    <!-- 對應的dao接口 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.apollodemo.mapper" targetProject="src/main/java">
      <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>
    <!--定義需要操作的表及對應的DTO名稱-->
    <table tableName="t_user" domainObjectName="User"/>
  </context>
</generatorConfiguration>

看完這篇關(guān)于詳解Springboot中apollo的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI