ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] 마이바티스(Mybatis) 란? 그리고 사용방법
    program_language 및 궁금한것/Java 2019. 8. 31. 20:54

    Mybatis 사이트

     

    Ibatis의 다음버젼 Mybatis

    - java Persistence framework의 일종이다.

    - 데이터의 저장, 조회, 변경, 삭제를 다루는 클래스 및 설정 파일의 집합

    - 퍼시스턴스 프레임 워크를 사용하면 간단하게 DB와 연동된다.

     

    TransactionManager 타입

    JDBC(JDBC커밋과 롤백을 처리하기 위해 사용) , MANAGED(컨테이너가 관리)

     

    DatasourceSource 타입

    - UNPOOLED : 매번 커넥션을 열고 닫는다.

    - POOLED : 커넥션 풀이 적용

     

    pom.xml 추가

    <dependencies>
    		<!-- Mybatis연동 -->
    		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.5.2</version>
    		</dependency>
    
    		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>8.0.17</version>
    		</dependency>
    
    	</dependencies>

    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>
    	
    	<!-- typeAliases는 무조건 환경설정보다 상위에 위치해야 한다. -->
    	<typeAliases>
    		<typeAlias type="t_tok03.Student" alias="StudentType"/>
    	</typeAliases>
    	
    	<environments default="development">
    		<environment id="development">
    			<transactionManager type="JDBC"/>
    			<dataSource type="POOLED">
    				<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    				<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
    				<property name="username" value="root"/>
    				<property name="password" value="0000"/>
    			</dataSource>
    		</environment>
    	</environments>
    	
    	<mappers>
    		<mapper resource="t_tok03/Student.xml"/>
    	</mappers>
    </configuration> 

    Student.xml (mapper)

    <?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="Student">
    	<select id="findAllStudent" resultType="StudentType">
    		SELECT * FROM student
    	</select>
    </mapper>
      
    

    Main.java

    package t_tok03;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class Main {
    
    	public static void main(String[] args) throws IOException {
    		select(); 
    	}
    
    	public static void select() throws IOException {
    		Reader reader = Resources.getResourceAsReader("t_tok03/mybatis-config.xml");
    		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    		SqlSession session = sqlSessionFactory.openSession();
    
    		List<Student> list = session.selectList("Student.findAllStudent");
    		System.out.println(list.size());
    
    		for (Student s : list) {
    			System.out.println(s);
    		}
    	}
    
    }
    

     

    반응형

    'program_language 및 궁금한것 > Java' 카테고리의 다른 글

    [Java] JRE와 JDK의 차이점  (0) 2021.03.29
    [Java] 자바 static을 지양하자  (0) 2019.09.27
    [JAVA] IO 입출력 객체 사용법 (아주중요)  (0) 2019.07.29
    ibatis란?  (0) 2019.07.01
    [JAVA] POJO란?  (0) 2019.06.18

    댓글

Designed by Tistory.