配置

1.pom.xml 依赖

在 Spring Boot 项目中加入 spring-boot-starter-jdbc、mysql-connector-java 的依赖

  1. <!-- 数据库 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-jdbc</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <scope>runtime</scope>
  10. </dependency>

2.配置数据源

  1. # mysql
  2. spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&characterEncoding=utf-8
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

注意:

  • 可以不指定 driver-class-name,spring boot 会自动识别 url
  • 数据连接池默认使用 tomcat-jdbc

使用 JdbcTemplate 模板

1.测试脚本

  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT NULL,
  4. `create_time` datetime DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='用户表';

2.实体类

  1. /**
  2. * 实体类
  3. */
  4. public class User {
  5. private int id;
  6. private String name;
  7. private Date createTime;
  8. public int getId() {
  9. return id;
  10. }
  11. public void setId(int id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public Date getCreateTime() {
  21. return createTime;
  22. }
  23. public void setCreateTime(Date createTime) {
  24. this.createTime = createTime;
  25. }
  26. @Override
  27. public String toString() {
  28. return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
  29. }
  30. }

3.dao 接口

  1. /**
  2. * 用户 dao
  3. */
  4. public interface UserDao {
  5. int insert(User user);
  6. int deleteById(int id);
  7. int updateById(User user);
  8. User selectById(int id);
  9. }

4.dao 实现类

  1. public class UserDaoImpl implements UserDao {
  2. @Autowired
  3. private JdbcTemplate jdbcTemplate;
  4. @Override
  5. public int insert(User user) {
  6. String sql = "insert into user (name, create_time) values (?, ?)";
  7. return jdbcTemplate.update(sql, user.getName(), user.getCreateTime());
  8. }
  9. @Override
  10. public int deleteById(int id) {
  11. String sql = "delete from user where id=?";
  12. return jdbcTemplate.update(sql, id);
  13. }
  14. @Override
  15. public int updateById(User user) {
  16. String sql = "update user set name=?, create_time=? where id=?";
  17. return jdbcTemplate.update(sql, user.getName(), user.getCreateTime(), user.getId());
  18. }
  19. @Override
  20. public User selectById(int id) {
  21. String sql = "select * from user where id=?";
  22. return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
  23. @Override
  24. public User mapRow(ResultSet rs, int rowNum) throws SQLException {
  25. User user = new User();
  26. user.setId(rs.getInt("id"));
  27. user.setName(rs.getString("name"));
  28. user.setCreateTime(rs.getDate("create_time"));
  29. return user;
  30. }
  31. }, id);
  32. }
  33. }

5.测试类

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class SpringBootDemo131ApplicationTests {
  4. @Autowired
  5. private RoncooUserDao roncooUserDao;
  6. @Test
  7. public void insert() {
  8. RoncooUser roncooUser = new RoncooUser();
  9. roncooUser.setName("测试");
  10. roncooUser.setCreateTime(new Date());
  11. int result = roncooUserDao.insert(roncooUser);
  12. System.out.println(result);
  13. }
  14. @Test
  15. public void delete() {
  16. int result = roncooUserDao.deleteById(1);
  17. System.out.println(result);
  18. }
  19. @Test
  20. public void update() {
  21. RoncooUser roncooUser = new RoncooUser();
  22. roncooUser.setId(2);
  23. roncooUser.setName("测试2");
  24. roncooUser.setCreateTime(new Date());
  25. int result = roncooUserDao.updateById(roncooUser);
  26. System.out.println(result);
  27. }
  28. @Test
  29. public void select() {
  30. RoncooUser result = roncooUserDao.selectById(2);
  31. System.out.println(result);
  32. }
  33. @Test
  34. public void select2() {
  35. RoncooUser result = roncooUserDao.selectById(7);
  36. System.out.println(result);
  37. }
  38. // 分页测试
  39. @Test
  40. public void queryForPage(){
  41. Page<RoncooUser> result = roncooUserDao.queryForPage(1, 20, "测试");
  42. System.out.println(result.getList());
  43. }
  44. }

6.打印 sql 语句,添加如下

  1. <logger name="org.springframework.jdbc.core.JdbcTemplate" level="debug"/>

封装 Spring JDBC,带分页

提供三个类:JdbcDaoImpl.java、Page.java、Sql.java,具体查看附件。

JdbcDaoImpl.javaPage.javaSql.java