首页 > Java > java教程 > 正文

使用 JPA 和 Microsoft SQL Server 配置 Spring

PHPz
发布: 2024-07-17 08:17:25
原创
877 人浏览过

Configurando o Spring com JPA e Microsoft SQL Server

在 Java 开发环境中设置数据库可能是一项具有挑战性的任务,特别是在选择正确的驱动程序并正确配置依赖项时。在这里,我将分享如何使用JPA和SQL Server搭建Spring MVC环境。

第 1 步:添加依赖项

第一步是将必要的依赖项添加到您的 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>
登录后复制

第2步:配置JPA

现在让我们创建 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);
    }
}
登录后复制

配置亮点

  1. EntityManagerFactory Bean:使用 Hibernate 适配器配置 EntityManagerFactory 并定义 JPA 实体所在的包。
  2. 其他属性:Hibernate 特定设置,例如 SQL 方言、控制台中的 SQL 显示以及数据库架构生成。
  3. DataSource Bean:配置与数据库的连接,包括URL、用户、密码和驱动程序。
  4. TransactionManager Bean:管理 JPA 事务。

最后的考虑因素

为开发环境配置数据库时,必须确保驱动程序和 SQL Server 版本兼容。在上面的示例中,驱动程序版本 7.2.2.jre8 已成功与最新版本的 SQL Server Developer 和 Express 一起使用。

此配置应该为开始使用 SQL Server 使用 JPA 开发 Spring MVC 应用程序提供坚实的基础。根据需要进行实验和调整,以满足您的特定需求。

以上是使用 JPA 和 Microsoft SQL Server 配置 Spring的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!