【问题标题】:How to make a db connection using spring jdbc?如何使用 spring jdbc 建立数据库连接?
【发布时间】:2015-12-30 21:21:07
【问题描述】:

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

在我的代码中,我需要返回这个连接(以及我的本地信息)。

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

谢谢

编辑------

原来的dataSource 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

这个类扩展了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());
    }
}

我已将此类声明为 @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();
}

知道怎么做吗?

【问题讨论】:

  • 请问您为什么需要这样做? documentation 似乎表明处理连接是 Spring 的工作......
  • 我发现一些代码(我无法更改)需要它来进行锁定,我认为它可以去检查连接池,但似乎需要将其作为参数发送方法调用
  • 嗯,我无法完全理解您的上下文,但this question 的答案(与您的非常相似,重复?!)建议您可以使用DataSourceUtils
  • 谢谢@Morfic,我更新了我的问题,你能看看吗?我试过其他帖子的回复,但没有结果。
  • 不确定I cannot make it with annotations 是什么意思,但你说你正在使用spring,所以你不应该尝试自己实例化数据源。相反,您可以简单地在 bean 中定义一个自动装配字段:@Autowired private DataSource dataSource;

标签: spring jdbc spring-jdbc


【解决方案1】:

To Mae JDBC连接(使用Spring Boot)你需要先:

  1. 在 .properties 文件中定义数据库属性。
  2. 然后使用 JDBCTemplate 类进行 DB 连接。

一步一步的实现给出了here

【讨论】:

    【解决方案2】:

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

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

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

    如果创建以下 dao 类:

    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”作为“默认数据源”名称
    • 使用 dao/repository 时,dataSource 是必需的,因此 spring 建议您将 datasource 作为参数添加到 dao 的构造函数中

    【讨论】:

    • 嗨,是的,这就是 conf bean 的样子,但我没有 xml,我只能使用类文件并将其设置为注释,你知道该怎么做有注释吗?
    • 我已经在答案中添加了相应的Java配置。希望这会有所帮助。
    • 我已经按照这种方式编写了它,但仍然没有用...在 DAO 类中我有 @Service 注释,我应该删除它吗?还是包括其他?
    • Spring 通常使用@Repositoy 来表示一个dao 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多