• 数据库建立用户表初步结构

CREATE TABLE `user` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '姓名',
`phone` char(13) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
`passwd` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '经过MD5加密的密码',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `idx_phone`(`phone`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

  • idea搭建springboot项目,推送github
  • 搭建目录结构

  • 连接数据库,引入mybatis和mysql依赖和添加配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

<!-- MySQL的JDBC驱动包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- 引入第三方数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
spring.datasource.url=jdbc:mysql://xx.xx.xxx.xxx:3306/house?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =root
#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
#Druid 是阿里巴巴开源平台上一个数据库连接池实现
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
  • 引入Swagger2,加上Swagger2Config类  
    访问路径:http://localhost:8090/swagger-ui.html
<!--Swagger-UI API文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
  • 注意点:
  1. id采用自增,uuid或者Twitter的分布式自增ID算法snowflake? 为了方便暂时采用AUTO_INCREMENT自增
  2. 手机验证码存储在redis中
  3. 数据库连接采用mybatis,在mapper层要用@mapper注解(@mapper注解是mybatis3.4版本以后出来的,在没有@mapper的时候应该就是用@repository注解,使用@repository要自己写对应dao的mapper配置文件)或者在启动类上加@MapperScan("com.example.house.mapper")  
  4. mybatis注解:@mapper,@MapperScan,@Insert,@Select,@Update,@Delete,@Results,@Options
  5. @Options:插入数据后自动获取到该主键值