前面都是一样的,只是在后面的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的补充。
浙公网安备 33010602011771号