1 pom檔案引入
<!-- jdbcTemplate 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql 依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2 application.properties增加資料庫配置
spring.datasource.url=jdbc:mysql://localhost:3306/xxxx
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3 service類使用JdbcTemplate
@Service
public class TestServiceImpl implements ITestService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void jdbcTemplate() {
jdbcTemplate.queryForList("SELECT * FROM t_contents where 1 = 1 --");
}
}