Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- IntelliJ
- CloudWatch
- 정보처리기사 실기
- CKA
- CKA 기출문제
- 공부
- 코틀린 코루틴의 정석
- Spring
- aws
- 정보처리기사 실기 기출문제
- kotlin
- MySQL
- APM
- kotlin coroutine
- Kubernetes
- Elasticsearch
- 정보처리기사실기 기출문제
- 오블완
- AWS EKS
- mysql 튜닝
- kotlin spring
- kotlin querydsl
- Java
- PETERICA
- Linux
- Pinpoint
- AI
- minikube
- 기록으로 실력을 쌓자
- 티스토리챌린지
Archives
- Today
- Total
피터의 개발이야기
[Spring] Mybatis 연동 본문
반응형
오늘은 Mybatis연동을 정리합니다.
gradle 설정
//DataBase Mybatis mysql
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.6'
compile group: 'org.mybatis', name: 'mybatis-typehandlers-jsr310', version: '1.0.2'
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.2'
compile group: 'mysql', name: 'mysql-connector-java'
DDBConnectionConfig
@Configuration
@Slf4j
public class DBConnectionConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String dbUsername;
@Value("${spring.datasource.password}")
private String dbPassword;
@Value("${spring.datasource.classname}")
private String dbClassName;
@Bean
public DataSource dataSource() {
final HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setUsername(dbUsername);
hikariConfig.setPassword(dbPassword);
hikariConfig.addDataSourceProperty("url", dbUrl);
hikariConfig.setDataSourceClassName(dbClassName);
hikariConfig.setLeakDetectionThreshold(2000);
hikariConfig.setMaximumPoolSize(30);
hikariConfig.setPoolName("peterPool");
final HikariDataSource dataSources = new HikariDataSource(hikariConfig);
return dataSources;
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:config/mybatis-config.xml"));
sessionFactory.setVfs(SpringBootVFS.class);
return sessionFactory.getObject();
}
}
Application.properties
############
# Database #
############
spring.datasource.classname=com.mysql.cj.jdbc.MysqlDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=UTC&allowMultiQueries=true
spring.datasource.username=username
spring.datasource.password=password
# mybatis 매핑 type을 짧게 쓰기 위한 설정
mybatis.type-aliases-package=com.peterica.swagger.model
Controller
@GetMapping("/dbTest")
@ApiOperation(httpMethod = "GET", value = "Mybatis Test", notes = "Mybatis Test")
public List<String> dbTest(){
return testService.getTest();
}
Service
@Service
@RequiredArgsConstructor
public class TestService {
private final TestRepository testRepository;
public List<String> getTest(){
return testRepository.getTestList();
}
}
Repository
@Repository
@RequiredArgsConstructor
public class TestRepository {
private final SqlSession sqlSession;
public List<String> getTestList(){
return sqlSession.selectList("test.getTest");
}
}
Mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
<select id="getTest" resultType="String">
select memo
from test
</select>
</mapper>
Mysql
create table test
(
memo varchar(12) null
)
charset = utf8;
insert into test (memo) value ('test');
웹 호출
데이터 조회 성공.
반응형
'Programming > Spring' 카테고리의 다른 글
[SpringBoot] SpringBoot 슬랙 연동하기 (0) | 2021.01.09 |
---|---|
[Spring] Spring Boot에 Swagger 붙이기 (0) | 2021.01.04 |
[Spring] SpringBoot를 kill 하는 3가지 방법 (0) | 2020.12.26 |
[Spring] PDF의 첫장 섬네일 만들기 (0) | 2020.12.24 |
[Spring] PDF을 이미지 파일로 변환하기 (0) | 2020.12.23 |
Comments