Day71 :) Mybatis 세팅

Nux·2021년 12월 30일
0

자바웹개발

목록 보기
85/105
post-thumbnail

Mybatis

  • SQL Mapper 프레임워크
    • SQL Mapper: 쿼리문으로 직접 DB데이터를 다룸
      • 참고: ORM: 객체를 통해 간접적으로 DB 데이터를 다룸 (JPA, Hibernate...)
  • SQL을 XML파일에 분리해서 저장/관리
  • Java소스와 Query 소스가 혼재되어 관리가 어려운 JDBC의 단점을 보완

사용방법

라이브러리 세팅

https://mvnrepository.com/

  • pom.xml에 등록

라이브러리

  • spring-webmvc: 스프링 웹 애플리케이션 개발 지원
  • spring-jdbc: db액세스, 트랜잭션 처리 지원
  • commons-dbcp: db와 연결된 connection pool을 제공
  • mybatis: mybatis core
  • mybatis-spring: 스프링에서 mybatis를 쓸 수 있도록 지원
  • DB관련 라이브러리
    • ojdbc(오라클일 때)
    • mysql(MySQL일 때)

Connection Pool

  • 상기 디렉토리 대로 폴더 생성 후 spring bean configuration file 생성

Oracle일 때

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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">

	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
		<property name="username" value="db계정명"></property>
		<property name="password" value="비밀번호"></property>
	</bean>
</beans>

MySql일 때

<?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
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/jdbc
		http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
		
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://주소/스키마"/>
	<property name="username" value="db계정명"/>
	<property name="password" value="비밀번호"/>
</bean>

</beans>

mybatis-spring 연결

  • spring bean configuration file에 작성
1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	2.<property name="dataSource" ref="dataSource"></property>
	3.<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
	4.<property name="mapperLocations" value="classpath:/mybatis/mappers/*.xml"></property>
 </bean>

1. sqlSessionFactory

  • sqlSession객체를 제공하는 객체
sqlSession
  • insert, update, delete, select문을 실행하는 메서드 제공
  • mapper인터페이스와 mapper xml파일로 mapper인스턴스를 생성하는 메서드 제공

2.property name="dataSource"

  • name의 dataSource: sqlSession빈에서 사용할 이름
  • ref의 dataSource: mybatis-db연결에서 정의한 bean

3. property name="configLocation"

  • mybatis 환경설정파일 경로 지정

4.property name="mapperLocations" value="classpath: ..."

  • SQL문이 정의된 XML파일 경로 지정

기타설정

<mybatis-spring:scan base-package="com.sample.dao" factory-ref="sqlSessionFactory"/>
  • @Mapper 어노테이션이 부착된 인터페이스를 스캔해서
    mapper인스턴스 생성 후 스프링컨테이너에 등록
<context:annotation-config />
  • @Autowired, @Resource, @PostConstructor, @PreDestroy등의 어노테이션을 검색해 이 작업을 수행하는 객체들을 스프링 컨테이너에 등록
<context:component-scan base-package="com.sample.service"/>
  • @Component, @Controller, @RestController, @ControllerAdvice, @Respository, @Service, @Configuration등의 어노테이션이 부착된 클래스를 지정된 패키지와 하위패키지에서 전부 스캔한 후 스프링 컨테이너에 등록
<mvc:annotation-driven />
  • spring webmvc관련 어노테이션을 감지하고 이를 수행한 객체가 스프링 컨테이너에 등록됨
<mvc:view-resolvers>
	<mvc:jsp prefix="/WEB-INF/jsp/" suffix=""/>
</mvc:view-resolvers>
  • prefix경로를 suffix로 사용함
    • /WEB-INF/jsp/list.jsp -> list.jsp

0개의 댓글