일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- debian9
- 자바
- server.xml
- Servlet
- JDK
- ubuntu
- debian9 eclips
- jsp
- openjdk
- MySQL
- jre
- Linux
- linux eclips
- 재정렬
- 리눅스
- eclips install
- 18.04
- 우분투 이클립스
- eclips
- debian
- java
- 우분투18.04
- 리눅스 이클립스
- 데비안 이클립스
- ubuntu 18.04
- 우분투 후기
- 이클립스
- 데비안9
- 리눅스에서 이클립스
- 우분투 사용
- Today
- Total
실성한 developer
[ Spring ] STS Mysql Mybatis 연동 본문
스프링 어렵네 이해 했다고 생각했던 부분도
다른 분들 예제 코드를 보면 색다르다. 갈길이 멀다.
먼저 maven을 사용할려고 한다.
maven은 쉽게 생각해서 프로젝트를 관리를 편하게 해주는 도구이다. 라이브러리를 하나하나 다운받아서 넣어주는게 아닌 pom.xml에 적어 라이브러리를 가져와 빌드까지 해주는 아주 편리한 도구라고 생각하자.
이제 pom.xml에서 필요한 라이브러리를 가져올꺼다. <dependencies>안에 붙쳐 넣으면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <!-- JunitTest --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.8.RELEASE</version> </dependency> | cs |
이렇게 적어주면 version에 적힌 버전을 다운 받아 온다. 이클립스나 STS 오른쪽 하단에 보면 라이브러리를 다운받고 있는게 보임
라이브러리 버전이 궁금하거나 내가 필요한 라이브러리를 찾아야 되겠다 하면
https://mvnrepository.com/ 가서 검색해서 위에 방식처럼 코드가 있다. 그거 복사 붙쳐넣기 하면 된다.
그럼 이제 IDE와 mysql이 먼저 연동 되는지 부터 확인하자
src/main/webapp/WEB-INF/spring/appServlet/root-context.xml이 있다.
root-context.xml을 보면 밑에 source , namespaces, overview aop beans등등이 보인다.
namespaces를 클릭하면
이렇게 나온다. 내가 체크한 부분은 한번쯤은 들어본 단어들일것이다.
이렇게 체크후 source부분으로 가면 뭔가 많이 적혀있을꺼다.
이렇게 자동으로 추가되었다.
그럼 <beans></beans>사이에 추가 해주면 된다.
1 2 3 4 5 6 | <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/스미카(데이터베이스명)?useSSL=false&serverTimezone=UTC" /> <property name="username" value="DB계정명"></property> <property name="password" value="계정비밀번호"></property> </bean> | cs |
jsp에서 보였던 부분들이 많다.
지금은 db가 같은곳에 위치하기때문에 localhost이지만 나중에는 db주소를 주고 포트번호를 주면된다.
Connection부분은 끝 그럼 테스트 한번해보자. 패키지에 보면 src/test/java밑에 테스트파일하나 만들자
dbTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.test.controller; import java.sql.Connection; import javax.inject.Inject; import javax.sql.DataSource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"file:src/min/webapp/WEB-INF/spring/**/root-context"}) public class dbTest { @Inject private DataSource ds; @Test public void testConnection() throws Exception { try(Connection con = ds.getConnection()) { System.out.println("Connection : " + con + "\n"); } catch (Exception e) { e.printStackTrace(); } } } | cs |
이렇게 작성후
이 파일을 우클릭 -> Run As -> JUnit Test 를 하게 되면 옆에 창하나가 뜨면서 알아서 검사한다.
게이지가 초록색으로 찼으면 Console창에 con이 찍혀있다. 아니면 에러를 보여준다.
연결 성공했으면 이제 Mybatis설정을 한다.
Mybatis는 DAO와 DB 사이에서 동작하는 도구이다. 수많은 쿼리를 xml에 한번에 관리할 수 있어 가독성이 매우 좋다. 그리고 무엇보다도 실무에서 많이 쓴다.
이번에는 src/main/resources에 파일을 하나 추가한다. 나는 mybatis-config.xml로 보기 쉽게 했다.
1 2 3 4 5 | <?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> | cs |
지금은 필요하지 않기에 여기까지만 해도 된다.
그리고 아까 root-servlet.xml에 bean을 하나더 추가하자.
1 2 3 4 | <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:/mybatis-config.xml" /> </bean> | cs |
classpath부분에 아까 resource파일 밑에 만든 파일을 적어준다.
그리고 src/test/java밑에 테스트파일 하나 더 만들어서 테스트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.test.controller; import javax.inject.Inject; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"}) public class mybatisTest { @Inject private SqlSessionFactory sqlFactory; @Test public void testFactory() throws Exception { System.out.println("Mybatis Connection -------" + sqlFactory); } @Test public void testSession() throws Exception { try(SqlSession sqlSession = sqlFactory.openSession() ) { System.out.println(); } catch (Exception e) { e.printStackTrace(); } } } | cs |
아까와 동일하게 이 파일도 JUnit Test하여 별문제가 없이 로그와 초록색게이지가 꽉 차면 설정 끝이다.
'Study > Spring' 카테고리의 다른 글
[SPRING] Spring boot에서 MYSQL + JPA + REST API 설정 (1) | 2020.12.26 |
---|