springboot数据库与mybatis入门配置 初始化工程 参考链接:https://blog.csdn.net/typa01_kk/article/details/76696618
数据库配置以及测试 在application.properties配置如下
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5 通过jdbcTemplate的方式访问数据库
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoSpringbootApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void contextLoads() {
String sql = "select * from user";
List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
System.out.println("query result:" + JSON.toJSONString(resultList));
for (Map<String, Object> item: resultList) {
for (Map.Entry<String, Object> entry: item.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue().toString());
System.out.println("\n");
}
}
}
} 日志打印如下:
query result:[{"id":1,"name":"test"}]
id:1
name:test 成功访问数据库....