[Springboot 2019年10月23日]カスタムspringbootの自動設定

1. springboot 3キーカテゴリを作成します。

1.1プロパティは、クラスをお読みください。設定ファイルから設定情報を読み取るため

パッケージcom.shijt.springbootdemo.jdbc。

輸入org.springframework.boot.context.properties.ConfigurationProperties。

@ConfigurationProperties(接頭辞 =「mysql.jdbc」パブリック クラスJDBCRead {
     プライベート文字列ドライバ。
    プライベート文字列のURL。
    プライベート文字列のユーザ名。
    プライベート文字列のパスワード。

    パブリック文字列getDriver(){
         リターンドライバ。
    } 

    公共 ボイドsetDriver(文字列ドライバ){
         この .driver = ドライバ。
    } 

    パブリックストリングのgetURL(){
         戻りURL。
    } 

    公共 ボイドてsetURL(文字列のURL){
         このた.url = URL。
    } 

    パブリック文字列getUsername(){
         戻りユーザ名; 
    } 

    公共 ボイドsetUsername(文字列名){
         この .username = ユーザ名; 
    } 

    パブリック文字列getPasswordに(){
         戻りパスワード。
    } 

    公共 ボイドするsetPassword(文字列のパスワード){
         このみましょう。パスワード= パスワード。
    } 
}

1.2コアイベントクラス

パッケージcom.shijt.springbootdemo.jdbc。

インポート java.sqlの。* ; 

パブリック クラスJDBCUtils {
     プライベート文字列ドライバと、
    プライベート文字列のURL。
    プライベート文字列のユーザ名。
    プライベート文字列のパスワード。

    公共 INT testJdbc(){ 

        接続CON = NULL ;
        試す{
             // 加载のMySql的驱动类 
            Class.forNameの( "はcom.mysql.jdbc.Driver" ); 
            CON = したDriverManager.getConnection(URL、ユーザ名、パスワード); 
       //は唯一のテストを行い、死んで直接SQLを書きます 文字列のSQL
= "Smbms_user` INSERT INTO`(` id`、userCode``、 `username`)VALUES( '001'、 '001'、 'zhangsan')" ; のPreparedStatementてpstmt = con.prepareStatement(SQL); INT行= てpstmt .executeUpdate(); pstmt.close(); 戻り行; } キャッチ(ClassNotFoundExceptionがE){ するSystem.out.println( "クラスは、ドライバがロードに失敗し、ドライバが見つかりません!" ); e.printStackTrace(); } キャッチ(のSQLException SE){ するSystem.out.println( "失敗したデータベース接続!" ); SE。printStackTrace(); } キャッチ(例外E){ e.printStackTrace(); } 最後に{ 場合(!CON = ヌル){ 試み{ con.close(); } キャッチ(のSQLException E){ e.printStackTrace(); } } } 戻り 0 } パブリック文字列getDriver(){ リターンドライバ。 } 公共ボイドsetDriver(文字列ドライバ){ この .driver = ドライバ。 } パブリック文字列のgetURL(){ 戻りURL。 } 公共 ボイドてsetURL(文字列のURL){ このた.url = URL。 } パブリック文字列getUsername(){ 戻りユーザ名; } 公共 ボイドsetUsername(文字列名){ この .username = ユーザ名; } パブリック文字列getPasswordに(){ 戻りパスワード。 } 公共 ボイドするsetPassword(文字列のパスワード){ このみましょう。パスワード=パスワード; } }

1.3統合クラス:コアイベントクラスに割り当てられたクラスの属性を読み取るためのプロパティ

パッケージcom.shijt.springbootdemo.jdbc。

輸入org.springframework.boot.autoconfigure.condition.ConditionalOnClass。
輸入org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean。
輸入org.springframework.boot.autoconfigure.condition.ConditionalOnProperty。
輸入org.springframework.boot.context.properties.EnableConfigurationProperties。
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.Configuration。

輸入javax.annotation.Resource。

@Configuration 
@EnableConfigurationProperties({JDBCRead。クラス})
@ConditionalOnClass({JDBCUtils。クラス})
@ConditionalOnProperty(接頭辞 = "mysql.jdbc"、値= "有効"、= matchIfMissing 真のパブリック クラスJDBCAutoConfigration { 
    @Resource 
    プライベートJDBCRead jdbcRead; 

    @Bean 
    。@ConditionalOnMissingBean(JDBCUtils { クラス} )
     パブリックJDBCUtilsのgetJDBCUtils(){
         // 時間で実行される方法を決定する(むしろときにコールJDBCUtilsより、springbootから始まる) 
        のSystem.out.println( "JDBCAutoConfigrationのgetJDBCUtilsにステップ----- -----" )。
        jdbcUtils JDBCUtils = 新新JDBCUtils();
        jdbcUtils.setDriver(jdbcRead.getDriver())。
        jdbcUtils.setUrl(jdbcRead.getUrl())。
        jdbcUtils.setUsername(jdbcRead.getUsername())。
        jdbcUtils.setPassword(jdbcRead.getPassword())。
        返すjdbcUtilsを。
    } 
}

resourcesディレクトリのフォルダ2. META-INFファイル、ファイルspring.factoriesを作成します

org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ 
com.shijt.springbootdemo.jdbc.JDBCAutoConfigration

統合されたタイプが書かれた自動設定に追加されます

3.ライトコントローラクラス、JDBCUtilsを呼び出します

パッケージcom.shijt.springbootdemo.controller。

輸入com.shijt.springbootdemo.jdbc.JDBCUtils。
輸入org.springframework.stereotype.Controller。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.ResponseBody。

輸入javax.annotation.Resource。

@Controller 
パブリック クラス{UserControllerで
    @Resource 
    プライベートJDBCUtilsのjdbcUtils。

    (@RequestMapping "/ testJdbcを" 
    @ResponseBody 
    公共 INT testJdbc(){
         戻りjdbcUtils.testJdbcを();
    } 

}

appliaction.ymlの構成データベース接続情報

mysqlの:
  JDBC:
    ドライバー:はcom.mysql.jdbc.Driverの
    URLます。jdbc:mysqlの:// localhostを:3306 / smbms useUnicode =本当?&characterEncoding = UTF-8 
    のユーザー名:ルート
    パスワード:123456

4. [スタート] springboot、テスト

 

 

 

おすすめ

転載: www.cnblogs.com/shijt/p/11725627.html