使用 JPA 和 Microsoft SQL Server 配置 Spring

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

第 1 步:添加依赖项

第一步是将必要的依赖项添加到您的 pom.xml 文件中。

<依赖项>
    
    <依赖关系>
        com.microsoft.sqlserver
        mssql-jdbc
        <版本>7.2.2.jre8
    

    
    <依赖关系>
        org.springframework.boot
        spring-boot-starter-data-jpa
    

    
    <依赖关系>
        org.springframework.bo

ot
spring-boot-starter-web

第2步:配置JPA

现在让我们创建JPA配置类。我将使用命名法 JPAConfiguration.java.

软件包 br.com.meuprojeto.config;

导入 org.springframework.context.annotation.Bean;
导入 org.springframework.context.annotation.Configuration;
导入 org.springframework.context.annotation.Profile;
导入 org.springframework.jdbc.datasource.DriverManagerDataSource;
导入 org.springframework.orm.jpa.JpaTransactionManager;
导入 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
导入 org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
导入 org.springframework.transaction.annotation.EnableTransactionManagement;

导入 javax.persistence.EntityManagerFactory;
导入javax.sql.DataSource;
导入java.util.Properties;

@配置
@启用事务管理
公共类 JPAConfiguration {

    @豆
    公共LocalContainerEntityManagerFactoryBeanEntityManagerFactory(数据源dataSource,属性additionalProperties){
        LocalContainerEntityManagerFactoryBeanfactoryBean = new LocalContainerEntityManagerFactoryBean();
        HibernateJpaVendorAdapter 供应商适配器 = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setPackagesToScan(“br.com.meuprojeto.loja.models”);
        FactoryBean.setDataSource(dataSource);
        factoryBean.setJpaProperties(additionalProperties);
        返回工厂Bean;
    }

    @豆
    @个人资料(“开发”)
    公共属性additionalProperties() {
        属性属性 = new Properties();
        property.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
        property.setProperty("hibernate.show_sql", "true");
        property.setProperty("hibernate.hbm2ddl.auto", "创建");
        property.setProperty("javax.persistence.schema- Generation.scripts.create-target", "db-schema.jpa.ddl");
        返回属性;
    }

    @豆
    @个人资料(“开发”)
    公共 DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("sa");
        dataSource.setPassword(""); // 在这里添加您的密码
        dataSource.setUrl("jdbc:sqlserver://127.0.0.1;databaseName=MyProject;");
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        返回数据源;
    }

    @豆
    公共 JpaTransactionManager transactionManager(EntityManagerFactory emf) {
        返回新的 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 应用程序提供坚实的基础。根据需要进行实验和调整,以满足您的特定需求。