How to add DATABASE access in Dynamic Web Project ?

We will get such structure :

Add DATABSE in Dynamic Web Projet

Add DATABSE in Dynamic Web Projet

1. Create in your WEB_ROOT these folders: WEB-INF/database
2. Create file “datapase.properties” :

1
2
3
4
5
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc\:postgresql\://109.197.1.1\:5433/yourDB
jdbc.username=yourLogin
jdbc.password=yourPWD
jdbc.showSql=true

3. Create DataSource.xml file – it contains bean “dataSource” and gets value from “datapase.properties” file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>/WEB-INF/database/database.properties</value>
    </property>
</bean>
 
<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
 
</beans>

4. Create Hibernate.xml file – it cares about Spring Transactions, Hibernate Session Factory :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       ">


    <!-- Hibernate transaction manager -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

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

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- +1 -->
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <!-- Enable Hibernate's automatic session context management
                   Spring manages the transaction, and it doesn't need of that the session is holding in a
                   hibernate thread.
               <prop key="hibernate.current_session_context_class">thread</prop>
               -->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>

            </props>
        </property>

        <property name="packagesToScan" value="ru.tomatch.admin.model" />

        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>

        <property name="exposeTransactionAwareSessionFactory">
            <value>false</value>
        </property>


    </bean>
</beans>

5. And last file applicationContext.xml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<beans
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://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/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   
    <!-- Database Configuration -->
    <import resource="./database/DataSource.xml"/>
    <import resource="./database/Hibernate.xml"/>
 
    <!-- Auto scan the components -->
    <context:component-scan base-package="some.your.package1" />    
    <context:component-scan base-package="some.your.package2" />    

</beans>