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 "编程之美") 编程之美② 290714704 [立即加入](http://jq.qq.com/?_wv=1027&k=Sx4s4U "编程之美")
## 项目结构 ## 项目结构
##### 基础
springboot 版本 1.5.1.RELEASE springboot 版本 1.5.1.RELEASE
- springboot-helloworld<br> - 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<br>
Springboot 整合 Mybatis,数据库 MySQL Springboot 整合 Mybatis 的完整 Web 案例
- springboot-restful-json<br> - springboot-restful-json<br>
Springboot 实现 Restful 服务,基于 HTTP / 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 #### 修改数据库配置
##### 基本环境配置 配置文件地址springboot-mybatis/src/main/resources/application.properties
在 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 工程 在项目根目录 `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": "我的家在温岭。"
}
````

12
pom.xml
View File

@@ -1,18 +1,16 @@
<?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" <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"> 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> <modelVersion>4.0.0</modelVersion>
<parent> <name>springboot :: Examples</name>
<groupId>springdream</groupId>
<artifactId>springboot-learning-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>springboot</groupId> <groupId>springboot</groupId>
<artifactId>springboot</artifactId> <artifactId>springboot</artifactId>
<name>springboot :: Examples</name> <version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>springboot-helloworld</module> <module>springboot-helloworld</module>
<module>springboot-mybatis</module>
</modules> </modules>
</project> </project>

View File

@@ -12,7 +12,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version> <version>1.5.1.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>

59
springboot-mybatis/pom.xml Executable file
View File

@@ -0,0 +1,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>

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.spring.springboot.dao.CityDao">
<resultMap id="BaseResultMap" type="org.spring.springboot.domain.City">
<result column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" />
<result column="description" property="description" />
</resultMap>
<parameterMap id="City" type="org.spring.springboot.domain.City"/>
<sql id="Base_Column_List">
id, province_id, city_name, description
</sql>
<select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List" />
from city
where city_name = #{cityName}
</select>
</mapper>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="org.spring.springboot.domain"/>
</typeAliases>
<mappers>
<mapper resource="mapper/CityMapper.xml"/>
</mappers>
</configuration>