본문 바로가기
개발/개발환경설정

MySQL 연결

by 카리3 2020. 3. 16.

1. pom.xml

		<!-- 트랜잭션 처리 lib --> 
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version> 
		</dependency>
        
         <!-- MyBatis-Spring -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring-version}</version>
        </dependency>
		
		<!-- Spring-jdbc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        
		<!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-version}</version>
        </dependency>
        
         <!-- MyBatis -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${org.mybatis-version}</version>
        </dependency>
        

2. root-context.xml

	<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
        <property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3306/SECKIMDB?useSSL=false&amp;serverTimezone=UTC" />
		 <property name="username" value="SECKIM"></property>
        <property name="password" value="1234"></property>
    </bean> 
    
     <!-- MyBatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:/spring/appServlet/mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:/query/*.xml"></property>
    </bean>    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="secretary" />
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    	<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
  	</bean>
 
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  	<property name="dataSource" ref="dataSource" />
 </bean>

3. mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0/EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 
<configuration>
    
</configuration>

4. mapping-query-*.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="seckim.test"> 
    <select id="selectNow" resultType="String">
        SELECT NOW()
    </select>
</mapper>

5. 서비스 생성

package secretary.common.service;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 
    @Autowired
	private SqlSession sqlSession;
    
    public String selectNow() {
        return sqlSession.selectOne("seckim.test.selectNow");
    }
}

6. 컨트롤에서 호출

package secretary.common.controller;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import secretary.common.service.TestService;

@Controller
public class MainController {
	
	 @Autowired
	 private TestService testService;

	@RequestMapping(value = "/main.do")
	public String main(Locale locale, Model model) {
		String result = testService.selectNow();
		System.out.println("=======>main: "+result);

		return "common/main";
	}
}

 

'개발 > 개발환경설정' 카테고리의 다른 글

Interceptor 설정  (0) 2020.03.17
Aspectj + 어노테이션으로 로그남기기  (0) 2020.03.17
LOG4J 설정  (0) 2020.03.17
Spring MVC 설정  (0) 2020.03.12
개발환경설정  (0) 2020.02.15