我是spring
框架的新手,我想将位于MySQL
的{{1}}数据库与spring boot应用程序连接。
答案 0 :(得分:0)
要与mysql连接,需要以下依赖项
依赖关系(maven-> pom.xml)。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
application.properties
中的需要一些额外的配置设置,必须像这样
application.properties(src / main / resources / application.properties)
# DataSource settings: set here your own configurations for the database
# connection. In this example we have "netgloo_blog" as database name and
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:8889/database_name
spring.datasource.username = mysql-userId
spring.datasource.password = mysql-pwd
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy (Not necessary to add but you can use this too)
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
现在,当您启动spring boot配置时,您的EntityManager
对象将被构造。
上面定义的配置足以连接您的本地my-sql数据库,但这是您需要使代码更具可读性的更多配置。
启用JPAR存储库,以便必须在已定义的程序包中定义CRUD存储库。
像这样将@EnableJPARepositories("basepackage.*")
添加到您的SpringBootMainApplicationClass
中。
@SpringBootApplication
@EnableJPARepositories("com.demo.application.*.repositories")
@ComponentScan("com.demo.application.*")
public class SpringBootMainApplicationClass {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplicationClass.class, args);
}
}
通过在@EnableJPARepositories
中添加MainClass
注释,用户可以使您的代码更具可读性,并且EntityManager
对象仅限于定义的包。
答案 1 :(得分:0)
我正在列出最低配置:
在pom.xml中
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties中
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
到处都有这样的例子。喜欢阅读https://spring.io/guides/gs/accessing-data-mysql/
也请查看其他问题:
答案 2 :(得分:0)
您需要在application.properties中添加Mysql的数据库配置属性:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=mysql
spring.datasource.password=mysql
spring.jpa.hibernate.ddl-auto=create
,此配置将根据您创建的实体创建表。spring.datasource.url=jdbc:mysql://localhost:3306/db_example
,此配置通过jdbc连接到名为db_example的数据库。您需要创建该数据库。spring.datasource.username=mysql
这种配置,您需要将要通过jdbc连接到您的mysql数据库的用户放进去。spring.datasource.password=mysql
这种配置,您需要提供由用户代表的密码,以便通过jdbc连接到您的mysql数据库。除了您还需要添加Mysql和JPA的依赖项。
如果使用的是maven,请在pom.xml中添加依赖项:
<dependencies>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
如果使用的是gradle,请在build.gradle中添加依赖项:
dependencies {
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
}
答案 3 :(得分:0)
在pom.xml
中添加以下依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties中添加以下数据库属性。
spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=root
spring.datasource.password=root
有关更多详细信息,请参阅此link
答案 4 :(得分:0)
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/jpa
spring.datasource.username = root
spring.datasource.password =
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
build.gradle
implementation 'mysql:mysql-connector-java:8.0.15'