最新爆破专栏丨SpringBoot2(27),字节跳动学习笔记

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    database-platform: org.hibernate.dialect.MySQL5Dialect


**4. 创建数据库配置类**


**4.1 编写第一个数据库配置类**


这里我们先编写第一个数据库配置类,读取ds1中的数据库信息。



package com.yyg.boot.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description db1数据源配置类
 */
@ConfigurationProperties(prefix = “spring.datasource.ds1”)
@Component(“ds1Properties”)
@Data
public class Ds1Properties {

private String url;

private String username;

private String password;

private String driverClassName;

}


**4.2 编写第2个数据库配置类**


然后再编写第2个数据库配置类,读取ds2中的数据库信息。



package com.yyg.boot.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description db4数据源配置类
 */
@ConfigurationProperties(prefix = “spring.datasource.ds2”)
@Component(“ds2Properties”)
@Data
public class Ds2Properties {

private String url;

private String username;

private String password;

private String driverClassName;

}


**5. 注册数据源**


接下来我们在一个类中同时注册2个数据源就可以了,注意我们就是在这个配置类中分别关联加载2个数据源。



package com.yyg.boot.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description 数据源的配置类
 */
@Configuration
public class DataSourceConfig {

/**
     * 关联主数据源配置 ds1数据源
     */
    @Primary
    @Bean(name = “ds1Properties”)
    @ConfigurationProperties(prefix = “spring.datasource.ds1”)
    public DataSourceProperties ds1DataSourceProperties() {
        
        return new DataSourceProperties();
    }

/**
     * 初始化构建主数据源ds1数据源
     */
    @Primary
    @Bean(name = “ds1DataSource”)
    public DataSource ds1DataSource(@Qualifier(“ds1Properties”) DataSourceProperties dataSourceProperties) {
        //HikariDataSource",“org.apache.tomcat.jdbc.pool.DataSource”, "org.apache.commons.dbcp2.BasicDataSource
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

/**
     * 关联第二个ds2数据源配置
     */
    @Bean(name = “ds2Properties”)
    @ConfigurationProperties(prefix = “spring.datasource.ds2”)
    public DataSourceProperties ds2DataSourceProperties() {
        
        return new DataSourceProperties();
    }

/**
     * 初始化构建第二个ds2数据源
     */
    @Bean(“ds2DataSource”)
    public DataSource ds2DataSource(@Qualifier(“ds2Properties”) DataSourceProperties dataSourceProperties) {
        
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

}


**6. 配置数据源、连接工厂、事务管理器、扫描dao目录**


配置完2个数据源之后,我们还要利用事务管理器分别关联2个数据源。这里要注意合理的使用@Primary注解!


**6.1 配置第一个数据源管理器**


在这个数据源管理器中关联第1个数据源。



package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description 配置数据源、连接工厂、事务管理器、dao目录
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = “managerFactory1”, // 配置连接工厂
        transactionManagerRef = “transactionManager1”, // 配置事物管理器
        basePackages = {“com.yyg.boot.dao.db01”} // 设置dao所在位置

)
public class ManagerFactory01Config {

/**
     * 配置数据源,连接第1个数据源
     */
    @Autowired
    @Qualifier(“ds1DataSource”)
    private DataSource ds1DataSource;

@Primary
    @Bean(name = “managerFactory1”)
    public LocalContainerEntityManagerFactoryBean buildEntityManagerFactory1(EntityManagerFactoryBuilder builder) {
        return builder
                // 设置数据源
                .dataSource(ds1DataSource)
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages(“com.yyg.boot.entity”)
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
                .persistenceUnit(“ds1PersistenceUnit”)
                .build();

}

/**
     * 配置事务管理器
     */
    @Bean(name = “transactionManager1”)
    public PlatformTransactionManager transactionManagerDatabase1(EntityManagerFactoryBuilder builder) {
        
        return new JpaTransactionManager(buildEntityManagerFactory1(builder).getObject());
    }

}


**6.2 配置第2个数据源管理器**


在这个数据源管理器中关联第2个数据源。



package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description 配置数据源、连接工厂、事务管理器、dao目录
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = “managerFactory2”, // 配置连接工厂
        transactionManagerRef = “transactionManager2”, // 配置事物管理器
        basePackages = {“com.yyg.boot.dao.db02”} // 设置dao所在位置

)
public class ManagerFactory02Config {

/**
     * 配置数据源,连接第2个数据源
     */
    @Autowired
    @Qualifier(“ds2DataSource”)
    private DataSource ds2DataSource;

@Bean(name = “managerFactory2”)
    public LocalContainerEntityManagerFactoryBean buildEntityManagerFactory2(EntityManagerFactoryBuilder builder) {
        return builder
                // 设置数据源
                .dataSource(ds2DataSource)
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages(“com.yyg.boot.entity”)
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
                .persistenceUnit(“ds2PersistenceUnit”)
                .build();

}

/**
     * 配置事务管理器
     */
    @Bean(name = “transactionManager2”)
    public PlatformTransactionManager transactionManagerDatabase1(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(buildEntityManagerFactory2(builder).getObject());
    }

}


**7. 创建Entity实体类**


创建与数据库对应的实体类。



package com.yyg.boot.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description db1中的商品表
 */
@Entity
@Table(name = “goods”)
@Data
public class Goods {

@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

private String name;

}


**7.1 Goods商品类**


封装一个商品信息的实体类。



package com.yyg.boot.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description db1中的商品表
 */
@Entity
@Table(name = “goods”)
@Data
public class Goods {

@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

private String name;

}


**7.2 User用户类**


封装用户信息的实体类。



package com.yyg.boot.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/3
 * @Description db4中的用户表
 */
@Entity
@Table(name = “user”)
@Data
public class User {

@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

private String username;

private String birthday;

private String sex;

private String address;

}


**8. 创建Dao层代码**


**8.1 GoodsRepository类**


# 难道这样就够了吗?不,远远不够!

提前多熟悉阿里往年的面试题肯定是对面试有很大的帮助的,但是作为技术性职业,手里有实打实的技术才是你面对面试官最有用的利器,这是从内在散发出来的自信。

备战阿里时我花的最多的时间就是在学习技术上,占了我所有学习计划中的百分之70,这是一些我学习期间觉得还是很不错的一些学习笔记

我为什么要写这篇文章呢,其实我觉得学习是不能停下脚步的,在网络上和大家一起分享,一起讨论,不单单可以遇到更多一样的人,还可以扩大自己的眼界,学习到更多的技术,我还会在csdn、博客、掘金等网站上分享技术,这也是一种学习的方法。

今天就分享到这里了,谢谢大家的关注,以后会分享更多的干货给大家!

![阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服](https://img-blog.csdnimg.cn/img_convert/12d8c254a6c4c2adf8ac244ff4465876.webp?x-oss-process=image/format,png)

![阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服](https://img-blog.csdnimg.cn/img_convert/3e454155cf0ffb52d3e4eb763a394f20.webp?x-oss-process=image/format,png)

![image.png](https://img-blog.csdnimg.cn/img_convert/b23be780ebc2c04c11e18f96f8385077.webp?x-oss-process=image/format,png)





> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

可以扩大自己的眼界,学习到更多的技术,我还会在csdn、博客、掘金等网站上分享技术,这也是一种学习的方法。

今天就分享到这里了,谢谢大家的关注,以后会分享更多的干货给大家!

[外链图片转存中...(img-tgibxwTb-1715677316774)]

[外链图片转存中...(img-gVdl8WNs-1715677316775)]

[外链图片转存中...(img-yJwchl7T-1715677316775)]





> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 25
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值