spring boot 系列之三:spring boot 整合JdbcTemplate

前面两篇文章我们讲了两件事情:

  1. 通过一个简单实例进行 spring boot 入门
  2. 修改 spring boot 默认的服务端口号和默认 context path

这篇文章我们来看下怎么通过 JdbcTemplate 进行数据的持久化。

废话不多说,直接上干货。

一、代码实现

  1. 修改 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>
  2. 配置数据库信息,在 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
  3. 创建实体类并创建数据库
    1. 实体类
      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;
      }
      

      }

      View Code
    2. 数据库
  4. 实现 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());
    }
    

    }

  5. 实现 service 层
    1. 接口
      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);
      

      }

    2. 实现类
      @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);
      }
      

      }

  6. 实现 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)">;
    }
    

    }

  7. 测试
    1. 页面正确返回信息
    2. 数据库正确保存

二、总结

由此我们发现,spring boot 只是简化了 xml 的配置麻烦,并没有减少我们 java 代码的编写量。

spring boot 不是 spring 功能的增强,而是提供了一种快速使用 spring 的方式:开箱即用,没有代码生成,也无需 XML 配置。