Spring DB

  1 min read  

Spring Database 설정

1. 스프링 프레임워크 DB

JNDI 활용

1.JNDI란?
○ Java Naming and Directory interface의 약자로 DB Pool을 미리 Naming 시켜두는 방법(DB 연결방법 중 하나)

pom.xml에 dependancy 추가

<!-- mybatis, mybatis spring 의존성 추가 -->    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.4</version>
    </dependency>    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
    </dependency>    
<!-- mysql 커넥터 의존성 추가 -->    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>    
<!-- 스프링 JDBC 의존성 추가 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-version}</version>
    </dependency>        


DB 연동을 위해 Tomcat의 context.xml파일 생성

    <Resource
    	name="jdbc/edudb"
    	auth="Container"
    	type="javax.sql.DataSource"
    	driverClassName="com.mysql.cj.jdbc.Driver"
    	url="jdbc:mysql://localhost:3306/edudb?serverTimezone=Asia/Seoul"
    	username="root"
    	password="rmarudgh6697!"
    	maxTotal="20"
    	maxIdle="10"
    	maxWaitMillis="20000"
    />


src/main/resources 하단에 root-context.xml 파일 (mapper 경로 설정)

	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />		
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:mybatis/**/*Mapper.xml"></property>	
	</bean>
	
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:/comp/env/jdbc/edudb"></property>	
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="ideatec.edu.spring.frwk.tomcat.mapper" />	
	</bean>



2. 스프링 부트 DB

pom.xml에 dependancy 추가

<!-- jdbc starter(spring-boot-starter-tomcat) 의존성 추가 --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
		
<!-- mybatis framework 의존성 추가 -->     
 		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

<!-- mySql 의존성 추가 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

application.properties 파일에 DB연동을 위한 설정 정보를 입력

#jdbc
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/edudb?serverTimezone=Asia/Seoul

spring.datasource.username=oscar

spring.datasource.password=qwe123!@#

#mybatis
mybatis.type-aliases-package:spring.boot.tomcat.mapper/TestMapper

mybatis.mapper-locations:mapper/*.xml


SpringBootApplication에 다음과 같이 설정해주며 mapper위치를 설정해 줍니다.

	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		
		Resource[] res 
			= new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml");
		sessionFactory.setMapperLocations(res);
		
		return sessionFactory.getObject();
	}
profile img

oscar

Why do you make efforts commonly, dont't want to live commonly.

Read more