org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test
修改启动的端口号,配置数据库连接,注意冒号后面一定要有空格
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @RequestMapping(value = "/test",method = RequestMethod.POST) public String test(String name){ return name; } }
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1
package com.example.demo.entity; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package com.example.demo.mapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { ListgetUserList();//这里的方法名和xml定义的要一致 }
package com.example.demo.service; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper;//注入UserMapper,这里的警告不用管 public ListgetUserList(){ return userMapper.getUserList(); } }
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class DemoController { @Autowired private UserService userService; @RequestMapping(value = "/test",method = RequestMethod.POST) public String test(String name){ return name; } @RequestMapping(value = "/getUserList",method = RequestMethod.POST) public ListgetUserList(){ return userService.getUserList(); } }
com.github.pagehelper pagehelper-spring-boot-starter 1.2.10
pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql page-size-zero: true
@RequestMapping(value = "/getUserListPage",method = RequestMethod.POST) public PageInfogetUserListPage(Integer pageNum,Integer pageSize){ PageHelper.startPage(pageNum,pageSize); List users=userService.getUserList(); PageInfo pageInfo=new PageInfo<>(users); return pageInfo; }
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class DemoController { protected Logger log = Logger.getLogger(this.getClass()); @Autowired private UserService userService; @RequestMapping(value = "/test",method = RequestMethod.POST) public String test(String name){ log.info("进入了test方法。。。"); return name; }