SpringBoot入門文件配置

pom.xml配置html

<!-- 引入 springBoot 2.1.8 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>


    <dependencies>
        <!-- springBoot -web 整合 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- freemarker模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <!-- mybatis數據庫框架 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- mysql 數據庫鏈接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <!-- Server sql 數據庫鏈接-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.4.0.jre8</version>
        </dependency>
        <!-- 阿里巴巴json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>
        <!-- lombok能夠簡化get set 在類上面寫上@Data就行 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

application.yml 配置 多數據源java

spring:
  datasource:
    mes:
      jdbc-url: jdbc:mysql://localhost:3306/mes?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: *****
      driver-class-name: com.mysql.jdbc.Driver
    erp:
      jdbc-url: jdbc:mysql://localhost:3306/erp?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: *****
      driver-class-name: com.mysql.jdbc.Driver
    db:
      jdbc-url: jdbc:sqlserver://localhost:1433;DatabaseName=web
      username: sa
      password: *****
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  http:
    encoding:
      force: true
      charset: UTF-8
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html;charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: classpath:/templates
    settings:
      classic_compatible: true
server:
  port: 8888

數據庫Confmysql

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @program: publicweb
 * @description:
 * @author: HuangEnCi
 * @create: 2020-04-11 14:14
 */
@Configuration
@MapperScan(basePackages = "com.shineon.mapper.db",sqlSessionTemplateRef = DbDataSourceConfig.sqlSessionTemplate)
public class DbDataSourceConfig {
    //若是複製一個新的數據庫配置那麼只要改下面的常量和類上面的註解
    public static final String prefix="spring.datasource.db";//配置文件數據信息
    public static final String dataSource="dbDataSource";
    public static final String sqlSessionFactory="dbSqlSessionFactory";
    public static final String transactionManager="dbTransactionManager";
    public static final String sqlSessionTemplate="dbSqlSessionTemplate";

    /**
     * 建立 datasource
     * @return datasource
     */
    @Bean(name = dataSource)
    @ConfigurationProperties(prefix = prefix)
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    /**
     * 建立 sqlSessionFactory
     * @param dataSource1
     * @return  sqlSessionFactory
     * @throws Exception
     */
    @Bean(name = sqlSessionFactory)
    public SqlSessionFactory sqlSessionFactory(@Qualifier(dataSource) DataSource dataSource1) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource1);
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 建立事務管理器
     * @param dataSource1
     * @return
     */
    @Bean(name = transactionManager)
    public DataSourceTransactionManager transactionManager(@Qualifier(dataSource) DataSource dataSource1){
        return new DataSourceTransactionManager(dataSource1);
    }
    @Bean(name = sqlSessionTemplate)
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier(sqlSessionFactory) SqlSessionFactory sqlSessionFactory1){
        return new SqlSessionTemplate(sqlSessionFactory1);
    }
}

SpringBooot啓動類web

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @program: publicweb
 * @description: 這是springBoot 項目啓動類
 * @author: HuangEnCi
 * @create: 2020-04-10 15:30
 */
@SpringBootApplication
@Configuration
//@EnableScheduling
public class AppSpringBoot {
    public static void main(String[] args) {
        SpringApplication.run(AppSpringBoot.class,args);
    }
}
相關文章
相關標籤/搜索