-
Notifications
You must be signed in to change notification settings - Fork 0
/
APConfig.java
116 lines (100 loc) · 4.12 KB
/
APConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package tw.com.demo.config;
import oracle.jdbc.pool.OracleDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
/**
* Spring 的相關設定,與dispatcher-servlet.xml是一樣的
*/
@Configuration
@EnableWebMvc
@ComponentScan("tw.com.demo")
@EnableJpaRepositories("tw.com.demo.repository")
@EnableTransactionManagement
public class APConfig extends WebMvcConfigurerAdapter {
/**
* 導頁設定
*
*/
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
/**
* 資源設定,此資源為webapp裡面的資料
*
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
/**
* 以下為 資料庫連線的相關設定
*
*/
@Bean
public DataSource dataSource() {
// DriverManagerDataSource mySqlDataSource = new DriverManagerDataSource();
// mySqlDataSource.setDriverClassName("com.mysql.jdbc.Driver");
// mySqlDataSource.setUrl("jdbc:mysql://localhost:3306/spring_demo?useSSL=false");
// mySqlDataSource.setUsername("root");
// mySqlDataSource.setPassword("1234");
OracleDataSource oracleDataSource = null;
try {
oracleDataSource = new OracleDataSource();
oracleDataSource.setURL("jdbc:oracle:thin:@localhost:1521:xe");
oracleDataSource.setUser("dbo");
oracleDataSource.setPassword("1234");
} catch (SQLException e) {
e.printStackTrace();
}
return oracleDataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("tw.com.demo");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
factory.setJpaProperties(additionalProperties());
return factory.getObject();
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("spring.jpa.show-sql", "true");
properties.setProperty("spring.jpa.hibernate.ddl-auto ", "create-update");
return properties;
}
}