0
最近在一次采访中,有人问我是否有可能拥有没有XML配置的Spring应用程序或Hibernate应用程序..? 那么最好的答案是什么,请解释一下。没有XML配置的Spring应用程序和Hibernate应用程序
最近在一次采访中,有人问我是否有可能拥有没有XML配置的Spring应用程序或Hibernate应用程序..? 那么最好的答案是什么,请解释一下。没有XML配置的Spring应用程序和Hibernate应用程序
当然可以,e.g: - >账户
@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);
}
}
配置 - >
@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);
}
}
是有两种类型的配置,即XML的基础和注释基于配置 –
我问:“是有可能有Spring应用程序或休眠没有XML配置应用程序” ..? –
那就是我的评论建议。是的 –