SpringBoot依赖和依赖管理

 引入Jar包依赖管理,只做管理,并没有引入依赖

  
      org.springframework.boot
      spring-boot-starter-parent
      1.5.6.RELEASE
  

 Spring开发基本配置

 
      org.springframework.boot
      spring-boot-starter-web
  

改变JDK版本:


  		
  			
				org.apache.maven.plugins
				maven-compiler-plugin
				
				
					1.8
	1.8
				
			
  		
  

创建父工程:


      
          
          		org.springframework.boot
  				spring-boot-dependencies
  				1.5.6.RELEASE
  				pom
  				import
          
      
  
  
  
  		
  			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
				
			
      
  

 全局配置文件:

在resources目录下,使用一个全局的配置文件application.properties或者是application.yml

修改tomcat的端口为8088:

server.port=8088

 修改类路径:

server.context-path=/hello

 


组合注解

@SpringBootApplication

Log4J日志:



        org.springframework.boot
        spring-boot-starter
        
            
                org.springframework.boot
                spring-boot-starter-logging
            
        
         
    
        org.springframework.boot
        spring-boot-starter-log4j
        1.3.8.RELEASE
log4j.rootLogger=info,error,CONSOLE,DEBUG
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout     
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = info   
log4j.appender.info.append=true   
log4j.appender.info.File=e://springboot/api_services_info.log
log4j.logger.error=error  
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout     
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = error   
log4j.appender.error.append=true   
log4j.appender.error.File=e://springboot/api_services_error.log
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG   
log4j.appender.DEBUG.append=true   
log4j.appender.DEBUG.File=e://springboot/api_services_debug.log

@RestController
public class UserController{
private static Logger log = Logger.getLogger(UserController.class);
@Autowired
UserService userService;
@RequestMapping(“/add”)
public String addUser(){
      User u=new User(“zhangsan”,“12345”);
      int result = userService.addUser(u);
      log.info(“增加用户成功”+result);
      return “success”+result;
}
}

 

SpringBoot整合Mybatis(注解方式):


		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.1
	    
	   

		
			mysql
			mysql-connector-java
		

全局配置文件(application.properties )中添加如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/db1

 


SpringBoot实现文件上传:

 
        
            org.springframework.boot
            spring-boot-starter-web
        

      
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

 

全局配置文件中添加如下两个属性

maxFileSize:单文件最大
maxRequestSize:总上传数据大小

spring.servlet.multipart.enabled=true,是否支持 multipart 上传文件
spring.servlet.multipart.file-size-threshold=0,支持文件写入磁盘
spring.servlet.multipart.location=,上传文件的临时目录
spring.servlet.multipart.max-file-size=10Mb,最大支持文件大小
spring.servlet.multipart.max-request-size=10Mb,最大支持请求大小
spring.servlet.multipart.resolve-lazily=false,是否支持 multipart 上传文件时懒加载







SpringBoot整合Redis(单机版):

      
      		org.springframework.boot
      		spring-boot-starter-data-redis
      
#redis单服务器配置  
spring.redis.database=0  
spring.redis.host=192.168.25.128
spring.redis.port=6379
spring.redis.pool.max-active=8  
spring.redis.pool.max-wait=-1  
spring.redis.pool.max-idle=8  
spring.redis.pool.min-idle=0  
spring.redis.timeout=0  

SpringBoot整合Redis(集群版):


      
      		org.springframework.boot
      		spring-boot-starter-data-redis
      

spring.redis.cluster.nodes=192.168.22.12:6666

 


 对于静态资源css,js,img,SpringBoot一般默认放在classpath下的/static或/public文件夹,并且访问时,不用带static或public路径,直接可以取

spring.resources.static-locations=classpath:/static/

 一旦通过上面配置修改了静态文件存放路径,那么SpringBoot默认路径就不起作用


 


 

FR:徐海涛(hunk Xu)
QQ技术交流群:386476712

你可能感兴趣的:(SpringBoot)