对于数据访问层,无论是SQL还是NOSQL,springboot默认采用整合spring data方式进行统一处理,添加大量自动配置,屏蔽了许多设置,引入各种xxxTemplate,xxxRepository来简化我们对数据访问层的操作。对我们来说只需要进行简单的设置即可。
之前利用VMware安装了centos7系统,并利用桥接模式实现了主机和虚拟机之间的通信,最后利用docker安装了Mysql镜像。这次终于重新又回到了springboot的怀抱中。springboot整合jdbc和数据源真的是一波三折。首先明确我使用的springboot版本是2.2.4。并使用application.yml进行数据库连接相关配置。
(1)第一波
之前自己通过idea创建过了springboot项目,不想再重新建了,于是想导入jdbc启动器和mysql驱动,在网上找了一圈都没找到如何在已经创建好的springboot中继续添加启动器,只好自己手动添加。这里就有两个坑:jdbc启动器的名字问题、mysql驱动版本与mysql版本问题。最开始,找到的jdbc启动器是:
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-jdbcartifactId> dependency>
之后进行测试的时候一老报错:
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)}
说是没有这么一个bean,百度半天也没有结果,自己只好重新建一个springboot项目,并勾选Mysql driver和data jdbc。然后查看pom.xml文件,发现是:
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-jdbcartifactId> dependency>
修改完之后继续。application.yml中设置Driver的时候,需要注意和自己版本的mysql相对应,而springboo连接Mysql驱动t默认版本是没指定的,一般是比较新,用Mysql5.7就要指定为mysql-connector-java的版本为5.1.41之类的,而且对应的驱动是com.mysql.jdbc.Driver,最新版本的mysql驱动名称变了。
(2)第二波
这是自己犯的一个低级错误:
Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl
自己再输入urll时少了mysql后面的冒号:
jdbc:mysql://192.168.124.22:3306/jdbc
(3)第三波
这可不是我的锅了。在主机连接到虚拟机中linux下的docker中的mysql时,报错:
java.sql.SQLException: Access denied for user ''@'192.168.124.9' (using password: NO)
百度了下,在application.yml中,因为springboot中默认是data-username和data-password,要改成username和password。这也太坑了。
最后是相关代码:
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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.2.4.RELEASEversion> <relativePath/> parent> <groupId>com.gonggroupId> <artifactId>springboot-curdartifactId> <version>0.0.1-SNAPSHOTversion> <name>springboot-curdname> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-thymeleafartifactId> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId>5.1.41 <scope>runtimescope> dependency> <dependency> <groupId>org.springframework.bootgroupId>spring-boot-starter-data-jdbc dependency> <dependency> <groupId>org.webjarsgroupId> <artifactId>bootstrapartifactId> <version>4.1.2version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> <exclusions> <exclusion> <groupId>org.junit.vintagegroupId> <artifactId>junit-vintage-engineartifactId> exclusion> exclusions> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> 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(); } }
最后是相关输出:
接下来继续,我们可以自己让springboot启动时运行建表和插入语句,在application.yml中继续配置:
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;
启动springboot之后就会在jdbl数据库中生成一个department的表,我们在其中添加些数据:
最后利用jdbc进行数据操作:
@Controller public class HelloController { @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @RequestMapping("/query") public MaptestJdbc(){ List
此时记得先注释掉之前的自动建表配置好,不然我们添加的数据会没清楚,再启动服务器:
带上curd 是因为我在另一个配置文件application.properties中配置了:
server.servlet.context-path=/curd
至此,整合jdbc并操作mysql数据库就完成了。