Sprboot Series III: Sprboot Integration Jdbc Template

Keywords: Java Spring MySQL JDBC Database

In the first two articles, we talked about two things:

  1. Getting started with spring boot through a simple example
  2. Modify the default service port number and context path of spring boot

In this article, we'll look at how to persist data through JdbcTemplate.

Don't talk too much nonsense, go straight to dry goods.

I. Code Implementation

  1. Modify the pom file to introduce dependencies
    <!-- Introduce jdbc rely on -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <!-- Introduce mysql Database Connection Dependence-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
  2. Configure database information and add the following in 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. Create entity classes and create databases
    1. Entity class
      package com.study.entity;
      
      public class User {
      
          private Integer id;
          private String userName;
          private String password;
      
          public Integer getId() {
              return id;
          }
      
          public void setId(Integer id) {
              this.id = id;
          }
      
          public String getUserName() {
              return userName;
          }
      
          public void setUserName(String userName) {
              this.userName = userName;
          }
      
          public String getPassword() {
              return password;
          }
      
          public void setPassword(String password) {
              this.password = password;
          }
      
      }
    2. data base
  4. Implementation of dao layer
    @Repository
    public class UserDao {
    
        @Autowired
        JdbcTemplate jdbcTemplate;
    
        public void save(User user) {
            String sql = "insert into t_user(user_name, password) values(?,?)";
            jdbcTemplate.update(sql, user.getUserName(), user.getPassword());
        }
    }
  5. Implementing service Layer
    1. Interface
      public interface UserService {
      
          public void save(User user);
      }
    2. Implementation class
      @Service
      public class UserServiceImpl implements UserService {
      
          @Autowired
          UserDao userDao;
          
          public void save(User user){
              userDao.save(user);
          }
          
      }
  6. Implementing the controller layer
    @RestController
    public class UserController {
        @Autowired
        UserService service;
    
        @RequestMapping("/saveUser")
        public String saveUser(User user) {
    
            service.save(user);
            return "save user successful";
        }
    }
  7. test
    1. Page returns information correctly
    2. Correct Storage of Database

Two, summary

We find that spring boot simplifies the configuration of xml, but does not reduce the amount of java code we write.

Sprboot is not an enhancement of spring functionality, but provides a quick way to use spring: out-of-the-box, no code generation, and no XML configuration.

Posted by mcdsoftware on Tue, 05 Feb 2019 22:42:17 -0800