https://www.2cto.com/database/201707/654694.html
http://www.jb51.net/article/118092.htm
業務背景:因soa系統要供外網訪問,處於安全考慮用springboot做了個前置模塊,用來轉發外網調用的請求和soa返回的應答。其中外網的請求接口地址在DB2數據庫中對應專門的一張表來維護,要是springboot直接訪問數據庫,還要專門申請權限等,比較麻煩,而一張表用內置的H2數據庫維護也比較簡單,就可以作為替代的辦法。
環境:springboot+maven3.3+jdk1.7
1.springboot的Maven工程結構
說明一下,resource下的templates文件夾沒啥用。我忘記刪掉了。。。
2. 首先引入依賴jar包 pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<!--?xml version=
"1.0"
encoding=
"UTF-8"
?-->
<project xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelversion>
4.0
.
0
</modelversion>
<groupid>com.zlf</groupid>
spring-boot</artifactid>
<version>
1.0
-SNAPSHOT</version>
<!-- 增加父pom ,spring-boot-starter-parent包含了大量配置好的依賴管理,他是個特殊的starter,它提供了有用的maven默認設置 -->
<parent>
<groupid>org.springframework.boot</groupid>
spring-boot-starter-parent</artifactid>
<version>
1.4
.
3
.RELEASE</version>
</parent>
<!-- Spring默認使用jdk1.
6
,如果你想使用jdk1.
8
,你需要在pom.xml的屬性里面添加java.version,如下: -->
<properties>
<project.build.sourceencoding>UTF-
8
</project.build.sourceencoding>
<tomcat.version>
7.0
.
72
</tomcat.version>
<java.version>
1.8
</java.version>
</properties>
<!-- Spring通過添加spring-boot-starter-*這樣的依賴就能支持具體的某個功能。 -->
<!-- 我們這個示例最終是要實現web功能,所以添加的是這個依賴。 -->
<dependencies>
<dependency>
<!-- 指定為Web應用,並啟動一個內嵌的Servlet容器(默認是Tomcat)用於處理HTTP請求 -->
<groupid>org.springframework.boot</groupid>
spring-boot-starter-web</artifactid>
</dependency>
<!-- 對Java 持久化API的支持,包括spring-data-jap,spring-orm,Hibernate-->
<dependency>
<groupid>org.springframework.boot</groupid>
spring-boot-starter-data-jpa</artifactid>
</dependency>
<!-- lombok插件,方便model對象的處理 -->
<dependency>
<groupid>org.projectlombok</groupid>
lombok</artifactid>
</dependency>
<!-- 內嵌數據庫 -->
<dependency>
<groupid>com.h2database</groupid>
h2</artifactid>
</dependency>
<!-- mysql驅動 -->
<!-- <dependency> -->
<!-- <groupId>mysql</groupId> -->
<!-- mysql-connector-java</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupid>junit</groupid>
junit</artifactid>
<scope>test</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>javax.servlet</groupId> -->
<!-- jstl</artifactId> -->
<!-- </dependency> -->
</dependencies>
<build>
<!-- 打包后的jar包名稱 -->
<finalname>example</finalname>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<!-- 必須要的SpringBoot繼承的maven插件,缺少了無法打包jar。 -->
spring-boot-maven-plugin</artifactid>
<dependencies>
<!-- 在我們開發過程中,我們需要經常修改,為了避免重復啟動項目,我們可以啟用熱部署。 Spring-Loaded項目提供了強大的熱部署功能,
添加/刪除/修改 方法/字段/接口/枚舉 等代碼的時候都可以熱部署,速度很快,很方便。 想在Spring Boot中使用該功能非常簡單 ,就是在spring-boot-maven-plugin插件下面添加依賴: -->
<dependency>
<groupid>org.springframework</groupid>
springloaded</artifactid>
<version>
1.2
.
5
.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
|
3.在src/main/resource根目錄下進行配置h2數據庫。
schema.sql中建表。可以見多個表。用分號隔開
1
2
3
4
5
|
CREATE TABLE staff(
id
char
(
20
) not
null
primary key,
name
char
(
20
),
age INTEGER
);
|
data.sql 為新建的表進行初始化數據的操作。可以放入多個表的插入語句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
insert into staff values(
's01'
,
'張三'
,
26
);
insert into staff values(
's02'
,
'春天里asdglkj'
,
23
);
insert into staff values(
's03'
,
'劉三'
,
26
);
insert into staff values(
's04'
,
'萬里高空'
,
26
);
insert into staff values(
's05'
,
'火影'
,
26
);
insert into staff values(
's06'
,
'xiaopang'
,
26
);
insert into staff values(
's07'
,
'海賊王'
,
26
);
insert into staff values(
's08'
,
'王者榮耀'
,
26
)
|
application.properties db2數據庫設置和控制台現實設置等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#spring.datasource.url = jdbc:mysql:
//localhost:3306/zhanglfdatabase
#spring.datasource.username = root
#spring.datasource.password =
#spring.datasource.driverClassName = com.mysql.jdbc.Driver
#數據庫支持多種連接模式和連接設置,不同的連接模式和連接設置是通過不同的URL來區分的,URL中的設置是不區分大小寫。內存數據庫(私有)
#jdbc:h2:mem:
#內存數據庫(被命名)
#jdbc:h2:mem:<databasename>
#jdbc:h2:mem:test_mem
spring.datasource.url =jdbc:h2:mem:soa_service_api
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = org.h2.Driver
#進行該配置后,每次啟動程序,程序都會運行resources/schema.sql文件,對數據庫的結構進行操作,相當於新建一個表。
spring.datasource.schema=classpath:schema.sql
#進行該配置后,每次啟動程序,程序都會運行resources/data.sql文件,對數據庫的數據操作,相當於往表中插入數據。
spring.datasource.data=classpath:data.sql
# 數據庫類型聲明
spring.jpa.database = H2
# 是否開啟查詢語句在控制台打印
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
#開啟h2控制台功能和訪問地址。
spring.h2.console.enabled=
true
spring.h2.console.path=/h2-console</databasename>
這里有必要強調H2數據庫設置的屬性:
spring.datasource.url =jdbc:h2:mem:soa_service_api
|
嵌入式:
server.port=7900
#spring.session.store-type=none
## DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:h2:file:~/Desktop/ssdb;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO
#;AUTO_SERVER=TRUE
#;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=H2
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.connection-timeout=5000
#open shutdown
endpoints.shutdown.enabled=true
因為數據庫支持多種連接模式和連接設置,不同的連接模式和連接設置是通過不同的URL來區分的,URL中的設置是不區分大小寫。其中幾種常用的設置如下圖
前兩種對應的效果是:
1. jdbc:h2:file:E:/data/H2 表示將初始化的數據和H2 Console控制台執行的數據保存到E盤下data/H2文件夾中,即使應用重啟,數據不會丟失。
2. jdbc:h2:~/testdatabase這里就需要說明一下”~”這個符號在window操作系統下代表什么意思了,在Window操作系統下,”~”這個符號代表的就是當前登錄到操作系統的用戶對應的用戶目錄,所以testdatabase數據庫對應的文件存放在登錄到操作系統的用戶對應的用戶目錄當中,比如我當前是使用Administrator用戶登錄操作系統的,所以在”C:\Documents and Settings\Administrator.h2”目錄中就可以找到test數據庫對應的數據庫文件了
持久化本地的問題:由於本地已經存在表,而應用每次啟動都會創建表,導致下次啟動時會啟動報錯。除非手動注掉application.properties中新建表的配置,或則刪除本地對應目錄的文件。
3.jdbc:h2:mem:soa_service_api、jdbc:h2:mem:~/.h2/url類似與這種配置的,表示將初始化和h2 console控制台上操作的數據保存在內存(mem-memory)
保存到內存的問題:由於每次重啟應用內存釋放掉后,對應的數據也會消失,當然初始化的表+初始化數據就都沒了。然后重啟會從data.sql中重新初始化數據,啟動正常。但是你通過h2
console操作的其他數據則全部丟失。解決辦法是把在h2 console新添加的接口地址配置到data.sql中。然后重新啟動才行。
4.h2的配置說完,下面開始進行h2的測試代碼java文件部分,看看有沒有配置成功。我們用到JPA這個持久化的接口工具。
a.映射的pojo實體類-staffBo,注意點和說明都在代碼里了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
com.zlf.bo;
import
java.io.Serializable;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
import
javax.persistence.Table;
import
lombok.Data;
@Data
//可以省略get+set方法。
@Entity
@Table
(name=
"staff"
)
//注解的name屬性值要和schema.sql定義的表名一致。不然啟動雖然不報錯,但和表映射不上,就取不到任何值。
public
class
StaffBo
implements
Serializable {
private
static
final
long
serialVersionUID = 1L;
//主鍵是必須聲明的。不然啟動會報實體中無定義主鍵的錯: No identifier specified for entity: com.zlf.bo.StaffBo
@Id
private
String id;
// @Column(name="name") 如果表中字段名稱和這里的屬性名稱一樣,可以不同加Column注解。
private
String name;
@Column
(name=
"age"
)
private
int
age;
}
|
b.然后是類似與dao層接口的操作數據庫的接口,StaffRepository這個接口要繼承PagingAndSortingRepository才能實現對數據庫的crud操作。
1
2
3
4
5
6
|
package
com.zlf.repository;
import
org.springframework.data.repository.PagingAndSortingRepository;
import
com.zlf.bo.StaffBo;
public
interface
StaffRepository
extends
PagingAndSortingRepository<staffbo,string> {
}
</staffbo,string>
|
c. 然后就是service層的接口和實現類,在實現類中注入api接口的實例,並用實例操作數據庫,這里是h2數據庫。
1
2
3
4
5
6
7
|
package
com.zlf.service;
import
java.util.List;
import
com.zlf.bo.StaffBo;
public
interface
IStaffService {
public
List<staffbo> queryAllStaffList();
}
</staffbo>
|
這里我們只做了個查詢所有的操作。然后希望在頁面打印出來這些實體信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
com.zlf.service.impl;
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
import
com.zlf.bo.StaffBo;
import
com.zlf.repository.StaffRepository;
import
com.zlf.service.IStaffService;
@Service
public
class
StaffServiceImpl
implements
IStaffService {
@Autowired
private
StaffRepository staffRepository;
@Override
public
List<staffbo> queryAllStaffList() {
Iterable<staffbo> iterable = staffRepository.findAll();
List<staffbo> list=
new
ArrayList<staffbo>();
Iterator<staffbo> iterator = iterable.iterator();
while
(iterator.hasNext()){
StaffBo next = iterator.next();
list.add(next);
}
return
list;
}
}
</staffbo></staffbo></staffbo></staffbo></staffbo>
|
d. controller層
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package
com.zlf.controller;
import
java.util.List;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.ResponseBody;
import
com.zlf.bo.StaffBo;
import
com.zlf.service.IStaffService;
@Controller
@RequestMapping
(path=
"/staff"
,produces=
"application/json;charset=utf-8"
)
public
class
StaffController {
private
static
final
Logger logger=LoggerFactory.getLogger(StaffController.
class
);
@Autowired
private
IStaffService staffService;
@RequestMapping
(
"/getList"
)
@ResponseBody
public
List<staffbo> getAllList(){
List<staffbo> staffList=
null
;
try
{
staffList = staffService.queryAllStaffList();
}
catch
(Exception e) {
logger.error(
"查詢失敗"
);
}
return
staffList;
}
}
</staffbo></staffbo>
|
e. 重點來了–應用的啟動入口Application.java
這個java類最好放到和其他層級同級的根目錄中,這里就是com.zlf下,因為這個類的注解@SpringBootApplication會默認掃描與它同級目錄的其他文件。這樣才能完成注入等操作。如果你把它放到了和其他層的代碼一樣的級別中,則要用這種注解才行。
比如mianApplication下的Application.java,就是我說的另一種放到和其他層同級的結構中的情形。
它應該如何配置才能正常啟動呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package
mainApplication;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.context.annotation.ComponentScan;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
/**
* Spring Boot建議將我們main方法所在的這個主要的配置類配置在根包名下。
*
* @author Administrator
*
*
* @SpringBootApplication是Spring Boot提供的注解,他相當於加上如下注解:
* @Configuration,表明Application是一個Spring的配置對象,用於配置Spring應用上下文。
* @EnableAutoConfiguration,Spring Boot會根據類路徑(classpath)以及一些屬性值來自動完成一些配置行為,例如:開發基於Spring
* MVC的Web應用,需要在配置中加上
* @EnableWebMvc直接來激活一些默認的Web配置, 一旦Spring Boot發現運行時類路徑上包含了 spring-webmvc
* 依賴,它會自動的完成一個Web應用的基本配置
* ——例如配置DispatcherServlet等等。
* @ComponenScan告知Spring應用從什么位置去發現Spring構件(@Component, @Service,
* @Configuration)等等
*/
@SpringBootApplication
@Controller
// @RestController因為我們例子是寫一個web應用,因此寫的這個注解,這個注解相當於同時添加@Controller和@ResponseBody注解
// @EnableAutoConfiguration// Spring Boot會自動根據你jar包的依賴來自動配置項目的數據源依賴
@ComponentScan
(basePackages = {
"controller"
,
"service"
,
"dao"
})
// @ComponentScan路徑被默認設置為SampleController的同名package,也就是該package下的所有@Controller
// ,@Service , @Component, @Repository都會被實例化后並加入Spring Context中。這也是為什么要把這個類最好放到與其他包同級目錄 的原因了。
public
class
Application {
@RequestMapping
(
""
)
public
String home() {
// return "xiaozhang ,Hello World!";
return
"/index"
;
}
public
static
void
main(String[] args) {
// 啟動Spring Boot項目最簡單的方法就是執行下面的方法
SpringApplication.run(Application.
class
);
}
}
|
這樣就完成了測試代碼的簡單開發。在application.java中右鍵run as java application ,啟動程序。效果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ |
'_ | '
_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.
4.3
.RELEASE)
2017
-
07
-
05
20
:
50
:
07.579
INFO
5956
--- [ main] com.zlf.Application : Starting Application on pc-itwzhanglf02 with PID
5956
(E:\workspace\tsqa_springBoot2_hibernate_jpa\target\classes started by Administrator in E:\workspace\tsqa_springBoot2_hibernate_jpa)
2017
-
07
-
05
20
:
50
:
07.579
INFO
5956
--- [ main] com.zlf.Application : No active profile set, falling back to
default
profiles:
default
2017
-
07
-
05
20
:
50
:
07.657
INFO
5956
--- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
@50103bfb
: startup date [Wed Jul
05
20
:
50
:
07
CST
2017
]; root of context hierarchy
2017
-
07
-
05
20
:
50
:
09.655
INFO
5956
--- [ main] trationDelegate$BeanPostProcessorChecker : Bean
'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'
of type [
class
org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c3c549d9] is not eligible
for
getting processed by all BeanPostProcessors (
for
example: not eligible
for
auto-proxying)
2017
-
07
-
05
20
:
50
:
10.094
INFO
5956
--- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s):
8080
(http)
2017
-
07
-
05
20
:
50
:
10.110
INFO
5956
--- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017
-
07
-
05
20
:
50
:
10.110
INFO
5956
--- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/
7.0
.
72
2017
-
07
-
05
20
:
50
:
10.297
INFO
5956
--- [ost-startStop-
1
] org.apache.catalina.startup.TldConfig : At least one JAR was scanned
for
TLDs yet contained no TLDs. Enable debug logging
for
this
logger
for
a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017
-
07
-
05
20
:
50
:
10.297
INFO
5956
--- [ost-startStop-
1
] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017
-
07
-
05
20
:
50
:
10.297
INFO
5956
--- [ost-startStop-
1
] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in
2655
ms
2017
-
07
-
05
20
:
50
:
10.540
INFO
5956
--- [ost-startStop-
1
] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet:
'dispatcherServlet'
to [/]
2017
-
07
-
05
20
:
50
:
10.540
INFO
5956
--- [ost-startStop-
1
] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet:
'webServlet'
to [/h2-console
/*]
2017-07-05 20:50:10.540 INFO 5956 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-05 20:50:10.540 INFO 5956 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-05 20:50:10.540 INFO 5956 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-05 20:50:10.540 INFO 5956 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-05 20:50:10.961 INFO 5956 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [schema.sql]
2017-07-05 20:50:10.976 INFO 5956 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [schema.sql] in 15 ms.
2017-07-05 20:50:10.992 INFO 5956 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [data.sql]
2017-07-05 20:50:10.992 INFO 5956 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [data.sql] in 0 ms.
2017-07-05 20:50:11.118 INFO 5956 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-07-05 20:50:11.133 INFO 5956 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-07-05 20:50:11.227 INFO 5956 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2017-07-05 20:50:11.227 INFO 5956 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-07-05 20:50:11.227 INFO 5956 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-07-05 20:50:11.273 INFO 5956 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-07-05 20:50:11.398 INFO 5956 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-07-05 20:50:11.793 INFO 5956 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2017-07-05 20:50:11.840 INFO 5956 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-07-05 20:50:12.536 INFO 5956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50103bfb: startup date [Wed Jul 05 20:50:07 CST 2017]; root of context hierarchy
2017-07-05 20:50:12.626 INFO 5956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[]}" onto public java.lang.String com.zlf.Application.home()
2017-07-05 20:50:12.629 INFO 5956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/staff/getList],produces=[application/json;charset=utf-8]}" onto public java.util.List<com.zlf.bo.staffbo> com.zlf.controller.StaffController.getAllList()
2017-07-05 20:50:12.631 INFO 5956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.map<java.lang.string, java.lang.object="">> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-07-05 20:50:12.632 INFO 5956 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-07-05 20:50:12.670 INFO 5956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-05 20:50:12.670 INFO 5956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-05 20:50:12.733 INFO 5956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/
favicon.ico] onto handler of type [
class
org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017
-
07
-
05
20
:
50
:
13.232
INFO
5956
--- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans
for
JMX exposure on startup
2017
-
07
-
05
20
:
50
:
13.310
INFO
5956
--- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s):
8080
(http)
2017
-
07
-
05
20
:
50
:
13.310
INFO
5956
--- [ main] com.zlf.Application : Started Application in
6.213
seconds (JVM running
for
6.478
)
</java.util.map<java.lang.string,></com.zlf.bo.staffbo>
|
打開瀏覽器,訪問地址:http://localhost:8080/staff/getList,可以看到初始化的數據都出來了。
然后訪問地址:http://localhost:8080/h2-console出現下面的h2 console界面
在登陸頁面輸入在application.properties中配置的h2數據庫信息,登陸后可以看到左側已經有我們初始化的表,查詢數據,也能看到數據應初始化進來。則證明成功了!
以上所述是小編給大家介紹的springboot配置內存數據庫H2教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:
原文鏈接:http://www.2cto.com/database/201707/654694.html