资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

你会用SpringBoot调用JDBC操作数据库么?

1 环境准备

数据库使用MySQL,我们先创建一个SpringBoot项目再导入MySQL驱动:

江口ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!


    org.springframework.boot
    spring-boot-starter-jdbc


    mysql
    mysql-connector-java
    runtime
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.137.140:3306/mysql
    driver-class-name: com.mysql.jdbc.Driver

2 测试代码

package cn.zhangyu;

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;

import static sun.misc.Version.println;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJdbcApplicationTests {

    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads() throws SQLException {
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();

    }

}

3 执行sql语句

DataSourceInitializer:ApplicationListener;可以帮助我们执行sql语句,但是需要将文件命名为:schema-.sql、data-.sql
默认规则:schema.sql,schema-all.sql;(DataSourceProperties这个类进行了默认的配置)

DataSourceInitializer作用:

1)、runSchemaScripts();运行建表语句;

2)、runDataScripts();运行插入数据的sql语句;
  • 实例;
  1. 再pom中添加

    #    如果你配置文件没有指定执行文件的名称而是使用默认的schema.sql或者schema-all.sql的话
    #    就在配置文件中加上spring.datasource.initialization-mode=always
       initialization-mode: always
    
  2. 我们再resources目录下放入schema.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;
    
    INSERT INTO department(id,departmentName) VALUES(1,'业务部');
    INSERT INTO department(id,departmentName) VALUES(2,'前台部');
    
  3. 查看结果

    mysql> select * from department;
    +----+----------------+
    | id | departmentName |
    +----+----------------+
    |  1 | 业务部      |
    |  2 | 前台部      |
    +----+----------------+
    2 rows in set (0.00 sec)
    --------------------- 
  4. 编写controller 进行测试

    package cn.zhangyu;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    import java.util.Map;
    
    /**
    * Created by grace on 2018/11/28.
    */
    @RestController
    public class HelloController {
    
       //spring boot中已经给我们注册好JdbcTemplate
       @Autowired
       JdbcTemplate jdbcTemplate;
    
       @GetMapping("/hello")
       public Map getDepartment(){
           List> list = jdbcTemplate.queryForList("select * from department");
           return list.get(0);
       }
    }
    

访问http://localhost:8080/hello:
{"id":1,"departmentName":"业务部"}

  • 自定义sql执行
#可以使用   
    schema:
      - classpath:department.sql

schema是一个list所以可以使用- 指定多个位置


本文题目:你会用SpringBoot调用JDBC操作数据库么?
网页网址:http://cdkjz.cn/article/gescds.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220