관리 메뉴

HeBhy, since 1983.

Spring JDBC (jpa & mybatis) setup in Spring Boot 2.x (with lombok, mariaDB, maven) 본문

Dev/Web

Spring JDBC (jpa & mybatis) setup in Spring Boot 2.x (with lombok, mariaDB, maven)

HeBhy 2018. 9. 18. 02:03

1. application.properties 설정 (using mariaDB, maven)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ===========================
# JDBC
# ===========================
spring.datasource.url=jdbc:mysql://localhost:3306/testDB?useSSL=false&useUnicode=yes&characterEncoding=UTF8&autoReconnect=true&autoReconnectForPools=true&allowMultiQueries=true
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.username=foouser
spring.datasource.password=foopass
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
 
# ===============================
# JPA / HIBERNATE
# ===============================
# hibernate.hbm2ddl.auto속성과 동일: none(운영서버), create-drop/create(개발), drop
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
cs


 *pom.xml 설정은.. 인터넷에 많으니 pass

 *STS에서 lombok annotation처리위해선 lombok.jar 파일을 더블클릭해서 설치필요

  (참고: https://projectlombok.org/setup/eclipse)



2. DBConfig.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
@Configuration
@MapperScan(basePackages = "com.test")
@EnableTransactionManagement
public class DBConfig {
 
  // Private fields
  @Autowired
  private Environment env;
 
  @Autowired
  private DataSource dataSource;
 
  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
 
    entityManagerFactory.setDataSource(dataSource);
    entityManagerFactory.setPackagesToScan(new String[] { "com.test" }); // Classpath scanning of @Component, @Service, etc annotated class
    entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
 
    Properties additionalProperties = new Properties(); // Hibernate properties
    additionalProperties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
    additionalProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
    additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
    entityManagerFactory.setJpaProperties(additionalProperties);
 
    return entityManagerFactory;
  }
 
  @Bean
  public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
  }
 
  @Bean
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
  }
 
  @Bean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sessionFactory.setMapperLocations(resolver.getResources("classpath*:SQL/**/*Mapper.xml"));
    Properties property = new Properties();  // DB에서 사용할 값들 설정
    property.setProperty("key""val");
    sessionFactory.setConfigurationProperties(property);
    return sessionFactory.getObject();
  }
 
  @Bean
  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
    final SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
    return sqlSessionTemplate;
  }
 
}
cs



3. jpa는 @Entity 쓰고, @Data 도 활용해서 DB 처리한다..

 -sql 이나 mapper.xml 은 'src/main/resources/SQL' 이하에 폴더만들어 구분 저장.

Comments