Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

This project is a decorator PrettyJDBC library for easy integration into Spring Framework 5.

License

Notifications You must be signed in to change notification settings

omarchenko4j/PrettyJDBC-spring5

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Integrating PrettyJDBC with Spring 5

PrettyJDBC offers a set of Spring integrations that allow it to be used as a full-featured library for the data access layer in Spring applications.

Getting started

Installation

Maven:

<dependency>
    <groupId>com.github.marchenkoprojects</groupId>
    <artifactId>prettyjdbc-spring5</artifactId>
    <version>0.4.1</version>
</dependency>

Gradle:

compile 'com.github.marchenkoprojects:prettyjdbc-spring5:0.4.1'

Configuration

The basic configuration is as follows:

@Configuration
public void Config {

    @Bean
    public DataSource dataSource() {
        return ...
    }
    
    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource);
        return localSessionFactoryBean;
    }
}

The main bean that needs to be configured is LocalSessionFactoryBean which accepts configured DataSource.

If you want to use declarative transaction management in a Spring application, you need to @EnableTransactionManagement and configure PrettyJdbcTransactionManager.

@Configuration
@EnableTransactionManagement
public void Config {
    // LocalSessionFactoryBean and other configuration
    
    @Bean
    public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
        PrettyJdbcTransactionManager transactionManager = new PrettyJdbcTransactionManager();
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }
}

PrettyJdbcTransactionManager accepts configured SessionFactory from basic configuration.

In applications like Spring Web MVC you must use CurrentSessionManagementInterceptor to control the closure of the current session.

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CurrentSessionManagementInterceptor());
    }
}

About

This project is a decorator PrettyJDBC library for easy integration into Spring Framework 5.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages