시작하며
Dream Hanks의 마츠시타입니다. 이번에는 Spring과 MySql과 MyBatis의 연동방법을 설명하겠습니다.
xml 파일 설정
새로 프로젝트를 만들 때 기본적으로 생성되는 3개의 xml 파일을 커스터마이즈를 해드리겠습니다.
◆pom.xml 설정
아래의 dependency(의존하는 라이브러리)를 기본 생성된 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 37 38 |
<!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- MyBatis 3.4.1 --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- MyBatis-Spring --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</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> <!-- Mybatis log --> <!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 --> <dependency> <groupId>org.bgee.log4jdbc-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc4</artifactId> <version>1.16</version> </dependency> |
상기 pom 해설
<!– MySQL –> Mysql(DB)와 접속하기 위한 dependency
<!– MyBatis 3.4.1 –> MyBatis를 사용하기 위한 dependency
<!– MyBatis-Spring –> MyBatis와 Spring 프레임워크를 연동하기 위한 dependency
<!– Spring-jdbc –> Spring Jdbc에서 사용하기 위한 dependency
<!– Mybatis log –> MyBatis의 로그를 내기 위한 dependency
◆root-context.xml 설정
디폴트 상태
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans> |
설정 후 상태
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> <context:component-scan base-package="com.dreamhanks" /> <context:component-scan base-package="com.dreamhanks.service" /> <!-- MySQL dataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property> <property name="url" value="jdbc:log4jdbc:mysql://localhost:3306/work_db?useSSL=false&serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="Daiki8863"></property> </bean> <!-- mybatis SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property> </bean> <!-- mybatis --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean> </beans> |
①설정 후 상태의 root-context.xml을 복사해 주세요.
②<!– MySQL dataSource –> 설정
・아래의 value 값을 자신의 mysql 환경에 맞춘다.
1 2 3 4 |
<property name="url" value="jdbc:log4jdbc:mysql://localhost:3306/work_db?useSSL=false&serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="Daiki8863"></property> |
・work_db → 커런트 스키마 (Current schema)
・username, password를 자신의 mysql 환경에 맞춘다.
③패키지 이름 등은 자신의 환경에 맞출 것
1 2 3 4 |
<context:component-scan base-package="com.dreamhanks" /> <context:component-scan base-package="com.dreamhanks.service" /> |
◆servlet-context.xml 설정
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 |
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.dreamhanks.workmanager" /> </beans:beans> |
①특별히 설정해야 할 부분은 없습니다만, 아래의 내용이 자신의 환경과 맞는지 확인해 주십시오.
1 |
<context:component-scan base-package="com.dreamhanks.workmanager" /> |
끝으로
다음은 MyBatis Generator의 인스톨 방법에 대해 설명할 것입니다.
【제3회】My Batis Generator 설치 방법
감사합니다.
コメント