Spring Boot 1.5.4 Integrated JdbcTemplate (vi)

Source: Internet
Author: User

Previous article: Spring boot 1.5.4 Integrated devtools (v)

Spring BootUseJdbcTemplateAccessing the database

Spring Boot Integrated JdbcTemplate Project source code:

Https://git.oschina.net/wyait/springboot1.5.4.git

spring jdbctemplate @Autowired bean

① Import JdbcTemplate and the MySQL (Default version: 5.1.42 ) Database Dependency

<dependency>

<!--JdbcTemplate Configure the If the JPA If you have already joined, you may not need to introduce JDBC Configuration " -

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

② Configure the data source application.properties

#   Configure the data source

Spring.datasource.url=jdbc:mysql://localhost:3306/test

Spring.datasource.username=root

spring.datasource.password=123456

Spring.datasource.driver-class-name=com.mysql.jdbc.driver

③ Write UserService

New Service Package

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/07/57/wKiom1nIVvrRcYZaAAAJM36CcIA177.png "Title=" 000. PNG "alt=" Wkiom1nivvrrcyzaaaajm36ccia177.png "/>

New UserService interfaces and implementation classes. Userserviceimpl code:

"Note that UserService the interface and implementation classes must be under the peer package. If the userserviceimpl Implementation class is placed under the new Service.impl package,@ComponentScan Note Scan not to, Error!!! "

@Service

public class Userserviceimplimplements UserService {

   // Automatic injection JdbcTemplate

@Autowired

Privatejdbctemplate JdbcTemplate;

   // Add User

@Override

Publicint Create (int age, String name) {

Stringsql = "INSERT into user (NAME, age) VALUES (?,?)";

Returnthis.jdbcTemplate.update (SQL, name, age);

}

}

Controller New method:

@Autowired

Privateuserservice UserService;

/**

*

    * @ Description: Add user

& nbsp;   * @ creator: wyait

    * @ creation Time: . years 6 Month - Day 10:40:22

* @param map

* @return

*/

@RequestMapping ("/adduser")

@ResponseBody

Publicstring AddUser (Modelmap map) {

intnum = this.userService.create (" John Doe ");

Returnnum = = 1? "OK": "Fail";

}

④ start, test: Http://127.0.0.1:8080/addUser

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A6/08/wKioL1nIVrSD3SqIAAA5sN-Euv8996.png "title=" 001. PNG "alt=" Wkiol1nivrsd3sqiaaa5sn-euv8996.png "/>

the above-described JdbcTemplate just a few of the most basic operations, more information on the use of other data access operations please refer to: Jdbctemplateapi

Https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html

with this simple example above, we can see the Spring Boot The configuration of the next Access database still adheres to the framework's original purpose: simplicity. We only need to add database dependencies to the pom.xml , and then configure the connection information in the application.properties , without the need to like Spring the jdbctemplate Beanis created in the application and can be injected directly into its own object.

Spring Boot Series Articles:

Spring Boot 1.5.4 Overview (i)

Spring Boot 1.5.4 Introduction and Principles (ii)

Spring Boot 1.5.4 the Web Development (iii)

Spring Boot 1.5.4 Integrated JSP (iv)

Spring Boot 1.5.4 Integrated Devtools (v)

Spring Boot 1.5.4 Integrated JdbcTemplate (vi)

Spring Boot 1.5.4 Integrated SPRING-DATA-JPA (vii)


This article is from the IT Technology Solutions blog, so be sure to keep this source http://wyait.blog.51cto.com/12674066/1968310

Spring Boot 1.5.4 Integrated JdbcTemplate (vi)

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.