/ **
1、リソース/ application.propertiesプロパティファイルという名前の書き込み---->という名前の書き込み属性クラス、クラス:
** /
ファイル:application.properties
jdbc.driverClassName =はcom.mysql.jdbc.Driver
jdbc.url = JDBCます。mysql://192.168.28.128
jdbc.username = ORCL
jdbc.password = 123456
文件:JdbcProperties
パッケージcom.hope.config 。
輸入ロンボク。データ;
輸入org.springframework.boot.context.properties。ConfigurationProperties ;
/ **
* @author newcityman
*使用ロンボク插件
* @date 2019年12月16日-夜12時59分
* /
@ConfigurationProperties(プレフィックス= "JDBC")
@Data
パブリッククラスJdbcProperties {
ストリングdriverClassName 。
文字列のURL 。
文字列のユーザ名。
文字列のパスワード。
}
パッケージcom.hope.config 。
輸入com.alibaba.druid.pool.DruidDataSource 。
輸入org.springframework.beans.factory.annotation。バリュー;
輸入org.springframework.boot.context.properties。EnableConfigurationProperties ;
輸入org.springframework.context.annotation。豆;
輸入org.springframework.context.annotation。設定;
輸入org.springframework.context.annotation。PropertySource ;
インポートするjavax.sql.DataSource ;
/ **
* @author newcityman
* @Bean把类的返回值注入到春核心容器便于调用
* @date2019年12月15日-午後11時52分
* /
@Configuration
/*@PropertySource("classpath:application.properties")*/
@EnableConfigurationProperties(JdbcProperties クラス)
パブリッククラスJdbcConfig { @Bean パブリックデータソースデータソース(JdbcProperties JP){ DruidDataSourceデータソースは、= 新しいDruidDataSourceを(); dataSource.setDriverClassName(jp.getDriverClassName()) 。dataSource.setUrl(jp.getUrl()) 。dataSource.setUsername(jp.getUsername()) 。dataSource.setPassword(jp.getPassword()) 。DataSourceを返します。} }
パッケージcom.hope 。
輸入org.springframework.boot.SpringApplication 。
輸入org.springframework.boot.autoconfigure。SpringBootApplication ;
輸入org.springframework.web.bind.annotation。RestController ;
/ **
* @author newcityman
* @date 2019年12月15日-夜11時09分
* /
@SpringBootApplication
パブリッククラスBootDemoApplication {
公共の静的な無効メイン(文字列[]引数){
SpringApplication。実行(BootDemoApplication。クラス、引数);
}
}
パッケージcom.hope.web 。
輸入org.springframework.beans.factory.annotation。Autowired ;
輸入org.springframework.web.bind.annotation。GetMapping ;
輸入org.springframework.web.bind.annotation。ResponseBody ;
輸入org.springframework.web.bind.annotation。RestController ;
インポートするjavax.sql.DataSource ;
/ **
* @author newcityman
* @date 2019年12月15日- 23:28
* /
@RestController
パブリッククラスHelloController {
@Autowired プライベートデータソースのdataSource 。(@GetMapping
"ハロー")
@ResponseBody パブリック文字列のHello(){ 戻り"ハローzmy、你好" 。} }