在 Java 開發環境中設定資料庫可能是一項具有挑戰性的任務,特別是在選擇正確的驅動程式並正確配置依賴項時。在這裡,我將分享如何使用JPA和SQL Server來建立Spring MVC環境。
第一步是將必要的依賴項新增到您的 pom.xml 檔案中。
<dependencies> <!-- Dependência do MSSQL --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>7.2.2.jre8</version> </dependency> <!-- Dependência do Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Dependência do Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
現在讓我們建立 JPA 配置類別。我將使用命名法 JPAConfiguration.java。
package br.com.meuprojeto.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableTransactionManagement public class JPAConfiguration { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Properties additionalProperties) { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); factoryBean.setJpaVendorAdapter(vendorAdapter); factoryBean.setPackagesToScan("br.com.meuprojeto.loja.models"); factoryBean.setDataSource(dataSource); factoryBean.setJpaProperties(additionalProperties); return factoryBean; } @Bean @Profile("dev") public Properties additionalProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("javax.persistence.schema-generation.scripts.create-target", "db-schema.jpa.ddl"); return properties; } @Bean @Profile("dev") public DriverManagerDataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUsername("sa"); dataSource.setPassword(""); // Adicione sua senha aqui dataSource.setUrl("jdbc:sqlserver://127.0.0.1;databaseName=MeuProjeto;"); dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); return dataSource; } @Bean public JpaTransactionManager transactionManager(EntityManagerFactory emf) { return new JpaTransactionManager(emf); } }
為開發環境配置資料庫時,必須確保驅動程式和 SQL Server 版本相容。在上面的範例中,驅動程式版本 7.2.2.jre8 已成功與最新版本的 SQL Server Developer 和 Express 一起使用。
此配置應該為開始使用 SQL Server 使用 JPA 開發 Spring MVC 應用程式提供堅實的基礎。根據需要進行實驗和調整,以滿足您的特定需求。
以上是使用 JPA 和 Microsoft SQL Server 設定 Spring的詳細內容。更多資訊請關注PHP中文網其他相關文章!