Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Spring connects to Mysql database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Spring how to connect to the Mysql database, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

First, create a Maven project

Import coordinates

  adds the following coordinates to pom.xml and click on the upper right corner to refresh.

Org.springframework spring-context 5.3.15 org.springframework spring-jdbc 5.3.15 mysql mysql-connector-java 8.0.25

Third, managed DataSource class

  creates a class called AppConfig. Host the DataSource class, plus the @ Configuration annotation. Note to set the specified url, user name, and password to connect to the database.

Import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.DriverManagerDataSource;import javax.sql.DataSource;@Configurationpublic class AppConfig {@ Bean public DataSource dataSource () {DriverManagerDataSource d = new DriverManagerDataSource (); d.setUrl ("jdbc:mysql://localhost:3306/test?serverTimezone=UTC"); / / set url / / the above test to your database name d.setUsername ("root") / / set account d.setPassword ("root"); / / set password return d;}}

IV. Testing

  creates a Test class. Get the database connection through DataSource. And output.

Import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import javax.sql.DataSource;import java.sql.Connection;import java.sql.SQLException;public class Test {public static void main (String [] args) throws SQLException {ApplicationContext ac = new AnnotationConfigApplicationContext (AppConfig.class); DataSource d = (DataSource) ac.getBean ("dataSource"); Connection c = d.getConnection (); / / get connection System.out.println (c);}}

The following code appears in the console, which means the connection is successful.

Thank you for reading this article carefully. I hope the article "how to connect Spring to Mysql database" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report