使用Spring Boot + Hibernate + MySql运行MVC应用程序
我是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; }
UserRepository:
@Repository @Table(name = "Users") public interface UserRepository extends JpaRepository { }
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
org.springframework.boot spring-boot-starter-parent 1.1.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.hibernate hibernate-entitymanager org.hibernate hibernate-core 4.3.0.Final org.hibernate hibernate-entitymanager 4.3.0.Final runtime org.hibernate hibernate-validator true org.hibernate.javax.persistence hibernate-jpa-2.0-api true org.springframework spring-orm mysql mysql-connector-java
编辑:添加主类
主类
@ComponentScan @Configuration @EnableAutoConfiguration public class ApplicationStart { public static void main(String[] args) { SpringApplication.run(ApplicationStart.class, args); } }
这是我的应用程序的当前设置。 我似乎甚至不知道在哪里寻找错误,互联网上的教程对我的事业没有帮助。 因此,非常感谢有关如何解决exception的任何帮助。
如果需要更多信息,请评论。
谢谢-
确保您的application.properties
位于其中一个受支持的位置 。
- 当前目录的A / config子目录。
- 当前目录
- 一个classpath / config包
- 类路径根
列表按优先级排序(列表中较高的位置覆盖较低的项目)。
虽然使用:
将属性文件中的键/值对分开,但是我应该建议坚持使用更常用的=
分隔符。
你的pom包含一些不必要的混乱,我建议你移动。 你应该只需要对mysql-connector-java
的依赖,其他一切都是杂乱的(其他依赖是通过你依赖的启动项目提供的)。
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java
这些应该是您需要的一切,版本和传递依赖关系由spring-boot-dependency pom.xml处理。 (初级父母的祖父母)。
使用@EnableAutoConfiguration
注释时, @EnableAutoConfiguration
注释的类也将用于确定从哪个包开始扫描。 通常,您会将此注释放在入门类上。 建议将此应用程序类放在顶级包中(即your.package.application.StarterClass
),所有其他包应该是该包的子包。 这样,将自动检测所有类。
如果不可能,您可能需要添加额外的@ComponentScan
来指定要开始扫描的基础包,并使用@EntityScan
指定包含您的实体的包。
我也面临同样的问题,但原因非常不同,我在上述所有答案中都尝试了2小时的所有答案。 但终于知道我的文件属性名称’Application.properties’是以大写’A’开头的。 它应该是小’A’。 所以请同时考虑这一点。