git clone 下載工程 springboot-learning-example ,項目地址見 GitHub。下面開始運行工程步驟(Quick Start):java
a.建立數據庫 springbootdb:mysql
CREATE DATABASE springbootdb;
b.建立表 city :(由於我喜歡徒步)git
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;
c.插入數據web
INSERT city VALUES (1 ,1,'溫嶺市','BYSocket 的家在溫嶺。');
項目結構以下所示:spring
org.spring.springboot.controller - Controller 層 org.spring.springboot.dao - 數據操做層 DAO org.spring.springboot.domain - 實體類 org.spring.springboot.service - 業務邏輯層 Application - 應用啓動類 application.properties - 應用配置文件,應用啓動會自動讀取配置
打開 application.properties 文件, 修改相應的數據源配置,好比數據源地址、帳號、密碼等。(若是不是用 MySQL,自行添加鏈接驅動 pom,而後修改驅動名配置。)sql
在項目根目錄 springboot-learning-example,運行 maven 指令:數據庫
mvn clean install
右鍵運行 Application 應用啓動類的 main 函數,而後在瀏覽器訪問:apache
http://localhost:8080/api/city?cityName=溫嶺市
能夠看到返回的 JSON 結果:api
{ "id": 1, "provinceId": 1, "cityName": "溫嶺市", "description": "個人家在溫嶺。" }
<!-- Spring Boot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency>
整個工程的 pom.xml:瀏覽器
<?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>
# Mybatis 配置 mybatis.typeAliasesPackage=org.spring.springboot.domain mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.typeAliasesPackage 配置爲 org.spring.springboot.domain,指向實體類包路徑。mybatis.mapperLocations 配置爲 classpath 路徑下 mapper 包下,* 表明會掃描全部 xml 文件。
mybatis 其餘配置相關詳解以下:
mybatis.config = mybatis 配置文件名稱 mybatis.mapperLocations = mapper xml 文件地址 mybatis.typeAliasesPackage = 實體類包路徑 mybatis.typeHandlersPackage = type handlers 處理器包路徑 mybatis.check-config-location = 檢查 mybatis 配置是否存在,通常命名爲 mybatis-config.xml mybatis.executorType = 執行模式。默認是 SIMPLE
Application.java 代碼以下:
/** * 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); } }
mapper 接口類掃描包配置註解 MapperScan :用這個註解能夠註冊 Mybatis mapper 接口類。
City.java:
/** * 城市實體類 * * 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; } }
CityDao.java:
/** * 城市 DAO 接口類 * * Created by bysocket on 07/02/2017. */ public interface CityDao { /** * 根據城市名稱,查詢城市信息 * * @param cityName 城市名 */ City findByName(@Param("cityName") String cityName); }