在 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中文网其他相关文章!