Home >Database >Mysql Tutorial >How SpringBoot connects to MySQL to obtain data and write back-end interface
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.8</version> </dependency>
3. Create DriverManagerDataSource and JdbcTemplate objects in spring container 3.1 Method to load spring container in springboot 1. Create spring container file under resource
2. Define a common class and add Add the above annotation to automatically load the spring container after springboot starts 4. Create an object class and control class BookPay attention to the @Data annotation here , it is a part of lombok, its main function is to automatically generate get and set methods during compilation, so we do not need to manually write get and set methods in this class, reducing our workload, very convenient, highly recommended BookDao TestControllerresource ---->new- --->Directory---->new Directory(application)
##<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <!-- 1.1.数据库驱动 --> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <!-- 1.2.连接数据库的url --> <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/> <!-- 1.3.连接数据库的用户名 --> <property name="username" value="root"></property> <!-- 1.4.连接数据库的密码 --> <property name="password" value="root"></property> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean>
##application.properties
5. Start the MySQL database
You can refer to the previous article
Install MySQL8.0 and operate visually in Navicat6. Run the test
As shown below, the operation is successful
Enter in the browser to see if the acquisition is successful
Note:8080 and springboot refer to application.propertiesgetbookList is customized in RequestMapping in TestController
At this point, the SpringBoot backend interface is written alright
The above is the detailed content of How SpringBoot connects to MySQL to obtain data and write back-end interface. For more information, please follow other related articles on the PHP Chinese website!