在开发过程中,运用关系型数据库是必不可少的,因为处理事物是他们的强项.下面我们测试下spring Boot 在链接数据库方便该如何配置操作.为了节省大家的时间,注意看红色部分.
首先在 pom.xml中新增dependency,
<!-- Spring JDBC 的依赖包,使用 spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa 将会自动获得HikariCP依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> |
增加mysql依赖
其次配置 application.properties,新增内容如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false spring.datasource.password=user spring.datasource.username=1234
#这个一定要加上 用来指定连接mysql所用的驱动
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#以及连接池的配置信息
最后写controller以及对应的dto
运行项目,启动访问 127.0.0.1:8080/db/query 展示如下:
最后有个小提示:
如果在运行过程中提示错误
Error creating bean with name 'dataSource' defined in class path resource...
第一个检查application.propertier 文件中有没有指定 jdbc驱动
第二个检查pom.xml中dependency中有没有增加mysql的依赖
最最后,祝大家大便通畅.