创建service接口
package com.frame.service;
public interface DemoService {
void test();
}
创建service业务层实现类
package com.frame.service.impl;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.frame.service.DemoService;
@Service(
"demoService")
public
class DemoServiceImpl implements DemoService {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
// mybatis sql模板的命名空间
private
static
final String NAMESPACE =
"com.frame.mapper.DemoMapper";
@Override
public void test() {
System.out.println(
"返回查询结果集 -> " + sqlSessionTemplate.selectList(NAMESPACE +
".getTest"));
// 查询结果集
}
}
创建controller控制器
package com.frame.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.frame.service.DemoService;
@Controller
public
class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(
"/hello")
public String hello() {
System.out.println(
"执行hello控制器方法");
// 调用业务层执行查询操作
demoService.test();
return
"hello";
}
}