首頁  >  文章  >  Java  >  使用 JPA 和 Microsoft SQL Server 設定 Spring

使用 JPA 和 Microsoft SQL Server 設定 Spring

PHPz
PHPz原創
2024-07-17 08:17:25885瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn