일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- mysql
- rollbackOnly
- 금융공학
- paper2d
- mastering unreal engine
- 월화수목금토익
- JPA
- 삼성 플렉스북
- AT&T
- flex하는게 아니었는데..
- 주식
- java
- 최근증시
- jdk17
- spring message converters
- spring
- spring security
- 펜실베이니아
- 유학
- HRD
- kospi
- 증시
- 어학연수
- 학생비자
- 필라델피아
- weblogic
- UE4
- ue5
- @JsonView
- 인물
Archives
- Today
- Total
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:031. 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' 이하에 폴더만들어 구분 저장.
'Dev > Web' 카테고리의 다른 글
Thymeleaf 3.x 이상 사용 주의사항 (over Spring security 5.x) (0) | 2018.10.30 |
---|---|
JPA entity mapping (@OneToOne Lazy Loading) (0) | 2018.10.11 |
Spring Session using Redis 5.0.10 in Spring Boot 2.x (with cluster) (0) | 2018.09.18 |
CSS한줄로 a태그의 링크 포커스를 없애봅시다! (onfocus=blur 대신) (1) | 2010.12.17 |
Caching Solution for JAVA (WAS) (0) | 2010.05.25 |
Comments