本人使用maven创建的项目:
首先添加依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
然后创建一个实体类:
package cn.et.lesson02.entity;
public class Food {
private int foodid;
private String foodname;
private int price;
private String imgpath;
public int getFoodid() {
return foodid;
}
public void setFoodid(int foodid) {
this.foodid = foodid;
}
public String getFoodname() {
return foodname;
}
public void setFoodname(String foodname) {
this.foodname = foodname;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getImgpath() {
return imgpath;
}
public void setImgpath(String imgpath) {
this.imgpath = imgpath;
}
}
然后创建Controller层:
package cn.et.lesson02.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.et.lesson02.entity.Food;
import cn.et.lesson02.mapper.FoodMapper;
@RestController
public class SbController {
@Autowired
FoodMapper fm ;
/**
* 使用springmvc的例子
* @return
*/
@RequestMapping("/queryAll")
public List<Food> queryAll(){
return fm.queryFood();
}
}
最后创建 主方法 和 访问主方法:
package cn.et.lesson02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//必须添加SpringBootApplication 启用spring的自动配置功能
@SpringBootApplication
public class Main {
/**
* just run springboot启动的方法
*/
public static void main(String[] args) {
//启动会加载自动配置
SpringApplication.run(Main.class, args);
}
}