栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在不使用XML的情况下配置JPA / Hibernate / PostgreSQL

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在不使用XML的情况下配置JPA / Hibernate / PostgreSQL

将以下片段放入带有

@Configuration
和注释的类中
@EnableTransactionManagement

Hibernate / JPA(编辑packagesToScan字符串):

@Beanpublic LocalContainerEntityManagerFactoryBean entityManagerFactory() {    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();    em.setDataSource(dataSource());    em.setPackagesToScan(new String[] { "com.XY.model" });    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();    em.setJpaVendorAdapter(vendorAdapter);    em.setJpaProperties(additionalProperties());    return em;}Properties additionalProperties() {    Properties properties = new Properties();    properties.setProperty("hibernate.hbm2ddl.auto", "update");    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");    properties.setProperty("hibernate.show_sql", "true");    return properties;}

数据源(编辑用户名,密码和主机地址):

@Beanpublic DataSource dataSource() {    DriverManagerDataSource dataSource = new DriverManagerDataSource();    dataSource.setDriverClassName("org.postgresql.Driver");    dataSource.setUrl("jdbc:postgresql://localhost:port/DB_NAME");    dataSource.setUsername("root");    dataSource.setPassword("");    return dataSource;}

交易经理:

@Beanpublic PlatformTransactionManager transactionManager(EntityManagerFactory emf) {    JpaTransactionManager transactionManager = new JpaTransactionManager();    transactionManager.setEntityManagerFactory(emf);    return transactionManager;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/447802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号