Springboot 整合 Mybatis 的完整 Web 案例

This commit is contained in:
JeffLi1993
2017-02-07 18:05:37 +08:00
committed by liqiangqiang
parent 8474f311fb
commit 52a7cde071
12 changed files with 325 additions and 37 deletions

View File

@@ -9,38 +9,61 @@ QQ 群<br>
编程之美② 290714704 [立即加入](http://jq.qq.com/?_wv=1027&k=Sx4s4U "编程之美")
## 项目结构
##### 基础
springboot 版本 1.5.1.RELEASE
- springboot-helloworld<br>
入门 HelloWorld 案例Quick Start - [Spring Boot 之 HelloWorld详解](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")<br>
入门 HelloWorld 案例Quick Start - [Spring Boot 之 HelloWorld 详解](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")<br>
##### Web 开发相关
- springboot-mybatis<br>
Springboot 整合 Mybatis,数据库 MySQL
Springboot 整合 Mybatis 的完整 Web 案例
- springboot-restful-json<br>
Springboot 实现 Restful 服务,基于 HTTP / JSON 传输
## 项目 Quick Start 快速开发指南
#### 基本环境配置
在 MySQL 中,创建数据库 springbootdb
````
CREATE DATABASE springbootdb;
````
创建表 city 城市 (因为我喜欢徒步)
````
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;
````
插入基础数据
````
INSERT city VALUES (1 ,1,'温岭市','BYSocket 的家在温岭。');
````
### 运行 springboot-mybatis 工程
首先 check 基本环境配置完成,创建好数据库和表。
## 项目 Quick Start
##### 基本环境配置
在 MySQL 中,创建数据库 bysocket
````
CREATE DATABASE bysocket;
````
创建表
````
DROP TABLE IF EXISTS `account_bind`;
CREATE TABLE `account_bind` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`buyer_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '有赞帐号ID',
`fans_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '粉丝ID',
`fans_type` smallint(2) unsigned NOT NULL DEFAULT '0' COMMENT '粉丝类型',
`nobody` varchar(50) NOT NULL DEFAULT '' COMMENT '用户随机hash',
`kdt_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '店铺ID',
`add_time` int(10) unsigned NOT NULL DEFAULT '0',
`update_time` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `fans_id_type` (`fans_id`,`fans_type`),
KEY `buyer_id` (`buyer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='帐号绑定关系表';
````
#### 修改数据库配置
配置文件地址springboot-mybatis/src/main/resources/application.properties
修改相应的数据源配置,比如账号、密码等
##### 运行 springboot-mybatis 工程
#### 编译工程
在项目根目录 `springboot-learning-example`,运行 maven 指令:
````
mvn clean install
````
#### 运行工程
右键运行工程包中 `org.spring.springboot.Application` Spring Boot 应用启动类的 main 函数,然后在浏览器访问:
`````
http://localhost:8080/api/city?cityName=温岭市
`````
可以看到返回的 JSON 结果:
````
{
"id": 1,
"provinceId": 1,
"cityName": "温岭市",
"description": "我的家在温岭。"
}
````