在本文中,我们将使用Spring Data JPA和Spring Boot与MariaDB数据库进行通信。
依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
application.properties配置:
spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql:<font><i>//192.168.99.100:3306/test1</i></font><font> spring.datasource.username=root spring.datasource.password=root spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect spring.jpa.show-sql=<b>true</b> </font>
扫描或加载JPA存储库
- 如果存储库包是Spring Boot主包的子包,那么@SpringBootApplication就足够了,因为它包含了@EnableAutoConfiguration。
- 但是如果存储库包不是Spring主类包的子包,那么在这种情况下,我们需要声明存储库包,如下所示:@EnableJpaRepositories(basePackages = "com.springbootdev.examples.jpa.repositories&