spring boot 系列之三:spring boot 整合JdbcTemplate
前面两篇文章我们讲了两件事情:
这篇文章我们来看下怎么通过 JdbcTemplate 进行数据的持久化。
废话不多说,直接上干货。
一、代码实现
- 修改 pom 文件,引入相关依赖
<!-- 引入 jdbc 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 引入 mysql 数据库连接依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
- 配置数据库信息,在 application.properties 中添加如下内容:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root
- 创建实体类并创建数据库
- 实体类
package com.study.entity;
public class User {
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer id; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String userName; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String password; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(Integer id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUserName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUserName(String userName) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userName =<span style="color: rgba(0, 0, 0, 1)"> userName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassword() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> password; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassword(String password) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.password =<span style="color: rgba(0, 0, 0, 1)"> password; }
}
- 数据库
- 实体类
- 实现 dao 层
@Repository public class UserDao {
@Autowired JdbcTemplate jdbcTemplate; </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(User user) { String sql </span>= "insert into t_user(user_name, password) values(?,?)"<span style="color: rgba(0, 0, 0, 1)">; jdbcTemplate.update(sql, user.getUserName(), user.getPassword()); }
}
- 实现 service 层
- 接口
public interface UserService {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(User user);
}
- 实现类
@Service public class UserServiceImpl implements UserService {
@Autowired UserDao userDao; </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(User user){ userDao.save(user); }
}
- 接口
- 实现 controller 层
@RestController public class UserController { @Autowired UserService service;
@RequestMapping(</span>"/saveUser"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String saveUser(User user) { service.save(user); </span><span style="color: rgba(0, 0, 255, 1)">return</span> "save user successful"<span style="color: rgba(0, 0, 0, 1)">; }
}
- 测试
- 页面正确返回信息
- 数据库正确保存
- 页面正确返回信息
二、总结
由此我们发现,spring boot 只是简化了 xml 的配置麻烦,并没有减少我们 java 代码的编写量。
spring boot 不是 spring 功能的增强,而是提供了一种快速使用 spring 的方式:开箱即用,没有代码生成,也无需 XML 配置。