Get the App
SLTechnology News&Howtos  ›  Development  › 

How to encapsulate SpringBoot and use JDBC

Shulou Source: shulou.com Published: 2022-06-02 04:52:37 09月15日 Update

This article is about how SpringBoot encapsulates the content that uses JDBC. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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";}}

Thank you for reading! This is the end of this article on "how to encapsulate SpringBoot and use JDBC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Data database data source encapsulation content more articles configuration good practical information objects files articles look knowledge reference help related query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi MySQL Microsoft MariaDB