2015年10月8日 星期四

spring4 + mybatis3 sample (簡易版) - spring controller/service

基本上還是 spring 的 controller 和 service...一般般

Controller/Action

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.log4j.Logger;

//import javax.servlet.http.HttpServletRequest;

import java.util.*;

@Controller
public class SampleController {

    Logger logg = Logger.getLogger(this.getClass());
    String logId = "log";
    String logName = "log";

    @Autowired
    SampleService theService;

    @RequestMapping(value={"/test/list"}, method = RequestMethod.GET)
    public @ResponseBody List fn1(Model model){
        System.out.println("??enter controller");
        return theService.test1();
    }
   
    @RequestMapping(value={"/test/list2"}, method = RequestMethod.GET)
    public @ResponseBody List fn2(Model model){
        System.out.println("??enter list2");
        return theService.test2();
    }
}


Service

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

import java.util.*;

@Service
public class SampleService {

    @Autowired
    SampleDao dao = null;
   
    @Autowired
    SampleConfigDao dao2 = null;

    @Transactional
    public List test1()  {
        System.out.println("??enter service.test1");
        return dao.selectSample();
    }
   
    @Transactional
    public List test2()  {
        System.out.println("??enter test2");
        return dao2.selectSampleConfig("ABC");
    }
}

沒有留言: