ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • spring boot
    [Spring Boot] Maven ํ™˜๊ฒฝ Oracle ์—ฐ๋™ํ•˜๊ธฐ
    2023. 10. 14. 12:12
    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
Designed by Tistory.