面向解决问题的java编程,spring boot,mybatis generator和坑 |
您所在的位置:网站首页 › pomxml是干嘛的 › 面向解决问题的java编程,spring boot,mybatis generator和坑 |
1、start一个spring boot项目
第一课我们也不能免俗,要从starter开始,spring boot的起始项目脚手架可以从spring boot官方starter生成地址开始:https://start.spring.io/ 这张图列出了一个spring boot项目的几个基本信息,我们大略的说一下: spring boot的便利之处就在这里,我们引入我们需要的工具,框架的时候,不需要一个一个的jar包引入,而是可以整体引入,它自动会引入依赖包。点击
这个截图我用的是eclipse,但结构是典型的maven项目的结构。 src/main/java存放代码 src/main/resource下面存放配置文件、静态文件(static)和模板文件(templates),templates并不是maven的默认创建的目录,而是spring boot自行创建的,后面如果我们用thymeleaf的话,会把模板文件放在templates。 scr/test/java就是放测试代码的地方 target是我们的代码编译后的class文件和打包后的jar包、war包生成的地方 最外面的是pom.xml文件,就是maven的配置啦(请忽略上面的红点,这是我eclipse的问题)现在,我们需要做的第一件事就是运行src/main/java下面的Lesson1Application,把项目跑起来。 3、坑其实如果你直接运行是跑不起来的,报错信息如下: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-05-23 12:39:03.599 ERROR 19220 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). 原因是我们虽然还没有用到数据库,但我们在pom.xml里面引入了mybatis: org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1只要用到了mybatis-spring-boot-starter,哪怕还没有任何访问数据库的动作,但应用也会去找数据库的配置,如果没找到,就会报错,解决的办法就是把数据库的配置在application.properties里面加上: spring.datasource.driver=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/ry?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root但其实很不幸,还是可能报错,因为我虽然引入了配置,但我用的mysql本身的依赖包并没有: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-05-23 13:34:18.286 ERROR 14588 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource: Property: driverclassname Value: com.mysql.cj.jdbc.Driver Origin: "driverClassName" from property source "source" Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader Action: Update your application's configuration我们只能再次打开pom.xml,引入mysql的jdbc driver依赖包: mysql mysql-connector-java runtime或者在开始的start.spring.io页面,就直接把mysql的jdbc driver 包加上,效果是一样的 这下再运行Lesson1Application,就不会再有问题了。 虽然现在没有问题了,但数据库链接的问题其实还有各种坑等着我们,后面我们讲多数据源配置的时候,再跳一次坑,继续讲。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |