application.properties
#Configuration
myconfig.source = "D:\\sourceFolder"
#Database configuration
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
这是spring boot主应用程序调用
@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
ConfigProperties config = new ConfigProperties();
System.out.println("get source folder path" + config.getSource()); // here Null
DemoClass demo = new DemoClass();
demo.process();
}
}
这是配置类
@Configuration
@PropertySource(value = "classpath:application.properties")
@ConfigurationProperties(prefix = "myconfig")
public class ConfigProperties {
//@Value("${myconfig.source}")
private String source;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
这是演示类-处理所有数据
public class DemoClass {
@Autowired ConfigProperties config;
@Autowired DataRepository repository;
void process() {
List<DemoData> getresultList = getList();
System.out.println("get results List : " + getresultList);
}
private List<DemoData> getList() {
System.out.println(" gettng all data from DB: " + repository.findAll());
return repository.findAll();
}
}
这里的桌子是
id | year | dataname | data
这里是POJO类
@Entity
@Table(name = "demodata")
public class DemoData implements Serializable {
private static final long serialVersionUID = -2343243243242432341L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "year")
private int year;
@Column(name = "dataname")
private String dataName;
@Column(name = "data")
private String data;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getDataName() {
return dataName;
}
public void setDataName(String dataName) {
this.dataName = dataName;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
此处是JPA存储库
@Repository
public interface DataRepository extends JpaRepository<DemoData, Integer>{
List<DemoData> findAll();
}
这是我的日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-07-31 15:15:18.937 INFO 10720 --- [ main] c.e.filesCopy.DemoApplication : Starting DemoApplication on Ashwath-PC with PID 10720 (D:\STS-workspace\xelpro\filesCopy\target\classes started by Ashwath in D:\STS-workspace\xelpro\filesCopy)
2019-07-31 15:15:18.944 INFO 10720 --- [ main] c.e.filesCopy.DemoApplication : No active profile set, falling back to default profiles: default
2019-07-31 15:15:20.028 INFO 10720 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-31 15:15:20.137 INFO 10720 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 92ms. Found 1 repository interfaces.
2019-07-31 15:15:20.969 INFO 10720 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-07-31 15:15:21.409 INFO 10720 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-07-31 15:15:21.563 INFO 10720 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-07-31 15:15:21.663 INFO 10720 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-31 15:15:21.665 INFO 10720 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-07-31 15:15:21.887 INFO 10720 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-31 15:15:22.355 INFO 10720 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-07-31 15:15:23.457 INFO 10720 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2d760326'
2019-07-31 15:15:23.462 INFO 10720 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-07-31 15:15:24.509 INFO 10720 --- [ main] c.e.filesCopy.DemoApplication : Started DemoApplication in 6.23 seconds (JVM running for 6.919)
null
null gettng all data from DB:
Exception in thread "main" java.lang.NullPointerException
at com.example.filesCopy.DemoData.getList(DemoData.java:33)
at com.example.filesCopy.DemoData.process(DemoData.java:17)
at com.example.filesCopy.DemoApplication.main(DemoApplication.java:19)
2019-07-31 15:15:24.525 INFO 10720 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-07-31 15:15:24.526 INFO 10720 --- [ Thread-2] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2019-07-31 15:15:24.545 INFO 10720 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-07-31 15:15:24.550 INFO 10720 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
更新为pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>filesCopy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>filesCopy</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>