먼저 mysql이 설치되어 있어야한다.
리눅스에서 docker container로 mysql을 설치하는 방법이다.
https://palyoung.tistory.com/76
1. pom.xml 파일에 dependency 추가
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2. application.properties 파일에 아래 내용 추가
db id, pw는 설치하신 MySQL 정보 입력
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.21.111.144:3306/test
spring.datasource.username=root
spring.datasource.password=password
3. DatabaseConfig.java 파일 추가
package com.example.demo;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@MapperScan(basePackages="com.example.demo")
@EnableTransactionManagement
public class DataBaseConfig {
public class DatabaseConfig {
@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:com/example/dao/*.xml"));
return sessionFactory.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
final SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}
}
}
4. DbController.java 파일 추가
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.DbService;
@RestController
public class DbController {
@Autowired
DbService dbService;
@RequestMapping("/")
public @ResponseBody String root_test() throws Exception{
return "Hello World";
}
@RequestMapping("/now")
public @ResponseBody String now() throws Exception{
return dbService.getDual();
}
}
5. DbService.java 파일 추가
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dao.DbMapper;
@Service
public class DbService {
@Autowired
DbMapper dbMapper;
/* select dual */
public String getDual() throws Exception{
return dbMapper.getDual();
}
}
6. DbMapper.java 파일 추가
package com.example.demo.dao;
public interface DbMapper {
/* DB Select */
public String getDual() throws Exception;
}
7. Db.xml 파일 추가
src/main/resources/com/exmaple/dao/Db.xml
<?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="com.example.demo.dao.DbMapper">
<!-- /* select dual */ -->
<select id="getDual" resultType="java.lang.String">
SELECT NOW() FROM DUAL
</select>
</mapper>
8. 웹브라우져 확인
http://localhost:8080/now
2019-03-29 02:27:57
'Spring Boot' 카테고리의 다른 글
스프링부트 로그 설정 (0) | 2019.03.29 |
---|---|
[Spring Boot] MyBatis + Mysql 샘플2 (0) | 2019.03.29 |
DAO, DTO, VO란? (0) | 2019.03.28 |
스프링 MVC 패턴이란? (0) | 2019.03.28 |
WEB MVC HelloWorld (0) | 2019.03.28 |