com.mycompany.configuration.SpringDateConfigMySQL.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.configuration.SpringDateConfigMySQL.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.configuration;

import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

/**
 *
 * @author ?
 */
@Configuration
@EnableJpaRepositories(basePackages = "com.mycompany.repository")
@Import({ TransactionsConfig.class })

public class SpringDateConfigMySQL {
    @Bean
    //? ? 
    public DataSource dataSource() {
        //MySQL
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        //? 
        ds.setUrl("jdbc:mysql://localhost:3306/dateexam");
        ds.setUsername("root");
        ds.setPassword("root");
        /* ds.setUsername("exam");
        ds.setPassword("exam68");*/
        //
        ds.setInitialSize(5);
        ds.setMaxIdle(5);
        ds.setMaxTotal(15);

        return ds;
    }

    /**
     *  ???
     *
     * @param dataSource ? 
     * @param jpaVendorAdapter   ? 
     * @return  ???
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
            JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource);
        emf.setPersistenceUnitName("mycompany");
        emf.setJpaVendorAdapter(jpaVendorAdapter);
        // ? ?? ??
        emf.setPackagesToScan("com.mycompany.dto");
        return emf;
    }

    /**
     * ?? 
     *
     * @return ? 
     */
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        return adapter;
    }

    @Bean
    public BeanPostProcessor persistenceTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}