Spring Boot 3 教學 (五) 連線資料庫使用 JdbcTemplate 

Spring Boot 3 連線資料庫,可以使用 Spring 所提供的 JdbcTemplate ,進行資料庫的操作。

Spring Boot 3 資料庫連線設定

在 application.properties 中設定連線的資料

spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Spring Boot 3 pom.xml 增加連線資料庫所需要的設定

        <dependency>
		    <groupId>org.mariadb.jdbc</groupId>
		    <artifactId>mariadb-java-client</artifactId>
		    <version>3.3.1</version>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

Spring Boot 3 使用 JdbcTemplate 搭配 @Autowired 即可

@Autowired
  private JdbcTemplate jdbcTemplate;

也可以繼承 JdbcDaoSupport 製作 Dao 物件