EzyJPA: Configuration

Updated at 1685687424000

1. Configuration List

EzyJPA provides a list of configurations in bellow table:

PropertyDefault ValueDescription
1datasource.jdbc_urlnullJDBC connection url
2datasource.usernamenullDatasource's username
3datasource.passwordnullDatasource's password
4datasource.driver_class_namenullJDBC driver class name
5datasource.database_namenullDatabase name

You can encrypt sensitive configuration like username, password if you want and please take a look HikariCP to find out another configuration.

2. An Example

You can put bellow configuration to your application.yaml file:

    # for application.yaml
    datasource:
      jdbcUrl: jdbc:mysql://root:12345678@localhost:3306/test
      driverClassName: com.mysql.cj.jdbc.Driver

Or for application.properties file:

    # for application.properties
    datasource.jdbcUrl=jdbc:mysql://root:12345678@localhost:3306/test
    datasource.driverClassName=com.mysql.cj.jdbc.Driver

3. Auto Configuration

To configure automatically, you just need add configuration and add ezyfox-boot-autoconfigure to your project dependency like this:

com.tvd12ezyfox-boot-autoconfigure1.1.1

4. Configure Manually

For some reason, you can not use ezyfox-boot-autoconfigure you can config like this:

    import com.tvd12.ezydata.database.EzyDatabaseContext;
    import com.tvd12.ezydata.jpa.EzyJpaDatabaseContextBuilder;
    import com.tvd12.ezydata.jpa.loader.EzyJpaDataSourceLoader;
    import com.tvd12.ezydata.jpa.loader.EzyJpaEntityManagerFactoryLoader;
    import com.tvd12.ezyfox.bean.EzyBeanConfig;
    import com.tvd12.ezyfox.bean.EzyPackagesToScanAware;
    import com.tvd12.ezyfox.bean.EzySingletonFactory;
    import com.tvd12.ezyfox.bean.EzySingletonFactoryAware;
    import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
    import com.tvd12.ezyfox.bean.annotation.EzyConfigurationBefore;
    import com.tvd12.ezyfox.util.EzyPropertiesAware;
    import lombok.Setter;
    import javax.persistence.EntityManagerFactory;
    import javax.sql.DataSource;
    import java.util.Properties;
    import java.util.Set;
    import static com.tvd12.ezyfox.boot.util.EzyDatabaseContexts.addRepositoriesFromDatabaseContextToSingletonFactory;
    @Setter
    @EzyConfigurationBefore
    public class EzyJpaConfiguration implements
        EzyBeanConfig,
        EzyPropertiesAware,
        EzySingletonFactoryAware {
        private Properties properties;
        @EzyAutoBind
        private EzySingletonFactory singletonFactory;
        @Override
        public void config() {
            addRepositoriesFromDatabaseContextToSingletonFactory(
                databaseContext(),
                singletonFactory
            );
        }
        private EzyDatabaseContext databaseContext() {
            return new EzyJpaDatabaseContextBuilder()
                .properties(properties)
                .entityManagerFactory(entityManagerFactory())
                .scan("your_package_to_scan")
                .build();
        }
        private EntityManagerFactory entityManagerFactory() {
            return new EzyJpaEntityManagerFactoryLoader()
                .entityPackages(packagesToScan)
                .dataSource(dataSource())
                .properties(properties)
                .load("Default");
        }
        private DataSource dataSource() {
            return new EzyJpaDataSourceLoader()
                .properties(properties, "datasource")
                .load();
        }
    }

Next

You can take a look a list of default functions.