Spring Data - Hibernate的配置

//相当于sqlSessionFactoryBean这个类的配置
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource);
        //扫描加了@Entity的类
        bean.setPackagesToScan("com.wgc.persons.demo.entity");
        bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

        Properties properties = new Properties();
        //就在启动服务是,它会查看我们实体中的字段是否与数据库一致,如果不一致,它会自动修改,
        //如果把update该create , 它会自动在数据库创建相对应的表
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        //是不是显示sql语句
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "true");
        /* MySQLDialect 如果是使用MariaDB就要改为 MariaDBDialect */
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        bean.setJpaProperties(properties);
        return bean;
    }

 

posted @ 2018-12-27 15:10  YouAreABug  阅读(189)  评论(0编辑  收藏  举报