2020년 5월 21일 목요일

[Spring] Interworking Spring with Mybatis

pom.xml

repository
<!--  오라클 드라이버 -->
<repositories>
 <repository>
  <id>codelds</id>
  <url>https://code.lds.org/nexus/content/groups/main-repo</url>
 </repository>
</repositories>
  A code that sets the repositivity to receive the Oracle driver library file, pasted it under the properties tag at the top.

data source, mybatis library
<!-- 데이터소스 관련 라이브러리 -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.13</version>
</dependency>
<!-- 마이바티스 라이브러리-->
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.4.6</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
</dependency>
Download the data source library and the Mybatis library required to connect to the database.

Oracle Driver Library
<dependency>
 <groupId>com.oracle</groupId>
 <artifactId>ojdbc6</artifactId>
 <version>11.2.0.3</version>
</dependency>
 Download the Oracle driver to connect to the Oracle database.


root-context.xml

 Paste the following code from the root-context file.

root-context.xml
<!-- 아파치 DBCP 설정 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
       <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe" />
       <property name="username" value="Id"/>
       <property name="password" value="Password"/>
</bean>
 
 A code that works with Oracle using a data source class. What you should pay attention to here are url, username, and password properties. xe in url property value means SID, which means that the database is not connected. The property value for username, password, then enter the ID and password of the account that accessed the database.

 <!-- SqlSessionFactory 객체 주입 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
  <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property>
 </bean>
 configLocation means the path to the Mybatis setup file and the path to the mapperLocations maper file. And '*Mapper.xml' means treating a file whose name ends with 'Mapper' as a maper file. In addition, 'classpath:' represents a path from the root directory to resources, and if you don't recognize it, you must click the project properties - java Build Path - source tab -browser to set the path.

<!-- Mapper 어노테이션을 탐색할 경로 지정-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.example.spring01.**.service.impl" />
</bean>
 configLocation means the path to the Mybatis setup file and the path to the mapperLocations maper file. And '*Mapper.xml' means treating a file whose name ends with 'Mapper' as a maper file. In addition, 'classpath:' represents a path from the root directory to resources, and if you don't recognize it, you must click the project properties - java Build Path - source tab -browser to set the path.


Create a Mybatis Settings File

 Create a Mybatis-Config file that allows you to set up multiple things related to the Mybatis. Make it in resources.

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>
 <settings> 
 <!-- 오라클 필드 속성이 READ_COUNT 처럼 언더 스코어가 있을 때 VOreadCount 처럼 카멜 케이스로 변환 되게 합니다. --> 
  <setting name="mapUnderscoreToCamelCase" value="true"/> 
 </settings>
 <typeAliases>
 </typeAliases>
</configuration>
 Create the Mybatis-Config file at the path location set in root-context as shown above.


Create Mybatis Mapper File

 Creates a map file that allows you to return the results of a query statement Create a mapers folder in webapp/resources and create HomeMapper.xml.

HomeMapper.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와 중복되지 않도록 네임스페이스 기재 -->
<mapper namespace="com.example.portpolio.main.service.impl.MainMapper">
 <select id="selectLoginList" resultType="map">
  SELECT USER_ID, USER_PASSWORD
      FROM FIRST
 </select>
</mapper>
 First, enter the path of the maper interface with @Mapper attached with the namespace property value in the maper tag. Then enter the method name to return the query results to the id of the select tag. If you entered correctly, paste the query statement you want to execute into the select tag. The results of this query query return to the resultType's map (HashMap). If you attach a semicolon to a query statement as one thing to note, you will get an error.

댓글 없음:

댓글 쓰기