写在前面:
继续记录自己的SpringBoot学习之旅,这次是SpringBoot应用相关知识学习记录。若看不懂则建议先看前几篇博客,详细代码可在我的Gitee仓库SpringBoot克隆下载学习使用!
新建空项目,加入数据库驱动,改为Web项目,如图![![[Pasted image 20220803110135.png]]](https://1000bd.com/contentImg/2024/04/20/d6c8ad4534a822fb.png)
![![[Pasted image 20220803110042.png]]](https://1000bd.com/contentImg/2024/04/20/4b03aee59ae709d3.png)
![![[Pasted image 20220802182110.png]]](https://1000bd.com/contentImg/2024/04/20/2b139d7d897f4284.png)
jdbc:
template:
fetch-size: 10 #缓存行数
max-rows: 500 #最大行数
query-timeout: 10 #查询超时时间
@Test
public void getUsers(@Autowired JdbcTemplate jdbcTemplate){
String sql = "select * from user";
RowMapper<User> rowMapper = new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setPassword(rs.getString("password"));
user.setUsername(rs.getString("username"));
return user;
}
};
List<User> query = jdbcTemplate.query(sql, rowMapper);
query.forEach(System.out::println);
}
SpringBoot提供了3种内嵌数据库供选择,有H2,HSQL,Derby,仅仅用于测试开发,线上务必关掉yml配置中enabled为false
![![[Pasted image 20220803111254.png]]](https://1000bd.com/contentImg/2024/04/20/935b14ff58e4c279.png)
# H2数据库配置
server:
port: 8080
spring:
h2:
console:
# 访问用户名sa,密码123456
enabled: true
path: /h2
# 第一次连接需要,后面可不需要
datasource:
url: jdbc:h2:~/test
driver-class-name: org.h2.Driver
username: sa
password: 123456
http://localhost:8080/h2即可打开,如图![![[Pasted image 20220803113059.png]]](https://1000bd.com/contentImg/2024/04/20/0953248ae4fb5c2d.png)
![![[Pasted image 20220803113557.png]]](https://1000bd.com/contentImg/2024/04/20/8501a6af39001388.png)
![![[Pasted image 20220803113626.png]]](https://1000bd.com/contentImg/2024/04/20/4e206989d0d4884b.png)
![![[Pasted image 20220803114047.png]]](https://1000bd.com/contentImg/2024/04/20/3e334961a5b6d8fa.png)
![![[Pasted image 20220803114508.png]]](https://1000bd.com/contentImg/2024/04/20/0a741cdac2661124.png)
市面上主流NoSQL解决方案为Redis,Mongodb 和ES,这里用的是Windows版
安装详见CSDN
![![[Pasted image 20220829140754.png]]](https://1000bd.com/contentImg/2024/04/20/a4b02f9a37e58382.png)
![![[Pasted image 20220829142557.png]]](https://1000bd.com/contentImg/2024/04/20/e819c23b40fc69b6.png)
spring:
redis:
# 默认配置
port: 6379
host: localhost
![![[Pasted image 20220829142850.png]]](https://1000bd.com/contentImg/2024/04/20/aee15c753ddbbd53.png)
![![[Pasted image 20220829143025.png]]](https://1000bd.com/contentImg/2024/04/20/7b0b4e5cabdb241f.png)
![![[Pasted image 20220829150203.png]]](https://1000bd.com/contentImg/2024/04/20/fb2c7181e30cc586.png)
![![[Pasted image 20220829150254.png]]](https://1000bd.com/contentImg/2024/04/20/8a3c3df2ec406485.png)