0x1 环境
1. 系统:Windows 10
2. IDE:IntelliJ IDEA 2018.3 x64
0x2 创建项目
1.创建一个SpringBoot项目
选择Spring Initailizr >Next
简单配置即可>Next
选择项目所需依赖>Next
选择项目工作空间[随便写]:目录不存在会自动创建 > Next
创建好后,idea就会加载依赖包,并弹出提示,Meven项目需要导入依赖,选择自动导包
等待一会....当右下角没有进度条就说明好了
2. 创建项目结构
这三个文件可以删除
依次创建目录:controller层,domain层(entity),mapper层,query层,service层,创建好就是这样子......
注意:本文不使用application.properties文件 而使用更加简洁的application.yml文件,可能你们已经注意到了项目中有四个 yml文件这是为什么?
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到 yml文件中
在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application.yml:主文件
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
需要加载那个文件就在在 application.yml 文件中通过spring.profiles.active属性来设置,其值对应{profile}值。比如
创建数据库表
CREATE TABLE `user` ( `id` BIGINT(32) NOT NULL AUTO_INCREMENT, `userName` varchar(32) NOT NULL, `passWord` varchar(50) NOT NULL, `realName` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
3. 完善结构
①User.java
1 package xyz.bit1024.sunny.domain; 2 3 /** 4 * @ClassName User 5 * @Date 2019-06-15 16:14 6 * @Author xiaozhi 7 * @Version 1.0.0 8 * @Since JDK 1.8 9 * @Description 实体类 10 */ 11 public class User { 12 private Long id; 13 private String userName; 14 private String passWord; 15 private String realName; 16 17 public Long getId() { 18 return id; 19 } 20 21 public void setId(Long id) { 22 this.id = id; 23 } 24 25 public String getUserName() { 26 return userName; 27 } 28 29 public void setUserName(String userName) { 30 this.userName = userName; 31 } 32 33 public String getPassWord() { 34 return passWord; 35 } 36 37 public void setPassWord(String passWord) { 38 this.passWord = passWord; 39 } 40 41 public String getRealName() { 42 return realName; 43 } 44 45 public void setRealName(String realName) { 46 this.realName = realName; 47 } 48 49 @Override 50 public String toString() { 51 return "User{" + 52 "id=" + id + 53 ", userName='" + userName + '\'' + 54 ", passWord='" + passWord + '\'' + 55 ", realName='" + realName + '\'' + 56 '}'; 57 } 58 }
笔者按:在实体类中覆写toString方法有助于查错---<阿里巴巴Java开发手册>
UserMapper.java
1 package xyz.bit1024.sunny.mapper; 2 3 import xyz.bit1024.sunny.domain.User; 4 5 import java.util.List; 6 7 /** 8 * @ClassName UserMapper 9 * @Date 2019-06-15 16:21 10 * @Author xiaozhi 11 * @Version 1.0.0 12 * @Since JDK 1.8 13 * @Description 14 */ 15 public interface UserMapper { 16 ListlistAll(); 17 }
UserQuery.java 查询对象
1 package xyz.bit1024.sunny.query; 2 3 /** 4 * @ClassName UserQuery 5 * @Date 2019-06-15 16:28 6 * @Author xiaozhi 7 * @Version 1.0.0 8 * @Since JDK 1.8 9 * @Description 10 */ 11 public class UserQuery { 12 }
IUserService.java:接口层,面向接口思想
1 package xyz.bit1024.sunny.service; 2 3 import xyz.bit1024.sunny.domain.User; 4 5 import java.util.List; 6 7 /** 8 * @ClassName IUserService 9 * @Date 2019-06-15 16:17 10 * @Author xiaozhi 11 * @Version 1.0.0 12 * @Since JDK 1.8 13 * @Description 14 */ 15 public interface IUserService { 16 17 /** 18 * 全部用户 19 * @return List20 */ 21 List listAll(); 22 }
在service 层下创建 impl 目录,并在impl目录下面创建 IUserService 实现类并实现覆写父类方法
UserServiceImpl.java
1 package xyz.bit1024.sunny.service.impl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 import xyz.bit1024.sunny.domain.User; 6 import xyz.bit1024.sunny.mapper.UserMapper; 7 import xyz.bit1024.sunny.service.IUserService; 8 9 import java.util.List; 10 11 /** 12 * @ClassName UserServiceImpl 13 * @Date 2019-06-15 16:19 14 * @Author xiaozhi 15 * @Version 1.0.0 16 * @Since JDK 1.8 17 * @Description 实现类[业务类] 18 */ 19 @Service 20 public class UserServiceImpl implements IUserService { 21 22 @Autowired 23 private UserMapper userMapper; 24 25 @Override 26 public ListlistAll() { 27 return userMapper.listAll(); 28 } 29 }
⑥ 在resources 创建mapper目录,并创建 UserMapper.xml
1 2 34 5 10 116 7 8 9 12 id, userName, passWord, realName 13 1415 #{id,jdbcType=Long}, 16 #{userName,jdbcType=VARCHAR}, 17 #{passWord,jdbcType=VARCHAR}, 18 #{realName,jdbcType=VARCHAR} 19 20 23
UserController.java
1 package xyz.bit1024.sunny.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 import xyz.bit1024.sunny.domain.User; 7 import xyz.bit1024.sunny.service.IUserService; 8 9 import java.util.List; 10 11 /** 12 * @ClassName UserController 13 * @Date 2019-06-15 16:16 14 * @Author xiaozhi 15 * @Version 1.0.0 16 * @Since JDK 1.8 17 * @Description 18 */ 19 @RestController 20 @RequestMapping("/user") 21 public class UserController { 22 23 //注入Service对象 24 @Autowired 25 private IUserService iUserService; 26 27 @RequestMapping("/listAll") 28 public ListlistAll(){ 29 return iUserService.listAll(); 30 } 31 }
4 .完善配置
① 配置Mapper扫描包
application-dev.yml
logback.xml
class="ch.qos.logback.core.ConsoleAppender"> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n UTF-8 class="ch.qos.logback.core.rolling.RollingFileAppender"> class="ch.qos.logback.classic.filter.LevelFilter"> INFO ACCEPT DENY class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> ${LOG_HOME}/${APP_NAME}.info.%d{yyyy-MM-dd}.%i.log 10MB 30 6GB class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8 class="ch.qos.logback.core.rolling.RollingFileAppender"> class="ch.qos.logback.classic.filter.LevelFilter"> WARN ACCEPT DENY class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> ${LOG_HOME}/${APP_NAME}.warn.%d{yyyy-MM-dd}.%i.log 10MB 30 6GB class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8 class="ch.qos.logback.core.rolling.RollingFileAppender"> class="ch.qos.logback.classic.filter.LevelFilter"> ERROR ACCEPT DENY class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> ${LOG_HOME}/${APP_NAME}.error.%d{yyyy-MM-dd}.%i.log 10MB 30 6GB class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8
构造测试数据
INSERT INTO `springboot`.`t_user` (`id`, `userName`, `passWord`, `realName`) VALUES ('10000001', 'admin', 'admin', '超级管理员'); INSERT INTO `springboot`.`t_user` (`id`, `userName`, `passWord`, `realName`) VALUES ('10000002', 'user1', '123', '用户1'); INSERT INTO `springboot`.`t_user` (`id`, `userName`, `passWord`, `realName`) VALUES ('10000003', 'user2', '123', '用户2');
一切完毕只待测试
使用PostMan测试一波
成功,,完美
采坑:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
修改配置文件:
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
这次的集成就算完了,,有空把登录+shiro也集成
之前也没用过 GitHub :https://gist.github.com/bit1024-404/38f42bef407a7609e324eb00147ec5c1
转载请注明出处!