使用 Spring Boot + Hibernate + MySql 运行 MVC 应用程序

     2023-02-26     247

关键词:

【中文标题】使用 Spring Boot + Hibernate + MySql 运行 MVC 应用程序【英文标题】:Running a MVC app using Spring Boot + Hibernate + MySql 【发布时间】:2014-08-22 07:00:11 【问题描述】:

我是 Spring 环境的新手。我试图使用 SpringBoot 和 Hibernate 作为 ORM 和 MYSQL 作为数据库来开发一个基本的 MVC 应用程序。我在设置依赖项和配置时遇到了很多麻烦。目前,我对以下错误感到震惊,我无法弄清楚如何克服它。

org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

这是我在应用程序中的设置。它没有服务层和jsp页面以避免混乱

数据库:

MySql - 一个名为 Users 的数据库已经存在,它有一个名为 Users 的表,其中包含一个示例用户列表

User.java(模型)

@Entity
@Table(name = "Users")
public class User 

    @Id
    @GeneratedValue
    public String id;

    public String username;

    public String firstname;

    public String lastname;

    public String password;

用户存储库:

@Repository
@Table(name = "Users")
public interface UserRepository extends JpaRepository<User, String> 

用户控制器:

@RestController
public class UserController 

    private UserRepository userRepository;

    @Autowired
    public UserController(UserRepository userRepository)
    
        this.userRepository = userRepository;
    

    @RequestMapping("user")
    public void getUser(@RequestParam("id") String id) 
         User user = userRepository.findOne(id);
    

  

Application.properties:

server.port: 9000

spring.datasource.url:jdbc:mysql://localhost/Users

spring.datasource.driverClassName: com.mysql.jdbc.Driver

spring.datasource.username: 根

spring.datasource.password:

POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- HIBERNATE -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.0.Final</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- Spring ORM, works with Hibernate -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

</dependencies>

编辑:添加主类

主类

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class ApplicationStart 
    public static void main(String[] args)
    
        SpringApplication.run(ApplicationStart.class, args);
    

这是我的应用程序的当前设置。我什至不知道在哪里寻找错误,互联网上的教程对我的事业没有帮助。因此,非常感谢有关如何解决异常的任何帮助。

如果需要更多信息,请发表评论。

谢谢-

【问题讨论】:

显然它不会读取您的application.properties 文件。确保它位于类路径的根目录 (src/main/resources) 或位于 jar 旁边的文件系统上。通常,您可以使用 = 而不是 : 在属性文件中分隔键/值对,尽管后者应该可以工作。我也没有真正得到你的设置,只是删除了休眠的排除项,只包含 hibernate-entitymanager 而没有版本作为依赖项。其他一切都会得到照顾。 @M.Deinum:如果我删除该排除项,我会收到以下异常 - java.lang.IllegalArgumentException: Not an managed type: class models.User 而且,我的 application.properties 位于资源目​​录中。 这更多地表明您的设置存在其他问题。请添加您拥有的任何其他配置类/文件。 EnableAutoConfiguration 正在尝试根据它所在的包确定要扫描哪些包以查找实体。您可能需要添加一个 @EntityScan 指向包含您的实体类的包。另请注意,ComponentScan 将从 ApplicationStart 类所在的同一包开始扫描。也许您可以在帖子中包含您的包结构。 问题在于 ApplicationStart 与控制器、存储库和模型不在同一个包中。在将它们放在同一个包中之后,事情开始起作用了。我也不必添加 @EntityScan 注释。感谢您的帮助:)您能否在您的答案中添加您的最后一条评论 - 它可以帮助其他人轻松找到它:) 【参考方案1】:

确保您的application.properties 位于supported locations 之一中。

    当前目录的 /config 子目录。 当前目录 类路径 /config 包 类路径根

列表按优先级排序(列表中较高的位置覆盖较低的项目)。

虽然使用: 分隔属性文件中的键/值对应该可行,但我建议坚持使用更常用的= 分隔符。

您的 pom 包含一些不必要的杂物,我建议您移动它们。您应该只需要对mysql-connector-java 的依赖,其他一切都很混乱(其他依赖项是通过您依赖的启动项目提供的)。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

这些应该是您需要的一切,版本和传递依赖项由 spring-boot-dependency pom.xml 处理。 (starter-parent 的祖父母)。

当使用@EnableAutoConfiguration注解时,带有注解的类也将用于确定from which package开始扫描。通常,您会将此注释放在您的入门课程中。建议将此应用程序类放在***包中(即your.package.application.StarterClass),所有其他包都应该是该包的子包。这样所有的类都会被自动检测到。

如果这不可能,您可能需要添加一个额外的@ComponentScan 来指定一个基本包开始扫描,并添加一个@EntityScan 来指定包含您的实体的包。

【讨论】:

1) 我的 application.properties 在资源目录下。 2)我应用了您提供的依赖项配置,但我似乎遇到了以下异常:java.lang.IllegalArgumentException: Not an managed type: class models.User 这基本上表明它已经比依赖关系更进一步(它显然通过了数据源配置)并尝试构建存储库。请将您的主要课程添加到原始帖子中。 @M.Denium:我也添加了主类。除了我发布的配置之外,我似乎没有在应用程序中设置任何其他配置。 是否需要对hibernate-entitymanager 的显式依赖?在spring-boot-starter-data-jpa:1.1.8.RELEASE 中,我看到了对hibernate-entitymanager 的依赖。 @Abdull 如果你使用 JPA 的 starter pom 那么你不需要 hibernate-entitymanager 的显式依赖【参考方案2】:

我也面临同样的问题,但原因非常不同,我在所有答案中都尝试了上面提到的每件事,需要 2 小时。但终于知道我的文件属性名称“Application.properties”以大写“A”开头。它应该是小“A”。因此,请同时考虑这一点。

【讨论】:

6.spring+struts+hibernat注解方式整合

SSH注解方式实现1.创建db.properties文件,主要是数据库的连接数据jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.username=rootjdbc.password=0766342.创建Struts的配置文件,主要定义struts的常规配置<? 查看详情

Spring Boot & Hibernate:没有@Type 注释的 NVARCHAR 列类型

】SpringBoot&Hibernate:没有@Type注释的NVARCHAR列类型【英文标题】:SpringBoot&Hibernate:NVARCHARcolumntypewithout@Typeannotation【发布时间】:2020-08-0503:19:12【问题描述】:我的数据库中有一个表用于我的Customer实体。此表包含一些类型为N... 查看详情

springboot使用hibernatevalidator

...次要看哪些参数验证是否完整,需要去翻阅验证逻辑代码hibernatevalidator(官方文档)提供了一套比较完善、便捷的验证实现方式。spring-boot-starter-web包里面有hibernat 查看详情

使用 spring-boot:run 时是不是可以使用 spring-boot 命令行属性?

】使用spring-boot:run时是不是可以使用spring-boot命令行属性?【英文标题】:Arespring-bootcommandlinepropertiesavailablewhenusingspring-boot:run?使用spring-boot:run时是否可以使用spring-boot命令行属性?【发布时间】:2015-02-0219:55:03【问题描述】:... 查看详情

我可以通过 spring-boot 轻松替换 spring-boot-starter 以避免使用 spring-boot-starter-parent 吗?

】我可以通过spring-boot轻松替换spring-boot-starter以避免使用spring-boot-starter-parent吗?【英文标题】:CanIeasilyreplacespring-boot-starterbyspring-boottoavoiduseofspring-boot-starter-parent?【发布时间】:2018-10-1514:49:57【问题描述】:我想使用spring-boot-... 查看详情

使用spring-boot对rest服务进行访问控制

】使用spring-boot对rest服务进行访问控制【英文标题】:Accesscontroltorestserviceswithspring-boot【发布时间】:2017-02-1601:32:44【问题描述】:我玩了一下弹簧启动安全性。我使用mongodb、spring-boot-starter-data-rest、spring-boot-starter-security和sprin... 查看详情

使用 Spring Boot 安全性在 Spring Boot 中的登录问题

】使用SpringBoot安全性在SpringBoot中的登录问题【英文标题】:LoginIssueinSpringBootusingspringbootsecurity【发布时间】:2018-09-1009:49:09【问题描述】:我是SpringBoot新手,我正在我的示例应用程序上尝试SpringBoot安全性,并且我正在使用Mongo... 查看详情

CharacterEncodingFilter 未使用 spring-boot 1.2.0 和 spring-boot-legacy 设置

】CharacterEncodingFilter未使用spring-boot1.2.0和spring-boot-legacy设置【英文标题】:CharacterEncodingFilternotsetwithspring-boot1.2.0andspring-boot-legacy【发布时间】:2015-02-2201:17:30【问题描述】:我在tomcat(servlet2.5)中运行传统的春季启动战。这些请... 查看详情

在 Tomcat 上使用 Spring Boot 进行身份验证

】在Tomcat上使用SpringBoot进行身份验证【英文标题】:GettingAuthenticationToWorkWithSpringBootOnTomcat【发布时间】:2014-09-1014:07:47【问题描述】:我正在尝试让spring-boot-starter-security与spring-boot-starter-web和spring-boot-starter-tomcat一起工作。我... 查看详情

如何使用 Spring-Boot 播种 Spring-Security

】如何使用Spring-Boot播种Spring-Security【英文标题】:HowdoIseedSpring-SecuritywithSpring-Boot【发布时间】:2014-08-0207:21:13【问题描述】:我有一个使用Spring-Securityspring-security-web:4.0.0.M1的spring-boot1.1.0.BUILD-SNAPSHOT项目。我想在我的H2表中植... 查看详情

Spring Boot 在使用 solrj 而不是 spring-boot-starter-data-solr 时会爆炸

】SpringBoot在使用solrj而不是spring-boot-starter-data-solr时会爆炸【英文标题】:SpringBootblowsupwhenusingsolrjbutnotspring-boot-starter-data-solr【发布时间】:2015-06-1318:22:23【问题描述】:我正在尝试将现有的Spring应用程序移植到SpringBoot。我不使... 查看详情

Spring Boot 应用程序使用 spring-boot-starter-actuator 给出“无法启动 tomcat”异常

】SpringBoot应用程序使用spring-boot-starter-actuator给出“无法启动tomcat”异常【英文标题】:Springbootapplicationgives"unabletostarttomcat"exceptionwithspring-boot-starter-actuator【发布时间】:2020-07-2717:16:20【问题描述】:我有我的spring-boot应... 查看详情

spring-boot 使用啥版本的 Jackson?

】spring-boot使用啥版本的Jackson?【英文标题】:WhatversionofJacksondoesspring-bootuse?spring-boot使用什么版本的Jackson?【发布时间】:2018-05-2123:11:27【问题描述】:我正在努力确保以安全的方式使用spring-boot和Jackson。Jackson的某些版本中... 查看详情

使用 JUnit 5 的 spring-boot-starter-test

】使用JUnit5的spring-boot-starter-test【英文标题】:spring-boot-starter-testwithJUnit5【发布时间】:2019-03-2213:46:28【问题描述】:从2.0.6开始使用spring-boot-starter-test会引入JUnit4依赖项。如何使用spring-boot-starter-test(通过Gradle),但使用JUnit5... 查看详情

使用 spring-boot OAuth2 服务器保护的 Spring-boot 应用程序

】使用spring-bootOAuth2服务器保护的Spring-boot应用程序【英文标题】:Spring-bootapplicationsecuredwithaspring-bootOAuth2server【发布时间】:2015-11-3016:38:21【问题描述】:我正在学习如何使用Spring-boot保护应用程序以及如何通过本教程设置OAuth2... 查看详情

在 spring-boot 项目中使用 spring mvc xml 项目

】在spring-boot项目中使用springmvcxml项目【英文标题】:Usespringmvcxmlprojectinsidespring-bootproject【发布时间】:2016-08-1308:42:55【问题描述】:我创建了这个测试项目,由2个项目组成:一个使用spring-boot,一个使用spring-mvc。它们中的每... 查看详情

使用 spring boot 和 spring-boot-maven-plugin 生成战争时排除 application.properties

】使用springboot和spring-boot-maven-plugin生成战争时排除application.properties【英文标题】:Excludeapplication.propertieswhengeneratingwarusingspringbootandspring-boot-maven-plugin【发布时间】:2014-12-3120:51:54【问题描述】:我正在使用SpringBoot开发一个Web... 查看详情

Spring-boot & hibernate,使用事务

】Spring-boot&hibernate,使用事务【英文标题】:Spring-boot&hibernate,usingtransaction【发布时间】:2014-11-2020:36:47【问题描述】:我正在尝试使用spring-boot和休眠。当我使用存储库时它工作得很好,但我试图让一个Hibernate会话来创建... 查看详情