环境 idea2019.2 jdk1.8 数据库mysql 5.7
项目 结构

new ->Project 使用springboot快速搭建web项目 选好sdk next

填写项目信息 next

点Web-->勾选 Spring Web

点SQL->勾选 JDBC API 和 MySQL Driver

确认最后的项目信息 Finish 篇幅太长 点开看 这个例子只是生成了表 最后的例子中有数据生成
创建下面两个配置文件 放在resource目录下
 
 
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jdbc?useSSL=true&serverTimezone=UTC&characterEncoding=UTF8
    data-username: root
    data-password: root
    initialization-mode: always
    schema=classpath: schema.sql
application.yml
 
 
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;
schema.sql
手动新建jdbc数据库 测试数据库连接
@RunWith(SpringRunner.class)手动添加  没有自动生成
@RunWith(SpringRunner.class)
@SpringBootTest
class Springboot06jdbcApplicationTests {
    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println("------>>>"+dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println("=========>>>"+connection);
        connection.close();
    }
}
springboot 2 以后默认使用HikariDataSource 数据源
使用Hikari连接池
 
验证后 运行spingboot启动类即可
 
结果

如果想生成表 并生成数据 请看下面操作

user.sql
drop table  if exists user;
 create table user (id bigint(20) not null auto_increment,
 username varchar(40) DEFAULT NULL,
 name varchar(20) DEFAULT NULL,
 age int(3) DEFAULT NULL,
 balance decimal(10,2) DEFAULT NULL,
 primary key(id))ENGINE=InnoDB DEFAULT CHARSET=utf8;
data.sql
insert into user (id, username, name, age, balance) values (1,'account1','张三', 20, 100.00);
insert into user (id, username, name, age, balance) values (2,'account2','李四', 28, 180.00);
insert into user (id, username, name, age, balance) values (3,'account3','王五', 32, 280.00);
修改application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jdbc?useSSL=true&serverTimezone=UTC&characterEncoding=UTF8
    data-username: root
    data-password: root
    initialization-mode: always
    #schema=classpath: schema.sql
    schema=classpath: user.sql
    data=classpath: data.sql
生成表并且插入数据

完整的项目结构

 
  
  
  
 
 
  
 
 
 