简体   繁体   English

jdbc connectvity如何在Spring MVC中工作

[英]How jdbc connectvity should work in Spring MVC

Below is the way, i am using jdbc connectivity for my spring mvc. 下面是我的spring mvc使用jdbc连接的方式。 I have some technical doubts, which follows- 我有一些技术上的疑问,以下是-

1. 1。

As i have invoked datasource object in every bean that requires db connectivity. 因为我在每个需要数据库连接的bean中都调用了数据源对象。 Is it the right way of doing it? 这是正确的做法吗? What if i don't want a particular repository object to be instantiated when the application starts up (because I'm not sure when user will invoke the object, so why instantiate it at the very beginning)? 如果我不希望在应用程序启动时实例化特定的存储库对象怎么办(因为我不确定用户何时调用该对象,所以为什么要在开始时实例化该对象)?

<bean 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>classpath:jdbc.properties</value>
            </property>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
        <property name="maxStatements" value="${jdbc.maxStatements}" />
        <property name="testConnectionOnCheckout" value="${jdbc.testConnection}" />
    </bean>

<bean id="ustestuthenticationRepository" 
                class="com.test.repository.impl.UstestuthenticationRepositoryImpl">

        <property name="dataSource" ref="dataSource" />
</bean>
<bean id="someclass" 
                class="com.test.repository.impl.someclass">

        <property name="dataSource" ref="dataSource" />
</bean>

2. 2。

@Qualifier("dbDataSource")
    private static DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

and then creating 然后创建

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

I'm not sure if this is the right way of invoking datasource.If each and every Repository object creates separate jdbctemplate, is it something appropriate? 我不确定这是否是调用数据源的正确方法。如果每个存储库对象都创建单独的jdbctemplate,是否合适?

In every repository class, i am invoking datasource in the folowing way- 在每个存储库类中,我都按照以下方式调用数据源:

Modified Code 修改后的代码

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg  ref="dataSource" />
    </bean>

<bean id="someclass" class="com.era.repository.impl.someclass">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

and implementation in someclass is - 在某类中的实现是-

private JdbcTemplate jdbcTemplate;

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

then only accessing jdbcTemplate variable wherever is required. 然后仅在需要时访问jdbcTemplate变量。 Am i doing it correctly now? 我现在做对了吗? Please advise. 请指教。

What you done is not an error. 您所做的不是错误。 It is possible to work that way. 可以这样工作。 However. 然而。 It means you will simply work with JDBC directly. 这意味着您将直接使用JDBC。 It means, you need handle transactions "manually". 这意味着,您需要“手动”处理事务。 It is hard and a lot of code. 这很困难,并且有很多代码。

You better use spring data and hibernate. 您最好使用spring数据并休眠。 Or spring data and JPA. 或弹簧数据和JPA。 Anyway, spring data will help you to manage all resources and will simplify data access for you. 无论如何,spring数据将帮助您管理所有资源,并简化数据访问。

If you already on spring MVC, take also spring data. 如果您已经使用spring MVC,请同时获取spring数据。 Why bother? 何必呢? Official spring data example: https://spring.io/guides/gs/accessing-data-jpa/ 官方的春季数据示例: https : //spring.io/guides/gs/accessing-data-jpa/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM