zoukankan      html  css  js  c++  java
  • Spring Boot教程(二十八)通过JdbcTemplate编写数据访问

    数据源配置

    在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式。

    首先,为了连接数据库需要引入jdbc支持,在pom.xml中引入如下配置:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    

      

    嵌入式数据库支持

    嵌入式数据库通常用于开发和测试环境,不推荐用于生产环境。Spring Boot提供自动配置的嵌入式数据库有H2、HSQL、Derby,你不需要提供任何连接配置就能使用。

    比如,我们可以在pom.xml中引入如下配置使用HSQL

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>
    

      

    连接生产数据源

    以MySQL数据库为例,先引入MySQL连接的依赖包,在pom.xml中加入:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
    

      

    src/main/resources/application.properties中配置数据源信息

    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=dbuser
    spring.datasource.password=dbpass
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

      

    连接JNDI数据源

    当你将应用部署于应用服务器上的时候想让数据源由应用服务器管理,那么可以使用如下配置方式引入JNDI数据源。

    spring.datasource.jndi-name=java:jboss/datasources/customers
    

      

    源码来源

  • 相关阅读:
    LeetCode_374. Guess Number Higher or Lower
    LeetCode_371. Sum of Two Integers
    LeetCode_367. Valid Perfect Square
    LeetCode_350. Intersection of Two Arrays II
    LeetCode_349. Intersection of Two Arrays
    LeetCode_345. Reverse Vowels of a String
    LeetCode_344. Reverse String
    LeetCode_342. Power of Four
    hadoop生态系统的详细介绍
    hadoop启动jobhistoryserver
  • 原文地址:https://www.cnblogs.com/allalongx/p/8513213.html
Copyright © 2011-2022 走看看