1. 程式人生 > 其它 >springboot+mybatis+mysql進階版

springboot+mybatis+mysql進階版

前面都是一樣的,只是在後面的MyService中用的是interface,再呼叫interface的繼承類。

MyService.java

package com.example.service;

import com.example.model.Userinfo;

public interface MyService {

    public Userinfo getUserinfo(Integer id);
}

MyServiceimpl.java

package com.example.service;

import com.example.dao.UserinfoMapper;
import com.example.model.Userinfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyServiceImpl implements MyService { @Autowired UserinfoMapper userinfoMapper; @Override public Userinfo getUserinfo(Integer id) {
return userinfoMapper.selectByPrimaryKey(id); } }

MyController.java

package com.example.control;

import com.example.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @Autowired MyService myService; @RequestMapping("/hehe/id/{id}") public @ResponseBody Object hehe(@PathVariable("id")Integer id){ return myService.getUserinfo(id); } }

這裡面用的是Restful的方式來代入變數。

然後就是程序序入口了。

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.dao")
@SpringBootApplication
public class Spring007Mybatis03Application {

    public static void main(String[] args) {
        SpringApplication.run(Spring007Mybatis03Application.class, args);
    }

}

此文章是https://www.cnblogs.com/gangliao81/p/16253248.html的補充。