没有XML配置的Spring Application和Hibernate Application

时间:2017-04-25 08:10:10

标签: spring hibernate

最近在一次采访中,有人问我是否可以在没有XML配置的情况下使用Spring Application或Hibernate Application。? 那么最好的答案是什么,请解释一下。

1 个答案:

答案 0 :(得分:0)

是的,你可以,例如: - >帐户

@Entity
public class Account {
    @Id
    @GeneratedValue
    private Long id;
    @Temporal(TemporalType.DATE)
    private Data data;
    private Long accountNumber;
    private String owner;
    private double balance;

    public Long getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(Long accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "accountNumber=" + accountNumber +
                ", owner='" + owner + '\'' +
                ", balance=" + balance +
                '}';
    }
}

<强>道:

@Repository
public class AccountDao {

    @PersistenceContext
    EntityManager entityManager;

    @Transactional
    public void save(Account account) {
        entityManager.persist(account);
    }

    protected Account getByKey(Long key) {
        return (Account) entityManager.find(Account.class, key);
    }
}

配置 - &gt;

@Configuration
@EnableTransactionManagement
@ComponentScan("your scan package path")
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/your_data_base_name");
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setPassword("password");
        return driverManagerDataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource());
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(getHibernateJpaVendorAdapter());
        localContainerEntityManagerFactoryBean.setJpaProperties(additionalProperties());
        localContainerEntityManagerFactoryBean.setPackagesToScan("entity_package_name");
        return localContainerEntityManagerFactoryBean;
    }

    @Bean
    public JpaVendorAdapter getHibernateJpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(true);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(entityManagerFactory().getObject());
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate_show_sql", true);
        return properties;
    }
}

测试类:

public class Test {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        Account account = new Account();
        account.setAccountNumber(123L);
        account.setBalance(100056.5);

       AccountService accountService = applicationContext.getBean(AccountService.class);
        accountService.save(account);
    }
}