一、運行 springboot-mybatis 工程
1
|
CREATE
DATABASE springbootdb; |
1
2
3
4
5
6
7
8
|
DROP
TABLE IF EXISTS `city`; CREATE
TABLE `city` ( ` id `
int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市編號' , `province_id`
int(10) unsigned NOT NULL COMMENT '省份編號' , `city_name`
varchar(25) DEFAULT NULL COMMENT '城市名稱' , `description`
varchar(25) DEFAULT NULL COMMENT '描述' , PRIMARY
KEY (` id `) )
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; |
1
|
INSERT
city VALUES (1 ,1, '溫嶺市' , 'BYSocket
的家在溫嶺。' ); |
org.spring.springboot.controller – Controller 層org.spring.springboot.dao – 數據操作層 DAOorg.spring.springboot.domain – 實體類org.spring.springboot.service – 業務邏輯層Application – 應用啓動類application.properties – 應用配置文件,應用啓動會自動讀取配置
1
|
mvn
clean install |
右鍵運行 Application 應用啓動類的 main 函數,然後在瀏覽器訪問:
1
|
http: //localhost :8080 /api/city ?cityName=溫嶺市 |
1
2
3
4
5
6
|
{
"id" :
1,
"provinceId" :
1,
"cityName" :
"溫嶺市" ,
"description" :
"我的家在溫嶺。" } |
二、springboot-mybatis 工程配置詳解
1
2
3
4
5
6
|
<!--
Spring Boot Mybatis 依賴 --> <dependency>
<groupId>org.mybatis.spring.boot< /groupId >
<artifactId>mybatis-spring-boot-starter< /artifactId >
<version>${mybatis-spring-boot}< /version > < /dependency > |
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
|
<?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>springboot< /groupId > <artifactId>springboot-mybatis< /artifactId > <version>0.0.1-SNAPSHOT< /version > <name>springboot-mybatis
:: 整合 Mybatis Demo< /name > <!--
Spring Boot 啓動父依賴 --> <parent> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter-parent< /artifactId > <version>1.5.1.RELEASE< /version > < /parent > <properties> <mybatis-spring-boot>1.2.0< /mybatis-spring-boot > <mysql-connector>5.1.39< /mysql-connector > < /properties > <dependencies> <!--
Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter-web< /artifactId > < /dependency > <!--
Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot< /groupId > <artifactId>spring-boot-starter- test < /artifactId > <scope> test < /scope > < /dependency > <!--
Spring Boot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot< /groupId > <artifactId>mybatis-spring-boot-starter< /artifactId > <version>${mybatis-spring-boot}< /version > < /dependency > <!--
MySQL 連接驅動依賴 --> <dependency> <groupId>mysql< /groupId > <artifactId>mysql-connector-java< /artifactId > <version>${mysql-connector}< /version > < /dependency > <!--
Junit --> <dependency> <groupId>junit< /groupId > <artifactId>junit< /artifactId > <version>4.12< /version > < /dependency > < /dependencies > < /project > |
1
2
3
|
##
Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain mybatis.mapperLocations=classpath:mapper/*.xml |
mybatis.config = mybatis 配置文件名稱mybatis.mapperLocations = mapper xml 文件地址
mybatis.typeAliasesPackage = 實體類包路徑mybatis.typeHandlersPackage = type handlers 處理器包路徑mybatis.check-config-location = 檢查 mybatis 配置是否存在,一般命名爲 mybatis-config.xml
mybatis.executorType = 執行模式。默認是 SIMPLE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** *
Spring Boot 應用啓動類 * *
Created by bysocket on 16 /4/26 . */ //
Spring Boot 應用的標識 @SpringBootApplication //
mapper 接口類掃描包配置 @MapperScan( "org.spring.springboot.dao" ) public
class Application {
public
static void main(String[] args) {
//
程序啓動入口
//
啓動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件
SpringApplication.run(Application.class,args);
} } |
City.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/** *
城市實體類 * *
Created by bysocket on 07 /02/2017 . */ public
class City { /** *
城市編號 */ private
Long id ; /** *
省份編號 */ private
Long provinceId; /** *
城市名稱 */ private
String cityName; /** *
描述 */ private
String description; public
Long getId() { return
id ; } public
void setId(Long id )
{ this. id
= id ; } public
Long getProvinceId() { return
provinceId; } public
void setProvinceId(Long provinceId) { this.provinceId
= provinceId; } public
String getCityName() { return
cityName; } public
void setCityName(String cityName) { this.cityName
= cityName; } public
String getDescription() { return
description; } public
void setDescription(String description) { this.description
= description; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** *
城市 DAO 接口類 * *
Created by bysocket on 07 /02/2017 . */ public
interface CityDao { /** *
根據城市名稱,查詢城市信息 * *
@param cityName 城市名 */ City
findByName(@Param( "cityName" )
String cityName); } |