In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "SpringBoot how to encapsulate JDBC", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "SpringBoot how to encapsulate JDBC" this article.
Database configuration can be done directly in the configuration file in Spring Boot
Spring.datasource.username= rootspring.datasource.password= 123456spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
SpringBoot can generate database objects directly.
The default data source is Hikari
Jdbc connection
Import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import javax.sql.DataSource;import java.sql.Connection;import java.sql.SQLException;@SpringBootTestclass DataSpringbootApplicationTests {@ Autowired DataSource dataSource; @ Test void contextLoads () throws SQLException {System.out.println ("default data source"); System.out.println (dataSource.getClass ()) System.out.println ("get database connection"); Connection connection = dataSource.getConnection (); System.out.println (connection); System.out.println ("close data source"); connection.close ();}}
There are a lot of template in springboot that can be used directly.
Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestControllerpublic class JDBCController {@ Autowired JdbcTemplate jdbcTemplate / / query all database information @ GetMapping ("/ userList") public List userList () {String sql = "select * from user"; List mapList = jdbcTemplate.queryForList (sql); return mapList;} @ GetMapping ("/ addUser") public String addUser () {String sql = "insert into mybatis.user (id,name,pwd) values"; jdbcTemplate.update (sql) Return "update-ok"; @ GetMapping ("/ deleteUser/ {id}") public String deleteUser (@ PathVariable ("id") int id) {String sql = "delete from mybatis.user where id =?"; jdbcTemplate.update (sql,id); return "delete-ok";}}
The above is all the contents of the article "how SpringBoot encapsulates JDBC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.