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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24514981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:41:20  来源:igfitidea点击:

Running a MVC app using Spring Boot + Hibernate + MySql

javahibernatemavenspring-boot

提问by Balasubramanian

I am new to the Spring environment. I was trying to develop a basic MVC application using SpringBoot with Hibernate as ORM and MYSQL as database. I ran into lots of troubles setting-up the dependencies and the configurations. Currently, I was struck on the following error and I was not able to figure out how to get over it.

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

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.

This is the set-up that I have in my application. It has no service layer and jsp pagesto avoid the clutter

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

DATABASE:

数据库:

MySql - A Database called Users is already present and it has a table called Users with a sample list of users

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

User.java(Model)

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;
}

UserRepository:

用户存储库:

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

UserController:

用户控制器:

@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: root

spring.datasource.password:

服务器端口:9000

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

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

spring.datasource.username:root

spring.datasource.password:

POM.xml

POM文件

<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>

EDIT: Adding the main class

编辑:添加主类

MAIN CLASS

主班

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

This is the current setup of my application. I don't even seem to know where to look for errors and the tutorials in the internet did not help my cause. So, any help on how to resolve the exception is much appreciated.

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

Please comment if more information is required.

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

Thanks-

谢谢-

采纳答案by M. Deinum

Make sure that your application.propertiesis in one of the supported locations.

确保您application.properties位于受支持的位置之一

  1. A /config subdir of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The class path root

The list is ordered by precedence (locations higher in the list override lower items).

  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 一个类路径 /config 包
  4. 类路径根

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

Although separating your key/value pairs in a properties file with a :should work I suggest sticking to the more generally used =separator.

尽管在属性文件中用 a 分隔键/值对:应该可以工作,但我建议坚持使用更常用的=分隔符。

Your pom contains some unnecessary clutter which I suggest you move. You should only need the dependency on the mysql-connector-javaeverything else is clutter (the other dependencies are provided through the starter projects you depend on).

你的 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>

These should be everything you need, versions and transitive dependencies are taken care of by the spring-boot-dependency pom.xml. (The grandparent of the starter-parent).

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

When using the @EnableAutoConfigurationannotation the class with the annotation will also be used to determine from which packageto start scanning. In general you will put this annotation on your starter class. It is advisable to put this application class in a top level package (ie. your.package.application.StarterClass) all other packages should be sub package from that package. This way all classes will be automatically detected.

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

If that isn't possible you might need to add an additional @ComponentScanto specify a base package to start scanning from and a @EntityScanto specify the package(s) which contain your entities.

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

回答by Vikash

I was also facing same issue but cause was very different, i tried every thing mentioned above in all answer taking 2 hour. But finally came to know that my file properties name 'Application.properties' is started with capital 'A'. it should be small 'A'. So please keep this in consideration also.

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