[Spring] mysql 연동

개발자·2021년 2월 19일
0
post-thumbnail

🤢 mysql로 변경

예전에 만들어 둔 Spring 템플릿이 mariaDB 연동이었는데 맥북에 mysql만 설치되어 있어서 바꿔주었다.

pom.xml

  • DB연동에 필요한 라이브러리를 설정해준다.
<!-- Database & myBatis -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>${org.springframework-version}</version>
</dependency>
<dependency>
 	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.5.4</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
	<version>2.0.4</version>
</dependency>
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-dbcp2</artifactId>
	<version>2.7.0</version>
</dependency>
<dependency> 
	<groupId>mysql</groupId> 
	<artifactId>mysql-connector-java</artifactId> 
	<version>5.1.23</version> 
</dependency>

root-context.xml

1. web.xml

  • context-로 시작하고 xml로 끝나는 파일을 읽어 웹 서비스를 구성하기 위한 설정
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		    /WEB-INF/spring/root-context.xml
		    classpath*:spring/context-*.xml
	</param-value>
</context-param>

2. context-datasource.xml

<bean id="dataSourceOrigin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/[스키마명]?useSSL=false&amp;serverTimezone=UTC&amp;autoReconnection=true&amp;allowMultiQueries=true" />
        <property name="username" value="[mysql account]" />
        <property name="password" value="[mysql pw]" />
        <!-- validationQuery:유효 검사용 쿼리( 1개 이상의 row를 반환하는 쿼리를 넣어주면 된다. ) -->
        <property name="validationQuery" value="select 1" />
        <!-- testWhileIdle:컨넥션이 놀고 있을때 -_-; validationQuery 를 이용해서 유효성 검사를 할지 여부. -->
        <property name="testWhileIdle" value="true" />
        <!-- timeBetweenEvictionRunsMillis:해당 밀리초마다 validationQuery 를 이용하여 유효성 검사 진행 -->
        <property name="timeBetweenEvictionRunsMillis" value="600000" />
</bean>

<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
        <constructor-arg ref="dataSourceOrigin" />
        <property name="logFormatter">
            <bean class="com.my.common.log.Log4JdbcCustomFormatter">
                <property name="loggingType" value="MULTI_LINE" /><!-- 여러줄에 출력하려고 하시면 코멘트 해제 -->
                <property name="margin" value="8" />
                <property name="sqlPrefix" value="SQL : " />
            </bean>
        </property>
</bean>

3. context-mapper.xml

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost:3306/[스키마명]" />
	<property name="username" value="[mysql account]" />
	<property name="password" value="[mysql pw]" />
</bean>

<!-- mapper 자동 적용을 위한 Custom RefreshableSqlSessionFactoryBean을 사용(개발시에만 사용 권장) -->
<bean id="sqlSessionFactory" class="com.my.common.db.RefreshableSqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml" />
	<property name="interval" value="1000" />
</bean>
profile
log.info("공부 기록 블로9")

0개의 댓글