hibernate annotation

Spring 4 + Hibernate 5 integration example with zero xml

GitHub code:feat: 注释方式配置Hibernate连接数据库

代替applicationContext.xml , hibernate.cfg.xml , 和 Phone.hbm.xml

package club.cser.springroad.hibernate.client;

import club.cser.springroad.hibernate.entity.Computer;
import com.mchange.v2.c3p0.ComboPooledDataSource;
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.DataSourceTransactionManager;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties;

@Configuration // 创建bean
@EnableTransactionManagement
@PropertySource("classpath:db.properties") // 读取properties文件
@ComponentScan("club.cser.springroad.hibernate")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        dataSource.setJdbcUrl(env.getProperty("jdbc.jdbcUrl"));
        try {
            dataSource.setDriverClass(env.getProperty("jdbc.driverClass"));
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        dataSource.setInitialPoolSize(Integer.parseInt(env.getProperty("jdbc.initPoolSize")));
        dataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("jdbc.maxPoolSize")));
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(getDataSource());

        Properties props=new Properties();
        props.put("hibernate.show_sql", true);
        props.put("hibernate.format_sql", true);
        // props.put("hibernate.hbm2ddl.auto", "update");
        props.put("hibernate.dialect.storage_engine", "innodb");

        factoryBean.setHibernateProperties(props);
        factoryBean.setAnnotatedClasses(Computer.class);
//        factoryBean.setAnnotatedPackages("");
        return factoryBean;
    }

    @Bean(name = "transactionManager")
    public TransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }

}

entity直接注解类

定义详细列

https://stackoverflow.com/questions/3110266/how-to-set-a-default-entity-property-value-with-hibernate

@DynamicInsert + @Column(name = "buy_date", columnDefinition="datetime default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP") 详细定义列

transaction

xml配置里设置了切面,这里配置bean的类上直接@EnableTransactionManagement开启,然后在dao方法上@Transactional

注意方法上一定要有@Transactional,因为sessionFactory.getCurrentSession()一定要和事务绑定,否则会报错。

Last updated

Was this helpful?