如何使用 spring jdbc 建立数据库连接?

How to make a db connection using spring jdbc?

我正在尝试使用 spring jdbc 进行简单连接,return 是一种 Connection 类型,在我的项目中,我正在使用 spring jdbc 和 spring 数据,自动配置。

在我的代码中,我需要 return 此连接(使用我的本地信息)。

可以吗?在这种情况下,是否有可能获得目前正在使用的信息? (我的意思是,数据库名、密码等等。)

谢谢

编辑------

原始数据源 bean 如下所示

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="url" value="jdbc:mysql://${jdbc.host}:${jdbc.port}"/>
 </bean>

但是我不能用注释来做,有什么想法吗?

我试过这样做

DriverManagerDataSource source = new org.springframework.jdbc.datasource.DriverManagerDataSource();

错误

我不断得到这个

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

class 扩展了 JdbcDaoSupport,似乎需要它...

这是JdbcDaoSupport.class

package org.springframework.jdbc.core.support;

import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.dao.support.DaoSupport;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.SQLExceptionTranslator;

public abstract class JdbcDaoSupport extends DaoSupport {
    private JdbcTemplate jdbcTemplate;

    public JdbcDaoSupport() {
    }

    public final void setDataSource(DataSource dataSource) {
        if(this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
            this.jdbcTemplate = this.createJdbcTemplate(dataSource);
            this.initTemplateConfig();
        }

    }

    protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    public final DataSource getDataSource() {
        return this.jdbcTemplate != null?this.jdbcTemplate.getDataSource():null;
    }

    public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        this.initTemplateConfig();
    }

    public final JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    protected void initTemplateConfig() {
    }

    protected void checkDaoConfig() {
        if(this.jdbcTemplate == null) {
            throw new IllegalArgumentException("\'dataSource\' or \'jdbcTemplate\' is required");
        }
    }

    protected final SQLExceptionTranslator getExceptionTranslator() {
        return this.getJdbcTemplate().getExceptionTranslator();
    }

    protected final Connection getConnection() throws CannotGetJdbcConnectionException {
        return DataSourceUtils.getConnection(this.getDataSource());
    }

    protected final void releaseConnection(Connection con) {
        DataSourceUtils.releaseConnection(con, this.getDataSource());
    }
}

我已将此 class 声明为 @bean 并执行此操作

public DriverManagerDataSource provideSource() {
    DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
    //this.dataSource = dataSource;
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("user");
    dataSource.setPassword("pass");
    dataSource.setUrl("jdbc:mysql://localhost:3306/db");
    return dataSource;
}


@Bean
MyClientDao myClientDao(){
    MyClientDao myClientDao = new MyClientDao();
    myClientDao().setDatabaseName("db");
    myClientDao().setDataSource(provideSource());
    return myClientDao();
}

知道怎么做吗?

异常表示JdbcTemplate为空。 (代码摘自 JdbcDaoSupport.class)

protected void checkDaoConfig() {
    if(this.jdbcTemplate == null) {
        throw new IllegalArgumentException("\'dataSource\' or \'jdbcTemplate\' is required");
    }
}

您需要将 DAO(扩展 JdbcDaoSupport)与数据源(bean)连接起来

如果创建以下 dao class:

public class CustomDaoImpl extends JdbcDaoSupport implements CustomDao {
...<implementation>
}

您的配置为:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="url" value="jdbc:mysql://${jdbc.host}:${jdbc.port}"/>
 </bean>

<bean id="myClientDao" class="package.MyClientDao">
    <property name="dataSource" ref="dataSource" />
</bean>

这样 JdbcTemplate 将被正确初始化。

有了注释,这将如下所示:

@Configuration
public class ConfigBean {
    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
        //this.dataSource = dataSource;
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("user");
        dataSource.setPassword("pass");
        dataSource.setUrl("jdbc:mysql://localhost:3306/db");
        return dataSource;
    }

    @Bean
    public MyClientDao myClientDao(){
        MyClientDao myClientDao = new MyClientDao();
        myClientDao.setDataSource(dataSource());
        return myClientDao;
    }
}

不要忘记将@Bean 注释添加到数据源。

旁注:

  • spring 使用 'dataSource' 作为 'default datasource' 名称
  • 使用 dao/repository 时,数据源是必需的,因此 spring 建议您将数据源作为参数添加到 dao 的构造函数中

为了 Mae JDBC 连接(使用 Spring 引导)您需要首先:

  1. 在 .properties 文件中定义数据库属性。
  2. 然后使用 JDBC模板 Class 进行数据库连接。

给出了分步实施 here