0x1 environment
1. System: Windows 10
2. IDE:IntelliJ IDEA 2018.3 x64
0x2 creation project
1. Create a project SpringBoot
Select Spring Initailizr> Next
Simple configuration can> Next
Select the desired item rely> Next
Select the project workspace [write casually]: directory does not exist automatically creates> Next
Once it's created, idea will load dependencies, and pop-up prompts, Meven projects need to import dependence, select automatic pilot package
Wait a moment .... When the progress bar on the bottom right corner there is no good explanation
2. Create a project structure
These three files can be deleted
In order to create the directory: controller layer, domain layer (entity), mapper layer, query layer, service layer, is created like this ......
Note: This article does not use application.properties files use more concise application.yml file, you may have noticed, there are four yml project file which is why?
Because now there are a lot of environmental projects, development environment, test environment, quasi-production environment, production environment, different parameters for each environment, so we can put the configuration parameters for each environment to yml file
In Spring Boot multi environment configuration file names need to meet application- {profile} .yml format, wherein {profile} identification corresponding to your environment, such as:
application.yml: master file
application-dev.yml: development environment,
application-test.yml: Test Environment
application-prod.yml: the production environment
To load that file in the property set by spring.profiles.active application.yml file, which corresponds to the value {profile} value. such as
Create a database table
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. well-structured
①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 }
The author's note: In the entity classes override toString method helps troubleshoot --- <Alibaba Java Development Manual>
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 List<User> listAll(); 17 }
UserQuery.java query object
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: Interface layer interface-oriented thinking
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 List<User> 20 */ 21 List<User> listAll(); 22 }
Impl directory created at the service layer and create IUserService impl implementation class and implements the following directory override inherited methods
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 List<User> listAll() { 27 return userMapper.listAll(); 28 } 29 }
⑥ create mapper directory in the resources, and create UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="xyz.bit1024.sunny.mapper.UserMapper"> 4 <resultMap id="BaseResultMap" type="xyz.bit1024.sunny.domain.User"> 5 <result column="id" jdbcType="BIGINT" property="id" /> 6 <result column="userName" jdbcType="VARCHAR" property="userName" /> 7 <result column="passWord" jdbcType="VARCHAR" property="passWord" /> 8 <result column="realName" jdbcType="VARCHAR" property="realName" /> 9 </resultMap> 10 11 <sql id="Base_Column_List"> 12 id, userName, passWord, realName 13 </sql> 14 <sql id="Base_Column_List_Value"> 15 #{id,jdbcType=Long}, 16 #{userName,jdbcType=VARCHAR}, 17 #{passWord,jdbcType=VARCHAR}, 18 #{realName,jdbcType=VARCHAR} 19 </sql> 20 <select id="listAll" resultMap="BaseResultMap"> 21 select <include refid="Base_Column_List"/> from t_user 22 </select> 23 </mapper>
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 List<User> listAll(){ 29 return iUserService.listAll(); 30 } 31 }
4. perfect configuration
① scan package configuration Mapper
application-dev.yml
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_HOME" value="./logs" /> <property name="APP_NAME" value="bit1024" /> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/${APP_NAME}.info.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>30</maxHistory> <totalSizeCap>6GB</totalSizeCap> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="warnFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/${APP_NAME}.warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>30</maxHistory> <totalSizeCap>6GB</totalSizeCap> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="errorFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/${APP_NAME}.error.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>30</maxHistory> <totalSizeCap>6GB</totalSizeCap> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <-! Myibatis Logging Configuration -> <logger name="xyz.bit1024.sunny.mapper" level="INFO"/> <logger name="xyz.bit1024" level="DEBUG"/> <! - log output level -> <root level="INFO"> <appender-ref ref="CONSOLE"></appender-ref> <appender-ref ref="infoFile"></appender-ref> <appender-ref ref="warnFile"></appender-ref> <appender-ref ref="errorFile"></appender-ref> </root> </configuration>
Structure test data
INSERT INTO `springboot`.`t_user` (` id`, `userName`,` passWord`, `realName`) VALUES ( '10000001', 'admin', 'admin', ' super administrator' ); 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');
All completed only be tested
Use PostMan test wave
Success, and perfect
Pit:
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.
Modify the configuration file:
url: jdbc: mysql: // localhost :? 3306 / springboot useUnicode = & characterEncoding = utf-8 & useSSL = true & serverTimezone = UTC true
this integration even finished ,, Available also integrated the Log + shiro
Have not used before GitHub: https: //gist.github.com/bit1024-404/38f42bef407a7609e324eb00147ec5c1