SpringBoot基礎(chǔ)配置

SpringBoot常用配置

Mybatis-generatorConfig.xml

具體參數(shù)研乒,包名根據(jù)項目自行修改

<?xml version="1.0" encoding="UTF-8"?>
<!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="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的注釋 true:是 : true:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數(shù)據(jù)庫鏈接地址賬號密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/database?characterEncoding=utf8"
                        userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="true"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <javaModelGenerator targetPackage="com.text.demo.service.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.text.demo.generate.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類存放位置-->
        <!-- 客戶端代碼粪滤,生成易于使用的針對Model對象和XML配置文件 的代碼
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對象
                type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
        -->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.text.demo.dao"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--用戶相關(guān)-->
        <table tableName="user" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"/>
        <table tableName="carousel" domainObjectName="Carousel" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"/>
    </context>
</generatorConfiguration>

Maven添加生成實體類邪财、接口xml(在pom.xml添加)

<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.3.5</version>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.39</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <id>Generate MyBatis Artifacts</id>
      <phase>deploy</phase>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <!--允許移動生成的文件 -->
    <verbose>true</verbose>
    <!-- 是否覆蓋 -->
    <overwrite>true</overwrite>
    <!-- 自動生成的配置 -->
    <configurationFile>
      src/main/resources/generatorConfig.xml
    </configurationFile>
  </configuration>
</plugin>

添加Api接口請求網(wǎng)頁(swagger)

添加swagger依賴

<!-- 生成api文檔-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

swagger配置類

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "enabled", prefix = "swagger", havingValue = "true", matchIfMissing = false)
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.star.appapi.controller"))    //這里采用包掃描的方式來確定要顯示的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger標(biāo)題")
                .description("Api文檔")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/database?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  http:
    encoding:
      force: true
      charset: UTF-8
      enabled: true
server:
  tomcat:
    uri-encoding: UTF-8

重寫SpringApplication啟動類

@MapperScan(basePackages = {"com.包名.generator.dao", "com.包名.dao"})
@SpringBootApplication
public class LocalApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        JEditorPane response = new JEditorPane();
        response.setContentType("application/json;charset=utf-8");
        new SpringApplicationBuilder(LocalApplication.class).run(args);
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市宛瞄,隨后出現(xiàn)的幾起案子逊拍,更是在濱河造成了極大的恐慌寞忿,老刑警劉巖感昼,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異罐脊,居然都是意外死亡定嗓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進(jìn)店門萍桌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宵溅,“玉大人,你說我怎么就攤上這事上炎∈崖撸” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵藕施,是天一觀的道長寇损。 經(jīng)常有香客問我,道長裳食,這世上最難降的妖魔是什么矛市? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮诲祸,結(jié)果婚禮上浊吏,老公的妹妹穿的比我還像新娘而昨。我一直安慰自己,他們只是感情好找田,可當(dāng)我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布歌憨。 她就那樣靜靜地躺著,像睡著了一般墩衙。 火紅的嫁衣襯著肌膚如雪务嫡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天漆改,我揣著相機與錄音心铃,去河邊找鬼。 笑死籽懦,一個胖子當(dāng)著我的面吹牛于个,可吹牛的內(nèi)容都是我干的氛魁。 我是一名探鬼主播暮顺,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼秀存!你這毒婦竟也來了捶码?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤或链,失蹤者是張志新(化名)和其女友劉穎惫恼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體澳盐,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡祈纯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了叼耙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腕窥。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖筛婉,靈堂內(nèi)的尸體忽然破棺而出簇爆,到底是詐尸還是另有隱情,我是刑警寧澤爽撒,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布入蛆,位于F島的核電站,受9級特大地震影響硕勿,放射性物質(zhì)發(fā)生泄漏哨毁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一源武、第九天 我趴在偏房一處隱蔽的房頂上張望挑庶。 院中可真熱鬧言秸,春花似錦、人聲如沸迎捺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至叛买,卻和暖如春率挣,著一層夾襖步出監(jiān)牢的瞬間椒功,已是汗流浹背动漾。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工证九, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人呀页。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓渴逻,卻偏偏與公主長得像雪位,于是被迫代替她去往敵國和親雹洗。 傳聞我的和親對象是個殘疾皇子时肿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,060評論 2 355