开发者

Spring application connecting to Database

Any one tell me how to connect to database us开发者_运维知识库ing spring application.


Use Hibernate as ORM tool, and use Spring ORM support that would be a good design.

See also :

  • Object Relational Mapping (ORM) data access
  • Sample application


Check out Spring JDBC and the JdbcTemplate which is probably the fastest and simplest route.

Agreed with above to look at an ORM solution for anything beyond the basics.


First You have to make enteries for Data Source in application context.xml.

Then we have to make a entry in context.xml and we have to provide the database credentials and location in context.xml

I am attaching both the files of my project.

Hope it may help you.

Code For Application Context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context">

        <bean id="messageSource"
         class="org.springframework.context.support.ResourceBundleMessageSource">
                <property name="basenames">
                        <list>
                                <value>report/cobReports_message</value>
                        </list>
                </property>
        </bean>

        <context:component-scan
                base-package="com.jpmc.am.architecture.emailaddrutil, com.jpmc.am.architecture.cob" />

        <bean id="multipartResolver"
         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxUploadSize" value="200000" />
        </bean>


        <bean id="sqlMapClientTemplate" 
         class="org.springframework.orm.ibatis.SqlMapClientTemplate">
                <property name="sqlMapClient" ref="sqlMapClient" />
        </bean>

        <bean id="sqlMapClient" 
         class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                <property name="configLocation"
                 value="classpath:sqlMap/cobSqlMapConfig.xml" />
                <property name="dataSource" ref="cobDataSource" />
                <property name="useTransactionAwareDataSource" value="true" />
                <property name="sqlMapClientProperties">
                 <value>COB_SCHEMA=CLTAPPMGR //////// Database Schema Name</value>
                </property>
        </bean>

        /// Data Source Name
        <bean id="cobDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName" value="java:comp/env/jdbc/cob" />
        </bean>
</beans>

Code For Context.xml

<Context>

 <Resource name="jdbc/cob" auth="Container" type="javax.sql.DataSource" 
 factory="org.apache.commons.dbcp.BasicDataSourceFactory"
 driverClassName="oracle.jdbc.driver.OracleDriver"
 url="database location:databasename"
 username="username"
 password="password"
 />




</Context>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜