springboot 之整合基本的 jdbc 并设置 Mysql 数据库
发布时间:2023-09-20 16:13:27 所属栏目:MySql教程 来源:
导读: 对于数据访问层,无论是 SQL 还是 NOSQL,springboot 默认采用整合 spring data 方式进行统一处理,添加大量自动配置,屏蔽了许多设置,引入各种 xxxTemplate,xxxRepository 来简化我们对数据访问层的操作。对我
对于数据访问层,无论是 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.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </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.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </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.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(); } } 最后是相关输出: 接下来继续,我们可以自己让 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 Map<String,Object> testJdbc(){ List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from department"); return list.get(0); } } 此时记得先注释掉之前的自动建表配置好,不然我们添加的数据会没清楚,再启动服务器: 带上 curd 是因为我在另一个配置文件 application.properties 中配置了: server.servlet.context-path=/curd 至此,整合 jdbc 并操作 mysql 数据库就完成了。 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐