In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Springboot integration JdbcTemplate method is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
I. Preface
Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial building and development process of new Spring applications. The framework is configured in a specific way so that developers no longer need to define a templated configuration. In this way, Spring Boot is committed to becoming a leader in the booming field of rapid application development (rapid application development). This article is mainly about springboot integration with JdbcTemplate.
In this record, share with you.
II. Springboot integrates JdbcTemplate
1. Pom file dependency is introduced.
Org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java 5.1.38 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web
2. New configuration for application.yml
Spring: datasource: url: jdbc:mysql://localhost:3306/yys_springboot_jdbc username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
3 、 UserController.java
/ * Controller * @ author yys * / @ RestController@RequestMapping ("/ user") public class UserController {@ Autowired private UserService userService; @ RequestMapping ("/ add") public String addUser (String userName, Integer age) {return userService.addUser (userName, age)? "success": "fail";}
4 、 UserService.java
/ * user Management * Service * @ author yys * / @ Servicepublic class UserService {@ Autowired private JdbcTemplate jdbcTemplate; public boolean addUser (String userName, Integer age) {return jdbcTemplate.update ("insert into yys_user values (null,?, 1, now (), now ());", userName, age) > 0? True: false;}}
5. Startup class
@ SpringBootApplicationpublic class YysApp {public static void main (String [] args) {SpringApplication.run (YysApp.class, args);}}
6. Initialize the sql file
CREATE TABLE `NOT NULL COMMENT user` (`id`bigint (11) NOT NULL AUTO_INCREMENT COMMENT'ID, self-adding', `user_ name` varchar (32) NOT NULL COMMENT 'user name', `age`int (11) NOT NULL COMMENT 'user age', `status` tinyint (2) NOT NULL DEFAULT'1' COMMENT 'status:-1-deleted; 1-normal;', `create_ time`user` creation time', `update_ time`UPDAT', PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
7. Testing
Http://localhost:8080/user/add?userName=yys&age=18
A. Page result-as shown in the following figure:
B. Database results-as shown in the following figure:
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.