springboot学习笔记(一)使用原生jdbc操作数据库
操作内容不限形式,到达目的就成(非spring boot Initializr)
1.pom/gradle
- 继承spring-boot-starter-parent父类依赖
- 导入spring-boot-starter-parent-jdbc依赖
- 导入数据库驱动依赖
- 在没有datasource的时候,先导入内置数据库依赖,比如h2数据库
2.springboot配置文件,比如application.properties,application.yml
- 配置数据源datasource
3.配置入口文件,比如Application.java
- 添加注解@SpringBootApplication或者@EnableAutoConfiguration和@ComponentScan
-
程序入口代码:SpringApplication.run(Application.class,args);
4.单元测试,比如ApplicationTest.java
- 添加注解@RunWith(SpringRunner.class)和@SpringBootTest
- 需要注入组件的是时候,使用@AutoWired,比如获取数据源
@Autowired
private DataSource dataSource;
比如获取jdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
在需要测试的方法上面添加@Test注解
@Test
public void shouldAnswerWithTrue() throws SQLException {
System.out.println(dataSource.getConnection().toString());
}