나의개발일지

Spring) STS와 MySQL연결하기 for.Mac 본문

Back-End/Spring

Spring) STS와 MySQL연결하기 for.Mac

아. 이렇게 하면 될거 같은데.. 2023. 11. 10. 00:58
728x90

0. 사전준비

     MySQL과 MySQL WorkBench를 다운로드 하기 전에 기존에 있던 MySQL과 MySQL WorkBench를 삭제한다.

     (현재 맥 os SONOMA 는 WorkBench가 튕기는 문제가 발생한다.)

1. MySQL, MySQL WorkBench 다운로드

  - MySQL 사이트 접속한다.

     https://dev.mysql.com/downloads/mysql/

 

MySQL :: Download MySQL Community Server

Select Version: 8.2.0 Innovation 8.0.35 5.7.44 Select Operating System: Select Operating System… Microsoft Windows Ubuntu Linux Debian Linux SUSE Linux Enterprise Server Red Hat Enterprise Linux / Oracle Linux Fedora Linux - Generic Oracle Solaris macOS

dev.mysql.com

 

- Archives를 선택하여 이전 버전 선택 창으로 간다.

 

 

  - 버전을 선택후 맥북 기종에 맞는 DMG파일을 선택한다.  (M1 이상 : ARM, intel : x86)

 

 

- MySQL 설치한다.     *중요*(MySQL 설치시 root계정의 패스워드는 꼭기억해야한다)

 

2. MySQL WorkBench 설치

  - MySQL WorkBench 다운로드사이트 접속

     https://downloads.mysql.com/archives/workbench/

 

MySQL :: Download MySQL Workbench (Archived Versions)

Please note that these are old versions. New releases will have recent bug fixes and features! To download the latest release of MySQL Workbench, please visit MySQL Downloads. MySQL open source software is provided under the GPL License.

downloads.mysql.com

 

- 버전확인후 다운로드를 실행한다.    *중요*(버전은 MySQL버전과 맞춘다)

 

  - MySQL WorkBench 실행후 root 계정으로 접속  *중요*(아까 설정한 루트 패스워드 입력)

 

 

3. STS pom.xml 변경

  - Maven repository에 접속후 spring 버전에 맞는 spring jdbc 코드를 복사한다

    https://mvnrepository.com/artifact/org.springframework/spring-jdbc

 

 

  - pom.xml 파일에 추가한다.

 

  -  MySQL Connecter/J 리파지토리도 코드를 복사하여 pom.xml 파일에 넣는다.  *중요* (MySQL 버전과 맞춘다)

    https://mvnrepository.com/artifact/com.mysql/mysql-connector-j

 

4.  jdbc-context 파일 생성

  - 프로젝트/src/main/webapp/WEB-INF/spring 폴더 안에 jdbc-context파일을 생성한다.

 

  - jdbc-context안에 아래의 내용을 추가한다.  *중요* (password이름의 property에 value값은 아까 설정한 root비밀번호이다)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/db_library"/>
		<property name="username" value="root"/>
		<property name="password" value="%루트 비밀번호%"/>
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

</beans>

 

5. web.xml 파일 수정

  - web.xml 파일에 context-param 추가  (web.xml 파일은 프로젝트/src/main/webapp/WEB-INF 안에 있다)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring/root-context.xml
    /WEB-INF/spring/jdbc-context.xml
    </param-value>
</context-param>

 

6. 연동

이제 @Autowired를 이용하여 JdbcTemplate를 가져와서 프로젝트에 적절한 sql문을 사용해서 database를 관리 하면 된다.

 

728x90
반응형

'Back-End > Spring' 카테고리의 다른 글

Spring) DI 의존성 주입  (0) 2024.04.15
Spring) IoC & Container  (0) 2024.04.15
Spring) SpringMVC  (1) 2024.01.04
Spring) Row Mapper  (1) 2023.12.18
Spring) 쿠키와 세션  (1) 2023.12.14