您当前的位置:首页 > 电脑百科 > 程序开发 > 架构

SpringBoot实现多数据源配置详解

时间:2023-09-07 12:59:30  来源:Springboot实战案例锦集  作者:



开发环境:JDK1.8+SpringBoot2.4.12+Oracle

这里我们假设要使用两个数据源分别为:master和slave。

  • pom.xml 依赖包
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>com.Github.noraui</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
  </dependency>
  <dependency>
    <groupId>org.Apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
  </dependency>
  <dependency>
    <groupId>org.MyBatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
  </dependency>
</dependencies>
  • Application.yml配置
server:
  port: 50000 
---
spring:
  jpa: 
    hibernate:  
      ddlAuto: update
    openInView: true
    showSql: false
    databasePlatform: org.hibernate.dialect.Oracle10gDialect
---
# 第一个数据源      
master: 
  datasource:
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521/orcl
    username: t0
    password: t0
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimumIdle: 10
      maximumPoolSize: 200
      autoCommit: true
      idleTimeout: 30000
      poolName: MasterDatabookHikariCP
      maxLifetime: 1800000
      connectionTimeout: 30000
      connectionTestQuery: SELECT 1 FROM DUAL
# 第二个数据源      
slave:
  datasource:
    driverClassName: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521/orcl
    username: t1
    password: t1
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimumIdle: 10
      maximumPoolSize: 200
      autoCommit: true
      idleTimeout: 30000
      poolName: SlaveDatabookHikariCP
      maxLifetime: 1800000
      connectionTimeout: 30000
      connectionTestQuery: SELECT 1 FROM DUAL      
---
# mybatis 配置,分表对应到不同的包中        
master:
  mybatis:
    config-location: classpath:/MyBatis-conf.xml
    type-aliases-package: com.pack.domAIn #master数据源对应的包
    mapper-locations:
    - classpath:/com/pack/mapper/oracle/*.xml #master数据源对应mapper文件
slave:
  mybatis:
    config-location: classpath:/MyBatis-conf.xml
    type-aliases-package: com.pack.slave.domain #slave数据源对应的包
    mapper-locations:
    - classpath:/com/pack/slave/mapper/oracle/*.xml    #slave数据源对应mapper文件
---
# jpa相关的配置
master:
  jpa:
    repos: com.pack.base.repository #master数据源对应的包配置
    domain: com.pack.domain #master对应的实体包
slave:
  jpa:
    repos: com.pack.slave.repository #salve数据源对应的包配置
    domain: com.pack.slave.domain  #slave对应的实体包

以上就是两个数据源对应相关的配置了,大家注意看里面的注释。接下来我们看mabatis和jpa对应的类相关的配置了,都是固定的配置。

  • 数据源属性配置对应的JAVA类配置

BaseProperties类

public class BaseDataSourceProperties implements BeanClassLoaderAware, InitializingBean {

  private ClassLoader classLoader;
  private String name;
  private boolean generateUniqueName;
  private Class<? extends DataSource> type;
  private String driverClassName;
  private String url;
  private String username;
  private String password;
  private String jndiName;
  private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;
  private String platform = "all";
  private List<String> schema;
  private String schemaUsername;
  private String schemaPassword;
  private List<String> data;
  private String dataUsername;
  private String dataPassword;
  private boolean continueOnError = false;
  private String separator = ";";
  private Charset sqlScriptEncoding;


  private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;


  private Xa xa = new Xa();


  private String uniqueName;


  @Override
  public void setBeanClassLoader(ClassLoader classLoader) {
    this.classLoader = classLoader;
  }


  @Override
  public void afterPropertiesSet() throws Exception {
    this.embeddedDatabaseConnection = EmbeddedDatabaseConnection
        .get(this.classLoader);
  }
  public DataSourceBuilder<?> initializeDataSourceBuilder() {
    return DataSourceBuilder.create(getClassLoader()).type(getType())
        .driverClassName(determineDriverClassName()).url(determineUrl())
        .username(determineUsername()).password(determinePassword());
  }


  public String getName() {
    return this.name;
  }


  public void setName(String name) {
    this.name = name;
  }


  public boolean isGenerateUniqueName() {
    return this.generateUniqueName;
  }


  public void setGenerateUniqueName(boolean generateUniqueName) {
    this.generateUniqueName = generateUniqueName;
  }


  public Class<? extends DataSource> getType() {
    return this.type;
  }


  public void setType(Class<? extends DataSource> type) {
    this.type = type;
  }
  public String getDriverClassName() {
    return this.driverClassName;
  }


  public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
  }
  public String determineDriverClassName() {
    if (StringUtils.hasText(this.driverClassName)) {
      Assert.state(driverClassIsLoadable(),
          () -> "Cannot load driver class: " + this.driverClassName);
      return this.driverClassName;
    }
    String driverClassName = null;
    if (StringUtils.hasText(this.url)) {
      driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName();
    }
    if (!StringUtils.hasText(driverClassName)) {
      driverClassName = this.embeddedDatabaseConnection.getDriverClassName();
    }
    if (!StringUtils.hasText(driverClassName)) {
      throw new DataSourceBeanCreationException(
          "Failed to determine a suitable driver class", this,
          this.embeddedDatabaseConnection);
    }
    return driverClassName;
  }


  private boolean driverClassIsLoadable() {
    try {
      ClassUtils.forName(this.driverClassName, null);
      return true;
    }
    catch (UnsupportedClassVersionError ex) {
      // Driver library has been compiled with a later JDK, propagate error
      throw ex;
    }
    catch (Throwable ex) {
      return false;
    }
  }
  public String getUrl() {
    return this.url;
  }


  public void setUrl(String url) {
    this.url = url;
  }
  public String determineUrl() {
    if (StringUtils.hasText(this.url)) {
      return this.url;
    }
    String databaseName = determineDatabaseName();
    String url = (databaseName != null)
        ? this.embeddedDatabaseConnection.getUrl(databaseName) : null;
    if (!StringUtils.hasText(url)) {
      throw new DataSourceBeanCreationException(
          "Failed to determine suitable jdbc url", this,
          this.embeddedDatabaseConnection);
    }
    return url;
  }
  public String determineDatabaseName() {
    if (this.generateUniqueName) {
      if (this.uniqueName == null) {
        this.uniqueName = UUID.randomUUID().toString();
      }
      return this.uniqueName;
    }
    if (StringUtils.hasLength(this.name)) {
      return this.name;
    }
    if (this.embeddedDatabaseConnection != EmbeddedDatabaseConnection.NONE) {
      return "testdb";
    }
    return null;
  }
  public String getUsername() {
    return this.username;
  }


  public void setUsername(String username) {
    this.username = username;
  }
  public String determineUsername() {
    if (StringUtils.hasText(this.username)) {
      return this.username;
    }
    if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
      return "sa";
    }
    return null;
  }
  public String getPassword() {
    return this.password;
  }


  public void setPassword(String password) {
    this.password = password;
  }
  public String determinePassword() {
    if (StringUtils.hasText(this.password)) {
      return this.password;
    }
    if (EmbeddedDatabaseConnection.isEmbedded(determineDriverClassName())) {
      return "";
    }
    return null;
  }


  public String getJndiName() {
    return this.jndiName;
  }
  public void setJndiName(String jndiName) {
    this.jndiName = jndiName;
  }


  public DataSourceInitializationMode getInitializationMode() {
    return this.initializationMode;
  }


  public void setInitializationMode(DataSourceInitializationMode initializationMode) {
    this.initializationMode = initializationMode;
  }


  public String getPlatform() {
    return this.platform;
  }


  public void setPlatform(String platform) {
    this.platform = platform;
  }


  public List<String> getSchema() {
    return this.schema;
  }


  public void setSchema(List<String> schema) {
    this.schema = schema;
  }


  public String getSchemaUsername() {
    return this.schemaUsername;
  }


  public void setSchemaUsername(String schemaUsername) {
    this.schemaUsername = schemaUsername;
  }


  public String getSchemaPassword() {
    return this.schemaPassword;
  }


  public void setSchemaPassword(String schemaPassword) {
    this.schemaPassword = schemaPassword;
  }


  public List<String> getData() {
    return this.data;
  }


  public void setData(List<String> data) {
    this.data = data;
  }


  public String getDataUsername() {
    return this.dataUsername;
  }


  public void setDataUsername(String dataUsername) {
    this.dataUsername = dataUsername;
  }


  public String getDataPassword() {
    return this.dataPassword;
  }


  public void setDataPassword(String dataPassword) {
    this.dataPassword = dataPassword;
  }


  public boolean isContinueOnError() {
    return this.continueOnError;
  }


  public void setContinueOnError(boolean continueOnError) {
    this.continueOnError = continueOnError;
  }


  public String getSeparator() {
    return this.separator;
  }


  public void setSeparator(String separator) {
    this.separator = separator;
  }


  public Charset getSqlScriptEncoding() {
    return this.sqlScriptEncoding;
  }


  public void setSqlScriptEncoding(Charset sqlScriptEncoding) {
    this.sqlScriptEncoding = sqlScriptEncoding;
  }


  public ClassLoader getClassLoader() {
    return this.classLoader;
  }


  public Xa getXa() {
    return this.xa;
  }


  public void setXa(Xa xa) {
    this.xa = xa;
  }
  public static class Xa {
    private String dataSourceClassName;
    private Map<String, String> properties = new LinkedHashMap<>();


    public String getDataSourceClassName() {
      return this.dataSourceClassName;
    }


    public void setDataSourceClassName(String dataSourceClassName) {
      this.dataSourceClassName = dataSourceClassName;
    }


    public Map<String, String> getProperties() {
      return this.properties;
    }


    public void setProperties(Map<String, String> properties) {
      this.properties = properties;
    }


  }


  static class DataSourceBeanCreationException extends BeanCreationException {
    private static final long serialVersionUID = 1L;


    private final BaseDataSourceProperties properties;


    private final EmbeddedDatabaseConnection connection;


    DataSourceBeanCreationException(String message, BaseDataSourceProperties properties,
        EmbeddedDatabaseConnection connection) {
      super(message);
      this.properties = properties;
      this.connection = connection;
    }


    public BaseDataSourceProperties getProperties() {
      return this.properties;
    }


    public EmbeddedDatabaseConnection getConnection() {
      return this.connection;
    }


  }


}

mybatis对应的base属性文件

public class BaseMybatisProperties {
  private String configLocation;
  private String[] mapperLocations;
  private String typeAliasesPackage;
  private String typeHandlersPackage;
  private boolean checkConfigLocation = false;
  private ExecutorType executorType;
  private Configuration configuration;
  public String getConfigLocation() {
    return this.configLocation;
  }
  public void setConfigLocation(String configLocation) {
    this.configLocation = configLocation;
  }
  @Deprecated
  public String getConfig() {
    return this.configLocation;
  }
  @Deprecated
  public void setConfig(String config) {
    this.configLocation = config;
  }
  public String[] getMapperLocations() {
    return this.mapperLocations;
  }
  public void setMapperLocations(String[] mapperLocations) {
    this.mapperLocations = mapperLocations;
  }
  public String getTypeHandlersPackage() {
    return this.typeHandlersPackage;
  }
  public void setTypeHandlersPackage(String typeHandlersPackage) {
    this.typeHandlersPackage = typeHandlersPackage;
  }
  public String getTypeAliasesPackage() {
    return this.typeAliasesPackage;
  }
  public void setTypeAliasesPackage(String typeAliasesPackage) {
    this.typeAliasesPackage = typeAliasesPackage;
  }
  public boolean isCheckConfigLocation() {
    return this.checkConfigLocation;
  }
  public void setCheckConfigLocation(boolean checkConfigLocation) {
    this.checkConfigLocation = checkConfigLocation;
  }
  public ExecutorType getExecutorType() {
    return this.executorType;
  }
  public void setExecutorType(ExecutorType executorType) {
    this.executorType = executorType;
  }
  public Configuration getConfiguration() {
    return configuration;
  }
  public void setConfiguration(Configuration configuration) {
    this.configuration = configuration;
  }
  public Resource[] resolveMapperLocations() {
    List<Resource> resources = new ArrayList<Resource>();
    if (this.mapperLocations != null) {
      for (String mapperLocation : this.mapperLocations) {
        Resource[] mappers;
        try {
          mappers = new PathMatchingResourcePatternResolver().getResources(mapperLocation);
          resources.addAll(Arrays.asList(mappers));
        } catch (IOException e) {


        }
      }
    }


    Resource[] mapperLocations = new Resource[resources.size()];
    mapperLocations = resources.toArray(mapperLocations);
    return mapperLocations;
  }
}

因为我们使用的是Hikari数据源,所以这里我是直接copy默认系统Hikari的属性文件。

也就是这个文件:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties 为啥我不直接继承这个类而是在自己的项目中新建这么一个类,是因为我发现这个类有这个注解

@ConfigurationProperties(prefix = "spring.datasource")

怕的是它的这个注解会覆盖我接下来两个类的注解(我主要是懒得测试,所以直接copy一份无所谓了)。

接下来看看具体master和slave两个数据源的属性文件:

  • Properties(jpa和mybatis)文件
@Component
@ConfigurationProperties(prefix = "master.datasource")
public class MasterDataSourceProperties extends BaseDataSourceProperties {
}
 
@Component
@ConfigurationProperties(prefix = "slave.datasource")
public class SlaveDataSourceProperties extends BaseDataSourceProperties {
}
 
@Component
@ConfigurationProperties(prefix = "master.mybatis")
public class MasterMybatisProperties extends BaseMybatisProperties {
}
 
@Component
@ConfigurationProperties(prefix = "slave.mybatis")
public class SlaveMybatisProperties extends BaseMybatisProperties {
}

接下来是数据源的配置了。

  • 数据源配置类
@Configuration
public class HikariDataSourceConfig {
  
  @Bean
  @Primary
  public HikariDataSource masterDataSource(MasterDataSourceProperties properties) {
    HikariDataSource dataSource = createDataSource(properties,
        HikariDataSource.class);
    if (StringUtils.hasText(properties.getName())) {
      dataSource.setPoolName(properties.getName());
    }
    return dataSource;
  }
  @Bean
  public HikariDataSource slaveDataSource(SlaveDataSourceProperties properties) {
    HikariDataSource dataSource = createDataSource(properties,
        HikariDataSource.class);
    if (StringUtils.hasText(properties.getName())) {
      dataSource.setPoolName(properties.getName());
    }
    return dataSource;
  }
  
  @SuppressWarnings("unchecked")
  protected static <T> T createDataSource(BaseDataSourceProperties properties,
      Class<? extends DataSource> type) {
    return (T) properties.initializeDataSourceBuilder().type(type).build();
  }
  
}

因为我们配置的是多个数据源所有其中一个数据源必须加入这个注解@Primary

接下来是jpa的EntityManagerFactory工厂的配置了

  • EntityManagerFactory配置
public class EntityManagerFactoryConfig {

  @Configuration
  @EnableJpaRepositories(basePackages = {
      "${master.jpa.repos}" }, entityManagerFactoryRef = "masterEntityManagerFactory", transactionManagerRef = "masterJPATransactionManager")
  static class MasterEntityManagerFactory {
    @Resource(name = "masterDataSource")
    private DataSource masterDataSource;
    @Value("${master.jpa.domain}")
    private String masterDomainPkg;


    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean masterEntityManagerFactory(EntityManagerFactoryBuilder builder) {
      Map<String, Object> properties = new HashMap<>();
      properties.put("hibernate.hbm2ddl.auto", "update");
      properties.put("hibernate.id.new_generator_mappings", true);
      properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName()) ;
      return builder.dataSource(masterDataSource).packages(masterDomainPkg).persistenceUnit("master")
          .properties(properties).build();
    }


    @Bean
    @Primary
    public PlatformTransactionManager masterJPATransactionManager(EntityManagerFactoryBuilder builder) {
      JpaTransactionManager tm = new JpaTransactionManager(masterEntityManagerFactory(builder).getObject());
      return tm;
    }
  }


  @Configuration
  @EnableJpaRepositories(basePackages = {
      "${slave.jpa.repos}" }, entityManagerFactoryRef = "slaveEntityManagerFactory", transactionManagerRef = "slaveJPATransactionManager")
  @ConditionalOnProperty(name = "multiple.ds.enabled", havingValue = "true")
  static class SlaveEntityManagerFactory {
    @Resource(name = "slaveDataSource")
    private DataSource slaveDataSource;
    @Value("${slave.jpa.domain}")
    private String slaveDomainPkg;


    @Bean
    public LocalContainerEntityManagerFactoryBean slaveEntityManagerFactory(EntityManagerFactoryBuilder builder) {
      Map<String, Object> properties = new HashMap<>();
      properties.put("hibernate.hbm2ddl.auto", "update");
      properties.put("hibernate.id.new_generator_mappings", true);
      properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName()) ;
      return builder.dataSource(slaveDataSource).packages(slaveDomainPkg).persistenceUnit("slave")
          .properties(properties).build();
    }


    @Bean
    public PlatformTransactionManager slaveJPATransactionManager(EntityManagerFactoryBuilder builder) {
      JpaTransactionManager tm = new JpaTransactionManager(slaveEntityManagerFactory(builder).getObject());
      return tm;
    }
  }


}
  • mybatis SqlSessionFactory工厂配置
public class SqlSessionFactoryConfig {


  @Configuration
  static class MasterSqlSessionFactory {
    @Resource
    private MasterMybatisProperties properties;
    @Autowired(required = false)
    private Interceptor[] interceptors;
    @Autowired
    private ResourceLoader resourceLoader = new DefaultResourceLoader();
    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;


    @Bean
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource)
        throws Exception {
      SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
      factory.setDataSource(dataSource);
      factory.setVfs(SpringBootVFS.class);
      if (StringUtils.hasText(this.properties.getConfigLocation())) {
        factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
      }
      factory.setConfiguration(properties.getConfiguration());
      if (!ObjectUtils.isEmpty(this.interceptors)) {
        factory.setPlugins(this.interceptors);
      }
      if (this.databaseIdProvider != null) {
        factory.setDatabaseIdProvider(this.databaseIdProvider);
      }
      if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
      }
      if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
        factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
      }
      if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        factory.setMapperLocations(this.properties.resolveMapperLocations());
      }


      return factory.getObject();
    }
    @Bean
      public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }
    @Bean
    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
      ExecutorType executorType = this.properties.getExecutorType();
      if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
      } else {
        return new SqlSessionTemplate(sqlSessionFactory);
      }
    }
  }
  
  @Configuration
  @ConditionalOnProperty(name = "multiple.ds.enabled", havingValue = "true")
  static class SlaveSqlSessionFactory {
    @Resource
    private SlaveMybatisProperties properties;
    @Autowired(required = false)
    private Interceptor[] interceptors;
    @Autowired
    private ResourceLoader resourceLoader = new DefaultResourceLoader();
    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;


    @Bean
    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
      SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
      factory.setDataSource(dataSource);
      factory.setVfs(SpringBootVFS.class);
      if (StringUtils.hasText(this.properties.getConfigLocation())) {      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
      }
      factory.setConfiguration(properties.getConfiguration());
      if (!ObjectUtils.isEmpty(this.interceptors)) {
        factory.setPlugins(this.interceptors);
      }
      if (this.databaseIdProvider != null) {
        factory.setDatabaseIdProvider(this.databaseIdProvider);
      }
      if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
        factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
      }
      if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {     factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
      }
      if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
        factory.setMapperLocations(this.properties.resolveMapperLocations());
      }
      return factory.getObject();
    }
    
    @Bean
    public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
  }
    @Bean
    public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
      ExecutorType executorType = this.properties.getExecutorType();
      if (executorType != null) {
        return new SqlSessionTemplate(sqlSessionFactory, executorType);
      } else {
        return new SqlSessionTemplate(sqlSessionFactory);
      }
    }
  }
  
}

接下来还需要配置mapper相关的配置

  • mapper配置
public class MapperScanConfig {
  
  @Configuration
  @MapperScan(basePackages = {"com.pack.base.mapper"}, sqlSessionTemplateRef  = "masterSqlSessionTemplate")
  static class MasterMapper {
  }
  
  @Configuration
  @MapperScan(basePackages = {"com.pack.slave.mapper"}, sqlSessionTemplateRef  = "slaveSqlSessionTemplate")
  @ConditionalOnProperty(name = "multiple.ds.enabled", havingValue = "true")
  static class SlaveMapper {
  }
  
}

到这里我们所有的相关配置就完成了,接下来我们只需建立对应的包即可。

图片图片

MyBatis-conf.xml配置文件内容如下:

图片图片

测试:

建立com.pack.domain包,然后新建类Users.java

@Entity
@Table(name = "T_USERS")
@Data
public class Users {
  @Id
  private Long id;
  private String username ;
  private String password ;
  private String realName ;
  private String phone ;
  private String idNo ;
  @Column(length=4000)
  private String authority ;
  @Column(columnDefinitinotallow="int default 0")
  private Integer status = 0 ;
}

建立包com.pack.slave.domain,然后新建类

@Entity
@Table(name = "T_PERSON")
@Data
public class Person{
  @Id
  private Long id;
  private String name ;
  private String email ;
}

启动服务器分别在不同的用户下查看表是否建立,如果都建立了就表示成功。

完毕!!!



Tags:SpringBoot   点击:()  评论:()
声明:本站部分内容及图片来自互联网,转载是出于传递更多信息之目的,内容观点仅代表作者本人,不构成投资建议。投资者据此操作,风险自担。如有任何标注错误或版权侵犯请与我们联系,我们将及时更正、删除。
▌相关推荐
详解基于SpringBoot的WebSocket应用开发
在现代Web应用中,实时交互和数据推送的需求日益增长。WebSocket协议作为一种全双工通信协议,允许服务端与客户端之间建立持久性的连接,实现实时、双向的数据传输,极大地提升了用...【详细内容】
2024-01-30  Search: SpringBoot  点击:(10)  评论:(0)  加入收藏
SpringBoot如何实现缓存预热?
缓存预热是指在 Spring Boot 项目启动时,预先将数据加载到缓存系统(如 Redis)中的一种机制。那么问题来了,在 Spring Boot 项目启动之后,在什么时候?在哪里可以将数据加载到缓存系...【详细内容】
2024-01-19  Search: SpringBoot  点击:(86)  评论:(0)  加入收藏
SpringBoot3+Vue3 开发高并发秒杀抢购系统
开发高并发秒杀抢购系统:使用SpringBoot3+Vue3的实践之旅随着互联网技术的发展,电商行业对秒杀抢购系统的需求越来越高。为了满足这种高并发、高流量的场景,我们决定使用Spring...【详细内容】
2024-01-14  Search: SpringBoot  点击:(90)  评论:(0)  加入收藏
公司用了六年的 SpringBoot 项目部署方案,稳得一批!
本篇和大家分享的是springboot打包并结合shell脚本命令部署,重点在分享一个shell程序启动工具,希望能便利工作。 profiles指定不同环境的配置 maven-assembly-plugin打发布压...【详细内容】
2024-01-10  Search: SpringBoot  点击:(163)  评论:(0)  加入收藏
简易版的SpringBoot是如何实现的!!!
SpringBoot作为目前最流行的框架之一,同时是每个程序员必须掌握的知识,其提供了丰富的功能模块和开箱即用的特性,极大地提高了开发效率和降低了学习成本,使得开发人员能够更专注...【详细内容】
2023-12-29  Search: SpringBoot  点击:(132)  评论:(0)  加入收藏
用 SpringBoot+Redis 解决海量重复提交问题
前言 一:搭建redis的服务Api 二:自定义注解AutoIdempotent 三:token创建和检验 四:拦截器的配置 五:测试用例 六:总结前言:在实际的开发项目中,一个对外暴露的接口往往会面临很多...【详细内容】
2023-12-20  Search: SpringBoot  点击:(53)  评论:(0)  加入收藏
SpringBoot中如何优雅地个性化定制Jackson
当使用 JSON 格式时,Spring Boot 将使用ObjectMapper实例来序列化响应和反序列化请求。在本教程中,我们将了解配置序列化和反序列化选项的最常用方法。一、默认配置默认情况下...【详细内容】
2023-12-20  Search: SpringBoot  点击:(132)  评论:(0)  加入收藏
springboot-如何集成Validation进行参数校验
一、步骤概览 二、步骤说明1.引入依赖包在 pom.xml 文件中引入 validation 组件,它提供了在 Spring Boot 应用程序中进行参数校验的支持。<!-- WEB 程序依赖包 --><dependen...【详细内容】
2023-12-13  Search: SpringBoot  点击:(156)  评论:(0)  加入收藏
优雅的springboot参数校验,你学会了吗?
前言在后端的接口开发过程,实际上每一个接口都或多或少有不同规则的参数校验,有一些是基础校验,如非空校验、长度校验、大小校验、格式校验;也有一些校验是业务校验,如学号不能重...【详细内容】
2023-11-29  Search: SpringBoot  点击:(198)  评论:(0)  加入收藏
Springboot扩展点之BeanDefinitionRegistryPostProcessor,你学会了吗?
前言通过这篇文章来大家分享一下,另外一个Springboot的扩展点BeanDefinitionRegistryPostProcessor,一般称这类扩展点为容器级后置处理器,另外一类是Bean级的后置处理器;容器级...【详细内容】
2023-11-27  Search: SpringBoot  点击:(174)  评论:(0)  加入收藏
▌简易百科推荐
对于微服务架构监控应该遵守的原则
随着软件交付方式的变革,微服务架构的兴起使得软件开发变得更加快速和灵活。在这种情况下,监控系统成为了微服务控制系统的核心组成部分。随着软件的复杂性不断增加,了解系统的...【详细内容】
2024-04-03  步步运维步步坑    Tags:架构   点击:(5)  评论:(0)  加入收藏
大模型应用的 10 种架构模式
作者 | 曹洪伟在塑造新领域的过程中,我们往往依赖于一些经过实践验证的策略、方法和模式。这种观念对于软件工程领域的专业人士来说,已经司空见惯,设计模式已成为程序员们的重...【详细内容】
2024-03-27    InfoQ  Tags:架构模式   点击:(13)  评论:(0)  加入收藏
哈啰云原生架构落地实践
一、弹性伸缩技术实践1.全网容器化后一线研发的使用问题全网容器化后一线研发会面临一系列使用问题,包括时机、容量、效率和成本问题,弹性伸缩是云原生容器化后的必然技术选择...【详细内容】
2024-03-27  哈啰技术  微信公众号  Tags:架构   点击:(10)  评论:(0)  加入收藏
DDD 与 CQRS 才是黄金组合
在日常工作中,你是否也遇到过下面几种情况: 使用一个已有接口进行业务开发,上线后出现严重的性能问题,被老板当众质疑:“你为什么不使用缓存接口,这个接口全部走数据库,这怎么能扛...【详细内容】
2024-03-27  dbaplus社群    Tags:DDD   点击:(11)  评论:(0)  加入收藏
高并发架构设计(三大利器:缓存、限流和降级)
软件系统有三个追求:高性能、高并发、高可用,俗称三高。本篇讨论高并发,从高并发是什么到高并发应对的策略、缓存、限流、降级等。引言1.高并发背景互联网行业迅速发展,用户量剧...【详细内容】
2024-03-13    阿里云开发者  Tags:高并发   点击:(6)  评论:(0)  加入收藏
如何判断架构设计的优劣?
架构设计的基本准则是非常重要的,它们指导着我们如何构建可靠、可维护、可测试的系统。下面是这些准则的转换表达方式:简单即美(KISS):KISS原则的核心思想是保持简单。在设计系统...【详细内容】
2024-02-20  二进制跳动  微信公众号  Tags:架构设计   点击:(36)  评论:(0)  加入收藏
详解基于SpringBoot的WebSocket应用开发
在现代Web应用中,实时交互和数据推送的需求日益增长。WebSocket协议作为一种全双工通信协议,允许服务端与客户端之间建立持久性的连接,实现实时、双向的数据传输,极大地提升了用...【详细内容】
2024-01-30  ijunfu  今日头条  Tags:SpringBoot   点击:(10)  评论:(0)  加入收藏
PHP+Go 开发仿简书,实战高并发高可用微服务架构
来百度APP畅享高清图片//下栽のke:chaoxingit.com/2105/PHP和Go语言结合,可以开发出高效且稳定的仿简书应用。在实现高并发和高可用微服务架构时,我们可以采用一些关键技术。首...【详细内容】
2024-01-14  547蓝色星球    Tags:架构   点击:(115)  评论:(0)  加入收藏
GraalVM与Spring Boot 3.0:加速应用性能的完美融合
在2023年,SpringBoot3.0的发布标志着Spring框架对GraalVM的全面支持,这一支持是对Spring技术栈的重要补充。GraalVM是一个高性能的多语言虚拟机,它提供了Ahead-of-Time(AOT)编...【详细内容】
2024-01-11    王建立  Tags:Spring Boot   点击:(124)  评论:(0)  加入收藏
Spring Boot虚拟线程的性能还不如Webflux?
早上看到一篇关于Spring Boot虚拟线程和Webflux性能对比的文章,觉得还不错。内容较长,抓重点给大家介绍一下这篇文章的核心内容,方便大家快速阅读。测试场景作者采用了一个尽可...【详细内容】
2024-01-10  互联网架构小马哥    Tags:Spring Boot   点击:(115)  评论:(0)  加入收藏
站内最新
站内热门
站内头条