Spring Boot에 데이터베이스 설정
pom.xml파일에 관련된 의존성 추가 (없으면 추가하기)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
pom.xml
DataSource 설정
(DataSource 설정방법 : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html)
(Properties 옵션정보 : https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=test spring.datasource.password=test
src/main/resources/application.properties
Spring Boot에 MyBatis 설정
pom.xml파일에 mybatis관련된 의존성 추가
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency>
pom.xml
SqlSessionFactory 설정
@SpringBootApplication public class DemoApplication {
...
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean.getObject(); } ...
}
src/main/java/com.example/DemoApplication.java
Mapper 작성 및 설정
데이터베이스 테이블 생성 (mysql)
CREATE TABLE `test`.`test_table` ( test_column varchar(100) );
DTO(Data Transfer Object) 클래스 생성
package com.example.domain; public class DemoDTO { private String testColumn; public String getTestColumn() { return testColumn; } public void setTestColumn(String testColumn) { this.testColumn = testColumn; } }
src/main/java/com.example.domain/DemoDTO.java
====================================================
1. 어노테이션 방법
--------------------------------------------------------------------------------
Mapper 인터페이스 작성
package com.example.mapper; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Delete; import com.example.domain.DemoDTO; public interface DemoMapper { @Insert("insert into test_table (test_column) values (#{testColumn})") public void insert(DemoDTO demoDTO) throws Exception; @Select("select * from test_table where test_column = #{testColumn}") @Results(value = { @Result(property="testColumn", column="test_column") }) public DemoDTO select(String testColumn) throws Exception; @Update("update test_table set test_column = #{testColumn}") public void update(DemoDTO demoDTO) throws Exception; @Delete("delete from test_table where test_column = #{testColumn}") public void delete(String testColumn) throws Exception; }
src/main/java/com.example.mapper/DemoMapper.java
Mapper 인터페이스 @MapperScan을 통해 연결
... @SpringBootApplication @MapperScan(value={"com.example.mapper"}) public class DemoApplication { ... }
src/main/java/com.example/DemoApplication.java
====================================================
2. XML 방법
--------------------------------------------------------------------------------
2. XML 방법
--------------------------------------------------------------------------------
Mapper 인터페이스 작성
package com.example.mapper; import org.apache.ibatis.annotations.Param; import com.example.domain.DemoDTO; public interface DemoMapper { public void set(@Param("testColumn")String testColumn) throws Exception; public DemoDTO get(@Param("testColumn")String testColumn) throws Exception; }
src/main/java/com.example.mapper/DemoMapper.java
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 namespace="com.example.mapper.DemoMapper"> <resultMap type="com.example.domain.DemoDTO" id="demoDTO"> <result column="test_column" property="testColumn"/> </resultMap> <insert id="set" parameterType="String"> insert into test_table (test_column) values (#{testColumn}) </insert> <select id="get" parameterType="String" resultMap="demoDTO"> select * from test_table where test_column = #{testColumn} </select> </mapper>
src/main/resrouces/mapper/DemoMapper.xml
Mapper 인터페이스 및 XML 연결
... @SpringBootApplication @MapperScan(value={"com.example.mapper"}) public class DemoApplication { ... @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); Resource[] arrResource = new PathMatchingResourcePatternResolver() .getResources("classpath:mappers/*Mapper.xml"); sqlSessionFactoryBean.setMapperLocations(arrResource); return sqlSessionFactoryBean.getObject(); } }
src/main/java/com.example/DemoApplication.java
Spring Boot에 데이터베이스 및 MyBatis 설정 테스트 코드
package com.example; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.web.WebAppConfiguration; import com.example.domain.DemoDTO; import com.example.mapper.DemoMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) @WebAppConfiguration public class DemoApplicationTests { @Autowired private DataSource dataSource; @Autowired private SqlSessionFactory sqlSessionFactory; @Autowired private DemoMapper demoMapper; @Test public void contextLoads() { } @Test public void textConnection() throws SQLException { System.out.println(dataSource); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test public void testSqlSesstion() throws Exception { System.out.println(sqlSessionFactory); } @Test public void testInsert() throws Exception { DemoDTO demoDTO = new DemoDTO(); demoDTO.setTestColumn("TEST_DATA_annotation"); demoMapper.insert(demoDTO); } @Test public void testSelect() throws Exception { DemoDTO demoDTO = demoMapper.select("TEST_DATA_annotation"); System.out.println("testSelect: " + demoDTO.getTestColumn()); } @Test public void testSet() throws Exception { demoMapper.set("TEST_DATA_xml"); } @Test public void testGet() throws Exception { DemoDTO demoDTO = demoMapper.get("TEST_DATA_xml"); System.out.println("testGet: " + demoDTO.getTestColumn()); } }
src/test/java/com.example/DemoApplicationTests.java
Part4에선 DB에서 가져온 데이터를 service와 controller를 통해 view까지 전달 방법을 알아보려 함.
댓글 없음:
댓글 쓰기
참고: 블로그의 회원만 댓글을 작성할 수 있습니다.