사용 환경
- Windows 10
- Java 1.8
- Maven 3.8.2
- Spring Boot 2.5.4
- Oracle DB 19
- MyBatis 2.2.0
Hello 프로젝트 따라하기
-
프로젝트 생성
브라우져에서 spring initializr를 방문하여 아래와 같이 입력하고 GENERATE 버튼을 클릭합니다.
- Project: Maven Project
- Language: Java
- Spring Boot: 2.5.4
- Project Meta:
- Group: trvoid.mybatis
- Artifact: basic-mybatis-example
- Name: basic-mybatis-example
- Package name: trvoid.mybatis
- Packaging: Jar
- Java: 8
- Dependencies:
- JDBC API
- MyBatis Framework
- Oracle Driver
생성된 프로젝트 파일을 다운로드하여 압축을 풉니다. 프로젝트 폴더 구조는 아래와 같습니다.
hello |-src |-main |-java |-trvoid.mybatis |-BasicMybatisExampleApplication.java |-resources |-application.properties |-test |-java |-trvoid.mybatis |-BasicMybatisExampleApplicationTests.java |-pom.xml
pom.xml 파일에서 의존성 항목과 빌드 플러그인을 확인할 수 있습니다.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
BasicMybtisExampleApplication.java 파일의 내용은 아래와 같습니다.
package trvoid.mybatis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BasicMybatisExampleApplication { public static void main(String[] args) { SpringApplication.run(BasicMybatisExampleApplication.class, args); } }
-
@Mapper
추가src/main/java/trvoid/mybatis/Car.java
package trvoid.mybatis; public class Car { private int id; private String model; private String manufacturer; public Car(int id, String model, String manufacturer) { this.id = id; this.model = model; this.manufacturer = manufacturer; } // getters and setters @Override public String toString() { return String.format("id:%d, model:%s, manufacturer:%s", id, model, manufacturer); } }
src/main/java/trvoid/mybatis/CarMapper.java
package trvoid.mybatis; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface CarMapper { String SELECT = " SELECT ID, MODEL, MANUFACTURER FROM CAR "; @Select(" SELECT COUNT(1) FROM ALL_OBJECTS " + " WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME = 'CAR' ") int countTable(); @Insert(" CREATE TABLE CAR ( " + " ID NUMBER(10) NOT NULL, " + " MODEL VARCHAR2(100) NOT NULL, " + " MANUFACTURER VARCHAR2(100) NULL, " + " CONSTRAINT CAR_PK PRIMARY KEY (ID) " + " ) ") void createTable(); @Insert(" INSERT INTO CAR ( " + " ID, MODEL, MANUFACTURER " + " ) VALUES ( " + " #{car.id}, #{car.model}, #{car.manufacturer,jdbcType=VARCHAR} " + " ) ") void insertCar(@Param("car") Car car); @Select(SELECT + " WHERE ID = #{carId} ") Car getCar(@Param("carId") String carId); @Select(SELECT) List<Car> findAll(); }
-
데이터베이스 질의 수행을 위해
CommandLineRunner
추가CAR
테이블이 없을 경우 생성하고 세 개의 행을 추가합니다.src/main/java/trvoid/mybatis/BasicMybatisExampleApplication.java
package trvoid.mybatis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.sql.DataSource; import java.util.List; @SpringBootApplication public class BasicMybatisExampleApplication implements CommandLineRunner { @Autowired DataSource dataSource; @Autowired CarMapper carMapper; public static void main(String[] args) { SpringApplication.run(BasicMybatisExampleApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("DataSource = " + dataSource); if (carMapper.countTable() == 0) { carMapper.createTable(); System.out.println("** Created a table: CAR"); int carId = ((int)Math.random()) % 100; Car car1 = new Car(carId + 0, "J1", "Jelly"); carMapper.insertCar((car1)); System.out.println(String.format("** Inserted a car: %s", car1.toString())); Car car2 = new Car(carId + 1, "T1", "Teal"); carMapper.insertCar((car2)); System.out.println(String.format("** Inserted a car: %s", car2.toString())); Car car3 = new Car(carId + 2, "W1", "Wisdom"); carMapper.insertCar((car3)); System.out.println(String.format("** Inserted a car: %s", car3.toString())); } System.out.println("== CARS =="); List<Car> list = carMapper.findAll(); list.forEach(x -> System.out.println(x)); System.out.println("-- CARS --"); System.exit(0); } }
-
데이터 소스 설정
src/main/resources/application.properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=your_db_username spring.datasource.password=your_db_password spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
-
컴파일
>mvn compile
-
실행
>mvn spring-boot:run
출력 결과
... DataSource = HikariDataSource (HikariPool-1) == CARS == id:1, model:J1, manufacturer:JELLY id:2, model:T1, manufacturer:TEAL id:3, model:W1, manufacturer:WISDOM -- CARS -- ...
Written with StackEdit.
댓글 없음:
댓글 쓰기