使用Spring Boot + Hibernate + MySql运行MVC应用程序 [英] Running a MVC app using Spring Boot + Hibernate + MySql

查看:241
本文介绍了使用Spring Boot + Hibernate + MySql运行MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  org.springframework .beans.factory.BeanCreationException:无法确定数据库类型为NONE的嵌入式数据库驱动程序类。如果你想要一个嵌入式数据库,请在类路径中放一个支持的数据库。 

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

数据库:



MySql - 一个名为Users的数据库已经存在,并且有一个名为Users的表,其中包含用户样本列表

用户。 Java(模型)

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

@Id
@GeneratedValue
public String id;

public String username;

public String firstname;

public String lastname;

公共字符串密码;
}

UserRepository:

  @Repository 
@Table(name =Users)
public interface UserRepository扩展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);




$ b

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:


POM.xml

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

<依赖关系>

< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>

< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
<排除项>
<排除>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< /排除>
< /排除>
< /依赖关系>

<! - - HIBERNATE - >

< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.3.0.Final< / version>
< /依赖关系>

< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.3.0.Final< / version>
< scope>运行时< / scope>
< /依赖关系>

< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
<可选> true< /可选>
< /依赖关系>

< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
<可选> true< /可选>
< /依赖关系>

<! - Spring ORM,适用于Hibernate - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< /依赖关系>

<! - MYSQL - >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< /依赖关系>

< /依赖关系>

编辑:添加主类

主类

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

这是我的应用程序的当前设置。我甚至不知道在哪里寻找错误,而互联网上的教程并没有帮助我的事业。所以,有关如何解决异常的任何帮助,我们将非常感激。



请评论是否需要更多信息。



感谢 -

解决方案

确保您的 application.properties 为在其中一个支持的位置



  1. 当前目录的A / config子目录。

  2. 当前目录

  3. 类路径/配置文件包

  4. 类路径root


$

尽管分开您的密钥/值对与一个属性文件应该工作我建议坚持更常用的 = 分隔符。



你的pom包含了一些我不建议你移动的不必要的混乱。你应该只需要依赖于 mysql-connector-java 其他所有东西都是混乱的(其他依赖项是通过你依赖的初始项目提供的)。

 <依赖关系> 
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-web< / artifactId>
< /依赖关系>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-data-jpa< / artifactId>
< /依赖关系>

<! - MYSQL - >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< /依赖关系>
< /依赖关系>

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



当使用 @EnableAutoConfiguration 注释带注释的类也将用于确定从哪个包开始扫描。一般来说,你会把这个注释放在你的初学者课堂上。建议将该应用程序类放在顶层包(即 your.package.application.StarterClass )中,所有其他包应该是该包的子包。这样所有的课程都会被自动检测到。



如果这是不可能的,你可能需要添加一个额外的 @ComponentScan 来指定一个基本包来启动从 @EntityScan 来指定包含实体的包裹。


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.

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 pages to avoid the clutter

DATABASE:

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

User.java(Model)

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

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>

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-

解决方案

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

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

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

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

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

When using the @EnableAutoConfiguration annotation the class with the annotation will also be used to determine from which package to 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.

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

这篇关于使用Spring Boot + Hibernate + MySql运行MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆