2015년 12월 8일 화요일

Spring Boot Part4 (Controller, Service, Mapper 연결 및 Transaction 설정)

Service 클래스 생성 및 Mapper 인터페이스 연결
package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.domain.DemoDTO;
import com.example.mapper.DemoMapper;

@Service
public class DemoService {
   
   @Autowired
   private DemoMapper demoMapper;
   
   public DemoDTO getDemoDTO(String testColumn) throws Exception {
      return demoMapper.select(testColumn);
   }
}
src/main/java/com.example.service/DemoService.java

Controller에 Service클래스 연결
package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.domain.DemoDTO;
import com.example.service.DemoService;

@Controller
public class DemoController {

   @Autowired
   DemoService demoService;

   @RequestMapping("/")
   String demo(Model model) throws Exception{
      DemoDTO demoDTO = demoService.getDemoDTO("TEST_DATA_annotation");
      model.addAttribute("testColumn", demoDTO.getTestColumn());
      return "demo";
   }
}
src/main/java/com.example.controller/DemoController.java

View에 데이터 출력
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="ko">
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="../static/demo.css" th:href="@{demo.css}"/>

<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
   th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
   rel="stylesheet" media="screen" />

</head>
<body>
<h1>Hello</h1>
<div class="demo"><p th:text="${testColumn}"/></div>
</body>
</html>
src/main/resources/templates/demo.html

Transaction 설정
JDBC DataSourceTransactionManager를 설정 (mybatis는JDBC를 이용하기 때문)
package com.example;

import javax.sql.DataSource;
...
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan({"com.example.mapper"})
public class DemoApplication {
   ...
   @Bean
   public DataSourceTransactionManager transactionManager(DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
   }
   ...
}
src/main/java/com.example/DemoApplication.java

어노테이션(@Transactional)기반 Transaction 설정(Service 클래스에 적용)
package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.DemoDTO;
import com.example.mapper.DemoMapper;

@Service
public class DemoService {
   
   @Autowired
    private DemoMapper demoMapper;
   
   @Transactional(propagation = Propagation.REQUIRED, rollbackFor={Exception.class})
   public DemoDTO getDemoDTO(String testColumn) throws Exception {
      DemoDTO demoDTO = new DemoDTO();
      demoDTO.setTestColumn("TEST_DATA_TEST");
      demoMapper.insert(demoDTO);
      if (demoDTO.getTestColumn() == "TEST_DATA_TEST")
         throw new Exception();
      
      DemoDTO demoDTO2 = demoMapper.select(testColumn);
      return demoDTO2;
   }
}
src/main/java/com.example.service/DemoService.java


댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.