Configure Data source in spring

Add below lines in your spring configuartion file

<!-- MySQL data source -->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"    destroy-method="close"> -->

<bean id="dataSource" class="com.test.config.MyDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useEncoding=true&amp;characterEncoding=UTF-8&amp;jdbcCompliantTruncation=false" />
<property name="username" value="root" />
<!-- <property name="password" value="test" /> -->
<property name="password" value="dGVzdA==" />
<property name="initialSize" value="1" />
<property name="maxTotal" value="10" />
<property name="maxWaitMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="60000" />
<property name="maxConnLifetimeMillis" value="60000" />
</bean>

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>user-queries.hbm.xml</value>
<value>usertype-queries.hbm.xml</value>
</list>
</property>

<property name="packagesToScan" value="package name of your entity classses" /> 

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> 
<prop key="hibernate.connection.autocommit">false</prop> 
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>

</props>
</property>
</bean>



<bean id="transactionManager"    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

now create MyDataSource class

import java.io.IOException;
import org.apache.commons.dbcp2.BasicDataSource;
import sun.misc.BASE64Decoder;

public class MyDataSource extends BasicDataSource {
     
    public void setPassword(String encodedPassword){
        
        super.setPassword(decode(encodedPassword)); 
         
    }

    
    private String decode(String password) {
        BASE64Decoder decoder = new BASE64Decoder();
        String decodedPassword = null;
        try {
            decodedPassword = new String(decoder.decodeBuffer(password));
        } catch (IOException e) {
            e.printStackTrace();
        }       
       
        return decodedPassword;
        
    }

}

Now in your dao class  use its like

@Repository
public class SmsDAOImpl implements SmsDAO {

    
    @Autowired
    SessionFactory sessionFactory;
    
    @Transactional
    public List getUser() {
        
        List list=null;
        Session hbSession=sessionFactory.getCurrentSession();
        Query query=hbSession.createQuery("from User");
        list=query.list();
        
        return list;
    }

}

Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s