commit d9f560097c2844a3b8677c62ca64996f56c08eed Author: F嘉阳 Date: Sat Mar 30 09:35:21 2019 +0800 官方文档指南启动成功 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..153c933 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +HELP.md +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b0e450e --- /dev/null +++ b/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.3.RELEASE + + + top.fjy8018 + jpadatasource + 0.0.1-SNAPSHOT + jpadatasource + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + mysql + mysql-connector-java + runtime + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/sql.sql b/sql.sql new file mode 100644 index 0000000..cd070ef --- /dev/null +++ b/sql.sql @@ -0,0 +1,13 @@ +DROP DATABASE IF EXISTS jpa_test; +CREATE DATABASE jpa_test + DEFAULT CHARACTER SET utf8mb4 + COLLATE utf8mb4_general_ci; +GRANT ALL ON jpa_test.* TO financial_adminer@'%' + IDENTIFIED BY 'financial_adminer_pass'; + +DROP DATABASE IF EXISTS jpa_test_bak; +CREATE DATABASE jpa_test_bak + DEFAULT CHARACTER SET utf8mb4 + COLLATE utf8mb4_general_ci; +GRANT ALL ON jpa_test_bak.* TO financial_adminer@'%' + IDENTIFIED BY 'financial_adminer_pass'; \ No newline at end of file diff --git a/src/main/java/top/fjy8018/jpadatasource/JpadatasourceApplication.java b/src/main/java/top/fjy8018/jpadatasource/JpadatasourceApplication.java new file mode 100644 index 0000000..9205223 --- /dev/null +++ b/src/main/java/top/fjy8018/jpadatasource/JpadatasourceApplication.java @@ -0,0 +1,13 @@ +package top.fjy8018.jpadatasource; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JpadatasourceApplication { + + public static void main(String[] args) { + SpringApplication.run(JpadatasourceApplication.class, args); + } + +} diff --git a/src/main/java/top/fjy8018/jpadatasource/config/DataAccessConfig.java b/src/main/java/top/fjy8018/jpadatasource/config/DataAccessConfig.java new file mode 100644 index 0000000..429a033 --- /dev/null +++ b/src/main/java/top/fjy8018/jpadatasource/config/DataAccessConfig.java @@ -0,0 +1,44 @@ +package top.fjy8018.jpadatasource.config; + +import com.zaxxer.hikari.HikariDataSource; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +/** + * @author F嘉阳 + * @date 2019-03-30 9:24 + */ +@Configuration +public class DataAccessConfig { + @Bean + @Primary + @ConfigurationProperties("spring.datasource.first") + public DataSourceProperties firstDataSourceProperties() { + return new DataSourceProperties(); + } + + @Bean + @Primary + @ConfigurationProperties("spring.datasource.first.configuration") + public HikariDataSource firstDataSource() { + return firstDataSourceProperties().initializeDataSourceBuilder() + .type(HikariDataSource.class).build(); + } + + @Bean + @ConfigurationProperties("spring.datasource.second") + public DataSourceProperties secondDataSourceProperties() { + return new DataSourceProperties(); + } + + @Bean + @ConfigurationProperties("spring.datasource.second.configuration") + public HikariDataSource secondDataSource() { + return secondDataSourceProperties().initializeDataSourceBuilder() + .type(HikariDataSource.class).build(); + } +} + diff --git a/src/main/java/top/fjy8018/jpadatasource/entity/Product.java b/src/main/java/top/fjy8018/jpadatasource/entity/Product.java new file mode 100644 index 0000000..c109800 --- /dev/null +++ b/src/main/java/top/fjy8018/jpadatasource/entity/Product.java @@ -0,0 +1,22 @@ +package top.fjy8018.jpadatasource.entity; + +import lombok.Data; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author F嘉阳 + * @date 2019-03-30 9:11 + */ +@Data +@Entity +public class Product { + + @Id + private Integer id; + + private String name; + + private Integer price; +} diff --git a/src/main/java/top/fjy8018/jpadatasource/repository/ProductRepository.java b/src/main/java/top/fjy8018/jpadatasource/repository/ProductRepository.java new file mode 100644 index 0000000..9329e42 --- /dev/null +++ b/src/main/java/top/fjy8018/jpadatasource/repository/ProductRepository.java @@ -0,0 +1,11 @@ +package top.fjy8018.jpadatasource.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import top.fjy8018.jpadatasource.entity.Product; + +/** + * @author F嘉阳 + * @date 2019-03-30 9:12 + */ +public interface ProductRepository extends JpaRepository { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..3d26e7a --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,26 @@ +spring: + datasource: + first: + driver-class-name: com.mysql.cj.jdbc.Driver + username: financial_adminer + password: financial_adminer_pass + url: jdbc:mysql://120.79.226.26/jpa_test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC + configuration: + maximum-pool-size: 30 + second: + driver-class-name: com.mysql.cj.jdbc.Driver + username: financial_adminer + password: financial_adminer_pass + url: jdbc:mysql://120.79.226.26/jpa_test_bak?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC + configuration: + maximum-pool-size: 20 + + jpa: + hibernate: + ddl-auto: update + show-sql: true + database-platform: org.hibernate.dialect.MySQL55Dialect + properties: + hibernate: + format_sql: true + use_sql_comments: true \ No newline at end of file diff --git a/src/test/java/top/fjy8018/jpadatasource/JpadatasourceApplicationTests.java b/src/test/java/top/fjy8018/jpadatasource/JpadatasourceApplicationTests.java new file mode 100644 index 0000000..45af356 --- /dev/null +++ b/src/test/java/top/fjy8018/jpadatasource/JpadatasourceApplicationTests.java @@ -0,0 +1,16 @@ +package top.fjy8018.jpadatasource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class JpadatasourceApplicationTests { + + @Test + public void contextLoads() { + } + +}