玩转Spring全家桶学习笔记(05如何配置单数据源)

 SpringBoot生成时默认的启动类--类1

package com.cocabit.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringDemoApplication.class, args);
	}
}

 为了配置数据源创建的一个类--类2

package com.cocabit.spring;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;
@SpringBootApplication
@Slf4j
public class DataSourceDemoApplication implements CommandLineRunner {

	@Autowired
	private DataSource dataSource;
	public static void main(String[] args) {
		SpringApplication.run(DataSourceDemoApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		showConnection();
	}
	private void showConnection() throws SQLException {
		log.info(dataSource.toString());
		Connection conn = dataSource.getConnection();
		log.info(conn.toString());
		conn.close();
	}

}

所有内容依据视频整理总结

1.@SpringBootApplication 是 Spring中 @Configuration,@EnableAutoConfiguration,@ComponentScan 的合体

因为有两个类设定了@SpringBootApplication,所以在run代码的时候就可以选择不同的启动入口了。

2.CommandLineRunner接口,中的run

为了在项目启动后执行

注:还记得上面的@SpringBootApplication么,其实“类2”中可以不设定的,只要在类上加上注释,@Component,通过启动“类1”,实现CommandLineRunner接口的“类2”照样能运行其中的run方法。

3.@Slf4j 注释

这里其实应用了 lombok

如果实在记不住几个常用的springboot的maven依赖,可以通过右击项目 Spring->Edit Starters来重新添加

如果你刚接触Lombok的话,你仅仅在项目中添加Lombok是不够的。代码中 log 会报错的。

log.info(dataSource.toString());

你还需要的是到你的eclipse路径中安装一下 lombok的jar  https://www.projectlombok.org/download

java -jar lombok.jar

重启下Eclipse,Clean下project。

4.项目中引用了Actuator

使用 http://localhost:8080/actuator/health 确实能够看到健康状况。

可是视频中的 http://localhost:8080/actuator/beans却报404了

真相网址 :https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

对于web来说默认只开放了info和health,如果想全部开放怎么整?

在application.properties文件中添加下面语句,你就成功的解锁了各种功能。

就是这样子了

5.初始化数据库

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xxx?characterEncoding=UTF-8
    username: root
    password: root
    initialization-mode: always
    schema:
    - classpath:schema.sql
    data:
    - classpath:data.sql
  • 配置文件使用的是yml的所以才会有这个展示方式
  • com.mysql.cj.jdbc.Driver 驱动发生了更改,com.mysql.jdbc.Driver。
  • initialization-mode: always
  • schema.sql中保存的是数据库建造语句(数据库建造语句前我添加了 drop table if exists `xxx`; 防止提示已经有xxx表)。data.sql中保存的是数据插入语句。在运行spring boot 的时候会进行数据库的初始化。
  • 关于classpath: 和 classpath:* 的区别
     
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值