controller内容如下
UserController
package com.fengzi.controller; import com.fengzi.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; @Controller @ResponseBody public class UserController { @Autowired private UserService userService; @RequestMapping(value = {"/find","/find/{id}"},method = RequestMethod.GET) public List<Map<String,Object>> findById(@PathVariable(value = "id", required = false) String id){ System.out.println(id); if (id != null) { return userService.findById(id); } return userService.findAll(); } }
dao层内容如下
UserDao
package com.fengzi.dao; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Mapper @Repository public interface UserDao { @Select("select id,name from user where id=#{id}") List<Map<String,Object>> findById(@Param(value = "id") String id); @Select("select * from user") List<Map<String,Object>> findAll(); }
service内容如下
UserService
package com.fengzi.service; import java.util.List; import java.util.Map; public interface UserService { List<Map<String,Object>> findById(String id); List<Map<String,Object>> findAll(); }
service实现类内容如下
UserServiceImp
package com.fengzi.service.ServiceImp; import com.fengzi.dao.UserDao; import com.fengzi.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class UserServiceImp implements UserService { @Autowired private UserDao userDao; public List<Map<String,Object>> findById(String id) { return userDao.findById(id); } public List<Map<String,Object>> findAll() { return userDao.findAll(); } }
application.yml内容如下
spring: datasource: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://134.175.85.103:3306/testdb server: port: 7001 logging: level: com.fengzi.dao: debug //可以显示sql语句
最后文件结构如下