diff --git a/README.md b/README.md index f4fccc5..3368690 100644 --- a/README.md +++ b/README.md @@ -9,38 +9,61 @@ QQ 群
编程之美② 290714704 [立即加入](http://jq.qq.com/?_wv=1027&k=Sx4s4U "编程之美") ## 项目结构 +##### 基础 springboot 版本 1.5.1.RELEASE - springboot-helloworld
-入门 HelloWorld 案例,Quick Start - [Spring Boot 之 HelloWorld详解](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")
+入门 HelloWorld 案例,Quick Start - [Spring Boot 之 HelloWorld 详解](http://www.bysocket.com/?p=1124 "Spring Boot 之 HelloWorld详解")
+ +##### Web 开发相关 - springboot-mybatis
-Springboot 整合 Mybatis,数据库 MySQL +Springboot 整合 Mybatis 的完整 Web 案例 - springboot-restful-json
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": "我的家在温岭。" +} +```` \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7aa5304..e52d442 100644 --- a/pom.xml +++ b/pom.xml @@ -1,18 +1,16 @@ - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - - springdream - springboot-learning-example - 0.0.1-SNAPSHOT - + springboot :: Examples + springboot springboot - springboot :: Examples + 1.0-SNAPSHOT pom springboot-helloworld + springboot-mybatis - + + \ No newline at end of file diff --git a/springboot-helloworld/pom.xml b/springboot-helloworld/pom.xml index 84f6fb8..ca3bda2 100755 --- a/springboot-helloworld/pom.xml +++ b/springboot-helloworld/pom.xml @@ -12,7 +12,7 @@ org.springframework.boot spring-boot-starter-parent - 1.3.3.RELEASE + 1.5.1.RELEASE diff --git a/springboot-mybatis/pom.xml b/springboot-mybatis/pom.xml new file mode 100755 index 0000000..f309b20 --- /dev/null +++ b/springboot-mybatis/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + springboot + springboot-mybatis + 0.0.1-SNAPSHOT + springboot-mybatis :: 整合 Mybatis Demo + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + 1.2.0 + 5.1.39 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis-spring-boot} + + + + + mysql + mysql-connector-java + ${mysql-connector} + + + + + junit + junit + 4.12 + + + diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/Application.java b/springboot-mybatis/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..b990fc0 --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,25 @@ +package org.spring.springboot; + +import org.mybatis.spring.annotation.MapperScan; +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 16/4/26. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +@MapperScan("org.spring.springboot.dao") +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/controller/CityRestController.java b/springboot-mybatis/src/main/java/org/spring/springboot/controller/CityRestController.java new file mode 100644 index 0000000..9ba5bde --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/controller/CityRestController.java @@ -0,0 +1,26 @@ +package org.spring.springboot.controller; + +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by bysocket on 07/02/2017. + */ +@RestController +public class CityRestController { + + @Autowired + private CityService cityService; + + @RequestMapping(value = "/api/city", method = RequestMethod.GET) + public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) { + return cityService.findCityByName(cityName); + } + +} diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/dao/CityDao.java b/springboot-mybatis/src/main/java/org/spring/springboot/dao/CityDao.java new file mode 100644 index 0000000..918eab1 --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/dao/CityDao.java @@ -0,0 +1,19 @@ +package org.spring.springboot.dao; + +import org.apache.ibatis.annotations.Param; +import org.spring.springboot.domain.City; + +/** + * 城市 DAO 接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityDao { + + /** + * 根据城市名称,查询城市信息 + * + * @param cityName 城市名 + */ + City findByName(@Param("cityName") String cityName); +} diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/domain/City.java b/springboot-mybatis/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..556709c --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,61 @@ +package org.spring.springboot.domain; + +/** + * 城市实体类 + * + * 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; + } +} diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/service/CityService.java b/springboot-mybatis/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..b3a6801 --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/service/CityService.java @@ -0,0 +1,17 @@ +package org.spring.springboot.service; + +import org.spring.springboot.domain.City; + +/** + * 城市业务逻辑接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityService { + + /** + * 根据城市名称,查询城市信息 + * @param cityName + */ + City findCityByName(String cityName); +} diff --git a/springboot-mybatis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-mybatis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..a85a5d7 --- /dev/null +++ b/springboot-mybatis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -0,0 +1,24 @@ +package org.spring.springboot.service.impl; + +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * 城市业务逻辑实现类 + * + * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + @Autowired + private CityDao cityDao; + + public City findCityByName(String cityName) { + return cityDao.findByName(cityName); + } + +} diff --git a/springboot-mybatis/src/main/resources/mapper/CityMapper.xml b/springboot-mybatis/src/main/resources/mapper/CityMapper.xml new file mode 100644 index 0000000..82a07d1 --- /dev/null +++ b/springboot-mybatis/src/main/resources/mapper/CityMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + id, province_id, city_name, description + + + + + diff --git a/springboot-mybatis/src/main/resources/mybatis-config.xml b/springboot-mybatis/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..df395d8 --- /dev/null +++ b/springboot-mybatis/src/main/resources/mybatis-config.xml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file