11.4 数据库层配置
上面的模板工程,我们来直接运行main函数,会发现启动失败,控制台会输出如下报错信息:
BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]
...
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
因为我们还没有配置数据源。我们先在MySQL中新建一个schema:
CREATE SCHEMA `blog` DEFAULT CHARACTER SET utf8 ;
11.4.1 配置数据源
接着,我们在配置文件application.properties中配置MySQL数据源:
```# datasource# datasource: unicode编码的支持,设定为utf-8
spring.datasource.url=jdbc//localhost:3306/blog?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&characterSetResults=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
其中,spring.datasource.url 中,为了支持中文的正确显示(防止出现中文乱码),我们需要设置一下编码。
数据库ORM(对象关系映射)层,我们使用spring-data-jpa :
spring.jpa.database=MYSQL
spring.jpa.show-sql=true# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update# Naming strategy
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
再次运行启动类,控制台输出启动日志:
…
_ __ _ _ _
| |/ / | | | (_) _
| ' / ___ | |_| |_ _ __ _| |_
| < / _ \| __| | | '_ \ |_ _|
| . \ (_) | |_| | | | | | |_|
|_|\_\___/ \__|_|_|_| |_|
_____ _ ____ _
/ ____| (_) | _ \ | |
| (___ _ __ _ __ _ _ __ __ _| |_) | ___ ___ | |_
\___ \| '_ \| '__| | '_ \ / _` | _ < / _ \ / _ \| __|
____) | |_) | | | | | | | (_| | |_) | (_) | (_) | |_
|_____/| .__/|_| |_|_| |_|\__, |____/ \___/ \___/ \__|
| | __/ |
|_| |___/
2017-07-17 21:10:48.741 INFO 5062 —- [ restartedMain] c.Chapter11KotlinSpringbootApplicationKt : Starting Chapter11KotlinSpringbootApplicationKt on 192.168.1.6 with PID 5062 …
…
2017-07-17 21:19:40.548 INFO 5329 —- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit ‘default’
2017-07-17 21:11:03.017 INFO 5062 —- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped “{[/application/env || /application/env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}” onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
…
2017-07-17 21:11:03.026 INFO 5062 —- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped “{[/application/beans || /application/beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}” onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-07-17 21:11:03.028 INFO 5062 —- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped “{[/application/health || /application/health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}” onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
…
2017-07-17 21:11:03.060 INFO 5062 —- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped “{[/application/autoconfig || /application/autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}” onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
…
2017-07-17 21:11:03.478 INFO 5062 —- [ restartedMain] o.s.ui.freemarker.SpringTemplateLoader : SpringTemplateLoader for FreeMarker: using resource loader [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6aab8872: startup date [Mon Jul 17 21:10:48 CST 2017]; root of context hierarchy] and template loader path [classpath:/templates/]
2017-07-17 21:11:03.479 INFO 5062 —- [ restartedMain] o.s.w.s.v.f.FreeMarkerConfigurer : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2017-07-17 21:11:03.520 WARN 5062 —- [ restartedMain] o.s.b.a.f.FreeMarkerAutoConfiguration : Cannot find template location(s): [classpath:/templates/] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)
2017-07-17 21:11:03.713 INFO 5062 —- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2017-07-17 21:11:03.871 INFO 5062 —- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-17 21:11:03.874 INFO 5062 —- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Bean with name ‘dataSource’ has been autodetected for JMX exposure
2017-07-17 21:11:03.886 INFO 5062 —- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Located MBean ‘dataSource’: registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2017-07-17 21:11:03.901 INFO 5062 —- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-07-17 21:11:04.232 INFO 5062 —- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http)
2017-07-17 21:11:04.240 INFO 5062 —- [ restartedMain] c.Chapter11KotlinSpringbootApplicationKt : Started Chapter11KotlinSpringbootApplicationKt in 16.316 seconds (JVM running for 17.68)
```
关于上面的日志,我们通过下面的表格作简要说明:
日志内容 | 简要说明 |
---|---|
LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory | 初始化JAP实体管理器工厂 |
EndpointHandlerMapping : Mapped “{[/application/beans …等 | SpringBoot健康监控Endpoint 等REST接口 |
FreeMarkerAutoConfiguration | Freemarker模板引擎自动配置,默认视图文件目录是classpath:/templates/ |
AnnotationMBeanExporter : Bean with name ‘dataSource’ has been autodetected for JMX exposure … Located MBean ‘dataSource’: registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] | 数据源Bean通过annotation注解注册MBean到JMX实现监控其运行状态 |
TomcatWebServer : Tomcat started on port(s): 8000 (http) | SpringBoot默认内嵌了Tomcat,端口我们可以在application.properties中配置 |
Started Chapter11KotlinSpringbootApplicationKt in 16.316 seconds (JVM running for 17.68) | SpringBoot应用启动成功 |