Spring Boot Series three: Spring Boot integration JdbcTemplate

Source: Internet
Author: User

In the first two articles we talked about two things:

    1. Get started with a simple instance of spring boot
    2. Modify the Spring boot default service port number and default context path

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

Don't say much nonsense, go straight to dry.

First, the code implementation

  1. Modify the Pom file to introduce dependent dependencies
    <!--Introducing JDBC Dependencies -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-jdbc</Artifactid>        </Dependency>        <!--introducing MySQL database connection dependencies -        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>        </Dependency>
  2. To configure the database information, add the following in Application.properties:
    spring.datasource.driver-class-name=com.mysql.jdbc.driverspring.datasource.url=jdbc:mysql://localhost:3306/ Testspring.datasource.username=rootspring.datasource.password=root
  3. Create an entity class and create a database
    1. Entity class
       Packagecom.study.entity; Public classUser {PrivateInteger ID; PrivateString UserName; PrivateString password;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}
      View Code
    2. Database
  4. Implementing the DAO Layer
    @Repository  Public class Userdao {    @Autowired    jdbctemplate jdbctemplate;      Public void Save (User user) {        = "INSERT INTO T_user (user_name, password) VALUES (?,?)" ;        Jdbctemplate.update (SQL, User.getusername (), User.getpassword ());}    }
  5. Implement service layer
    1. interface
       public  interface< /span> 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. Database is saved correctly

Second, summary

As a result, spring boot simply simplifies the configuration of XML and does not reduce the amount of Java code we write.

Spring boot is not an enhancement of spring functionality, but rather provides a quick way to use spring: out of the box, without code generation, and without XML configuration.

Spring Boot Series three: Spring Boot integration JdbcTemplate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.