본문 바로가기
스프링

스프링부트(Spring Boot) MyBatis 사용하기

by 그리득 2023. 12. 21.
728x90
pom.xml 라이브러리 등록

 

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>com.oracle.database.jdbc</groupId>
	<artifactId>ojdbc11</artifactId>
	<scope>runtime</scope>
</dependency>

 

 

application은 properties, yml중 하나 선택

application.properties에 라이브러리 등록
#DataBase(Oracle) 설정하기
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=spring
spring.datasource.password=spring

#mybatis설정하기
mybatis.config-location=classpath:/config/mybatis-config.xml
mybatis.mapper-locations=classpath:/mappers/**/*.xml

 

application.yml에 라이브러리 등록
spring: 
  datasource: 
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:xe
    username: spring
    password: spring
    
mybatis: 
  mapper-locations: classpath:mappers/**/*.xml
  config-location: classpath:config/mybatis-config.xml
config.xml, mapper.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>
		<typeAlias type="com.rocket.ksj.member.model.dto.Member" alias="member"/>
	</typeAliases>
</configuration>

 

<?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 namespace="member">
	<select id="selectMemberAll" resultType="member"> 
		SELECT * FROM MEMBER
	</select>
</mapper>

 

어노테이션 방식도 있지만 xml방식을 채택

 

 

'스프링' 카테고리의 다른 글

sql.Date 와 util.Date의 차이  (0) 2024.01.06
스프링부트 Security 적용하기  (0) 2023.12.21
WebSocket 채팅 구현  (0) 2023.12.18
ajax 응답/처리  (0) 2023.12.14
인터셉터 이용하기  (0) 2023.12.11