For data access layer, either SQL or NOSQL, springboot default with an integrated spring data processing unified way, adding a large number of automatic configuration, blocked many of the settings, the introduction of a variety of xxxTemplate, xxxRepository to streamline our operations on the data access layer. For us need only a simple settings.
Before the VMware installation using centos7 system using bridge mode enables communication between the host and the virtual machine, installed Mysql Finally docker image. The back has finally returned to the embrace of springboot. springboot jdbc and data source integration is really twists and turns. First, I use springboot clear version is 2.2.4 . Using application.yml connection configuration database .
(1) The first wave
Before that you create by over springboot project idea, do not want to rebuild, so you want to import mysql jdbc starters and drives the Internet to find a circle did not find how to continue the Enabler has been created in good springboot, had their own add manully. Here there are two pits: J name issue dbc starter , mysql mysql driver version and version issues . In the beginning, found jdbc starters are:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
When tested after an old error:
Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
He said there is no such a bean, Baidu long time did not result that he had to re-build a springboot project, and check Mysql driver and data jdbc. And then view the pom.xml file, it found:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency>
After modifying continue. Set Driver's application.yml in time, need to pay attention and their own versions of the corresponding mysql, and springboo connection Mysql drive t default version is not specified, the general is relatively new, it is necessary to specify the mysql-connector-java with Mysql5.7 the version 5.1.41 and the like, and the corresponding driver is com.mysql.jdbc.Driver, the latest version of mysql driver name changed.
(2) the second wave
It was a stupid mistake he had made of:
Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl
Mysql own little behind the colon when then enter urll:
jdbc:mysql://192.168.124.22:3306/jdbc
(3) Third Wave
This is not my pot. When docker connected to the host virtual machine under the linux mysql, given:
java.sql.SQLException: Access denied for user ''@'192.168.124.9' (using password: NO)
Baidu under, in application.yml because springboot the default data-username and data-password, to change the username and password. This is too pit.
The last is the relevant code:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gong</groupId> <artifactId>springboot-curd</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-curd</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: datasource: username: root password: 123456 url: jdbc:mysql://192.168.124.22:3306/jdbc driver-class-name: com.mysql.jdbc.Driver
SpringbootCurdApplicationTests.java
package com.gong.springbootcurd; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootCurdApplicationTests { @Autowired DataSource dataSource; @Test public void contextLoads() { } @Test public void testConnection() throws SQLException { System.out.println(dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }
Finally, the correlation output:
Next to continue, so that we can run and build the table insert statement when springboot start their own, continue to configure in application.yml in:
spring: datasource: username: root password: 123456 url: jdbc:mysql://192.168.124.22:3306/jdbc?serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver schema: - classpath:department.sql initialization-mode: always
department.sql
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for department -- ---------------------------- DROP TABLE IF EXISTS `department`; CREATE TABLE `department` ( `id` int(11) NOT NULL AUTO_INCREMENT, `departmentName` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
After starting springboot department will generate a table in jdbl database, in which we add some data:
Finally jdbc for data manipulation:
@Controller public class HelloController { @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @RequestMapping("/query") public Map<String,Object> testJdbc(){ List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from department"); return list.get(0); } }
At this point I remember before the first comment out automatically configured to build tables, add data or we will not know, and then start the server:
Bring curd is because I configured in another configuration file application.properties in:
server.servlet.context-path=/curd
So far, the integration and operation jdbc mysql database is complete.