首页>代码>Spring4+Hibernate4+MySQL+Maven项目整合Java Web实例(Annotations+XML)>/src/main/java/com/websystique/springmvc/configuration/HibernateConfiguration.java
package com.websystique.springmvc.configuration; import java.util.Properties; import javax.sql.DataSource; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement @ComponentScan({ "com.websystique.springmvc.configuration" }) @PropertySource(value = { "classpath:application.properties" }) public class HibernateConfiguration { @Autowired private Environment environment; @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan(new String[] { "com.websystique.springmvc.model" }); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); return dataSource; } private Properties hibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); return properties; } @Bean @Autowired public HibernateTransactionManager transactionManager(SessionFactory s) { HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(s); return txManager; } }

行于足下 LV1
2022年1月16日
1342203642 LV10
2021年2月8日
siximu912 LV10
2019年10月11日
123456请问 LV2
2019年7月15日
18217574564 LV2
2019年6月21日
落魄王公 LV10
2019年5月15日
m835036934 LV2
2019年3月26日
wy120678 LV8
2019年2月22日
三生石sh1 LV13
2019年1月18日
1529860026 LV24
2018年12月3日

暂无贡献等级
3038169233 LV1
2024年6月29日
星辰xc LV2
2023年5月7日
187236 LV4
2023年3月23日
isfrand LV4
2022年4月18日
dukie123 LV1
2022年2月16日
l2261327910 LV2
2022年2月9日
行于足下 LV1
2022年1月16日
Zeorwyc LV8
2021年11月30日
sharon_taozi LV1
2021年11月5日