-
728x90
๐ Setting
- java : 11
- oracle : 11c
- framework : spring Boot / maven
๐ป application.yml
datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: username: password:
- url
1) Oracle9 ๋ฒ์ ์ธ ๊ฒฝ์ฐ "jdbc:oracle:thin:@localhost:1521:XE"
2) Oracle11 ๋ฒ์ ์ธ ๊ฒฝ์ฐ "jdbc:oracle:thin:@localhost:1521:orcl"- username
oracle ์ ์ ๋ช
- password
oracle ์ ์ ์ ํจ์ค์๋
๐ป DBConfig.java
@Configuration public class DBConfig { @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Autowired ApplicationContext applicationContext; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setConfigLocation(applicationContext.getResource("classpath:/mybatis/mybatis-config.xml")); factoryBean.setMapperLocations(applicationContext.getResources("classpath:/mapper/*Mapper.xml")); return factoryBean; } @Bean public SqlSessionTemplate sqlSession(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
๐ป pom.xml
<!-- spring jdbc --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency> <!-- spring jdbc -->
maven ์ ๋ฐ์ดํธ ํ C:\Users\.m2\repository\com\oracle\database\jdbc\ojdbc8\12.2.0.1 ๋ด์ jar ํ์ผ์ด ์์ฑ๋์ง ์๋๋ค๋ฉด maven ํํ์ด์ง์์ ์ง์ jar ํ์ผ์ ๋ฐ์์ค๋๋ค.
- ๋ค์ด๋ก๋ ๋งํฌ
https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc8/12.2.0.1
๐ TEST
๐ป testDto
@Data public class TestDto { private int id; private String name; private String memo; }
๐ป testDao
@Repository public class TestDao { @Autowired private SqlSessionTemplate sqlSessionTemplate; public TestDto getTestInfo(int id) { return sqlSessionTemplate.selectOne("testMapper.getTestInfo", id); } }
๐ป testService
@Service public class TestService { @Autowired private TestDao testDao; public TestDto getTestInfo(int id) { return testDao.getTestInfo(id); } }
๐ป testController
@Controller public class TestController { @Autowired private TestService testService; @GetMapping("/test") @ResponseBody public void test() { TestDto obj = testService.getTestInfo(1); System.out.println("์ฑ๊ณต : " + obj.getId() + " / " + obj.getName() ); } }
๐ป testMapper
<?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="testMapper"> <select id="getTestInfo" parameterType="Int" resultType="domain.test.dto.TestDto"> SELECT * FROM test WHERE id = #{id} </select> </mapper>
๐ป Query
create table test (id int, name varchar(50), memo varchar(50)); INSERT INTO test VALUES(1,'์ตํ ์คํธ','ํ ์คํธ ๋ฐ์ดํฐ'); INSERT INTO test VALUES(2,'์ดํ ์คํธ','ํ ์คํธ ๋ฐ์ดํฐ'); INSERT INTO test VALUES(3,'๊นํ ์คํธ','ํ ์คํธ ๋ฐ์ดํฐ');
๐ป ๊ฒฐ๊ณผ
728x90'spring boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ