Springboot 注意事项 慢慢累积

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tangyaliang11/article/details/82056693
1.当使用这种方式配置数据源,默认url名称使用的是jdbc-url
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
2.使用这种方式配置数据源,使用的是url
    @ConfigurationProperties(prefix = "local.datasource")
    public DataSourceProperties localDataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "localDateSource")
    @ConfigurationProperties(prefix = "local.datasource")
    public DataSource firstDataSource() {
        return localDataSourceProperties().initializeDataSourceBuilder().build();
    }
3.在使用springboot时使用freemarker作为模板是,默认情况下输出的数字是千分位格式化的如:0,000,000。导致使用一些js框架时读取数字失败。
可以使用number_format: 0.##  设置格式。

1、spring boot maven Unable to find main class

在pom中加入 二者选其一  

<properties>
    <start-class>com.EurekaApplication</start-class>main方法类路径
</properties>
<build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  			<configuration>
        		<mainClass>com.EurekaApplication</mainClass>main方法类路径
    		</configuration>
  		</plugin>
  	</plugins>
  </build>

2、今天做项目,给方法名称起了一个不正规的名字,居然出现了错误提示,好生奇怪,看提示信息显示在chinesedrug类中没有test这个属性,才发现原来springboot还有这种检查。

将实现的接口repository注释掉后,错误提示消失。

3、springboot项目启动时,如果没有配置数据库配置,启动时会抛出如下异常。

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. 
If you have database settings to be loaded from a particular profile you may 
need to active it (no profiles are currently active).

直接原因是ClassPath中含有DataSource,所以必须在appliaction.yml/appliaction.properties中配置数据库相关信息。

简单点说就是在maven中你导入了数据库配置。例如:

扫描二维码关注公众号,回复: 3961058 查看本文章
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

网络中大多解决办法都是在启动类注解中加入一下内容,意识是排除数据自动配置。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

个人认为这是个办法,但不是个好办法(你会说出于测试其他功能,我的回答是只要你在代码中实现了repository就必须配置数据库连接信息,并且导入数据库驱动,不然即便配置中排除了数据库配置那么一样会出错误「亲测过哦」)。

因为如果你不使用数据库何必导入它,

如果使用数据库那就应该在appliaction.yml/appliaction.properties中配置相关内容。

例如:

spring:
  datasource: 
      username: root
      password: 123
      url: jdbc:mysql://192.168.1.6:3306/engine
      driver-class-name: com.mysql.jdbc.Driver

猜你喜欢

转载自blog.csdn.net/tangyaliang11/article/details/82056693