SpringBoot的配置文件和注解

SpringBoot

创建maven工程

在pom.xml中导入相应的依赖





    4.0.0

    com.shan
    ShanOneZzzfSSHBoot
    1.0-SNAPSHOT
            
    jar

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


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

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

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        

        
                
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
        
            mysql
            mysql-connector-java
            8.0.15
        

        
             
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.12
        

        
        
            com.alibaba
            druid
            1.1.17
        


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

        
        
            org.springframework.session
            spring-session-data-redis
        
                
        
            org.projectlombok
            lombok
            1.18.2
        
        
              
        
            com.alibaba
            fastjson
            1.2.60
        

        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
        
        
        
            io.springfox
            springfox-swagger2
            2.9.1
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.1
        

    

    
        
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    true
                    true
                
            
        
    


 

添加application.yml或者application.properties全局配置文件

              application.yml的简单配置

#格式要正确,不能有多余的空格或缩进
#配置tomcat端口号、和虚拟路径
server:
  port: 8080
  servlet:
    context-path: /boot
#配置SpringMVC,下面配置多个数据库需要添加  --- 用于标识不同的配置
spring:
  mvc:
    servlet:
      load-on-startup: 1
  application:
    name: springbootdemo
  profiles:
    active: local
  aop:
    auto: true
    proxy-target-class: false
#配置数据源
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: true
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j2
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
#配置mybatis
mybatis:
  type-aliases-package: com.pro.conf.domain.*
  mapper-locations: classpath:/mapper/**/*.xml
  configuration:
    map-underscore-to-camel-case: true
    use-generated-keys: true
    default-fetch-size: 100
    default-statement-timeout: 60



---
spring:
  profiles: local
  datasource:
    ddyj:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/ddyj?serverTimezone=Asia/Shanghai&useSSL=false
    test:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    pass:



---
spring:
  profiles: staging
  datasource:
    ddyj:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/ddyj?serverTimezone=Asia/Shanghai&useSSL=false
    test:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false
  redis:
    database: 3
    host: 127.0.0.1
    port: 6379
    pass:


---
spring:
  profiles: prod
  datasource:
    ddyj:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/ddyj?serverTimezone=Asia/Shanghai&useSSL=false
    test:
      username: root
      password: 123456
      url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false
  redis:
    database: 3
    host: 127.0.0.1
    port: 6379
    pass:

log4j2和逆向工程的配置文件

log4j2.xml配置



   
      /data/serviceLogs/boot-service
      info
      error
      debug
   
   
      
         
         
      
      
         
         
         
            
            
         
         
      
      
         
         
         
            
            
         
         
         
      
   
   

      
      

      
         
         
         
      

      
         
         
         
      
   

逆向工程配置文件 generatorConfig.xml





    

    
    
    
    

    
        
            
        

        
        
            
            
        

        
            
        

        
        
            
            
        

        
        
            
        

        
        
        
        
        
        
            
        

        

     
       

    

配置文件的位置

SpringBoot的配置文件和注解_第1张图片

1、启动器代码

package com.pro;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@SpringBootApplication  //开启SpringBootApplication
@EnableRedisHttpSession  //开启Redis共享
@MapperScan("com.pro.mapper")  //扫描mapper包
public class StartPro {
    public static void main(String[] args) {
        SpringApplication.run(StartPro.class,args);  //启动
    }
}

2、控制层Controller层的一般配置

import com.pro.request.LoginParams;
import com.pro.result.ResponseData;
import com.pro.result.ResponseResult;
import com.pro.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/loginController")
public class LoginController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/userLogin")
    @ResponseBody
    public ResponseResult userLogin(LoginParams params, HttpSession session){
        ResponseResult responseResult = userService.userLogin(params);
        Integer code = responseResult.getCode();
        if (code == 200){
            session.setAttribute("userInfo", responseResult.getData());
            return ResponseResult.success("登陆成功");
        }
        return ResponseResult.fail(1001,"登陆失败");
    }
}

3、服务层service.impl层的配置

import com.alibaba.fastjson.JSONObject;
import com.pro.conf.database.DatabaseType;
import com.pro.conf.database.DynamicDataSource;
import com.pro.domain.Student;
import com.pro.domain.User;
import com.pro.mapper.StudentMapper;
import com.pro.mapper.UserMapper;
import com.pro.result.ResponseResult;
import com.pro.service.IDataSourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class DataSourceServiceImpl implements IDataSourceService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private UserMapper userMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public ResponseResult dataSourceSwitch() {
        //在默认数据源下,查询当前数据
        User user = userMapper.selectByPrimaryKey(5);
        log.info("dataSourceSwitch::::{}", JSONObject.toJSONString(user));
        //查询test下的student信息
        DynamicDataSource.setDataSourceType(DatabaseType.test);
        Student student = studentMapper.selectByPrimaryKey(4);
        log.info("dataSource(Student):{}",JSONObject.toJSONString(student));
        DynamicDataSource.setDataSourceType(DatabaseType.ddyj);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user",user);
        jsonObject.put("student",student);
        return ResponseResult.success(jsonObject);
    }
}

4、domain层和mapper层不用添加注解

 

你可能感兴趣的:(Spring)