1.目录结构
2.配置文件
application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_boot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: "password"
tomcat:
initialSize: 1
min-idle: 1
max-idle: 20
max-wait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
logging:
file: logs/demo.log
application-dev.yml
server:
port: 8081
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.ctgu
springstart
0.0.1-SNAPSHOT
springstart
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
5.1.47
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
java类
SpringStartApplication.java
package com.ctgu.springstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class SpringStartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringStartApplication.class, args);
}
}
User.java
package com.ctgu.springstart.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import java.util.Date;
@Data
public class User {
private Integer id;
private String username;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer age;
@JsonProperty("account")
private String phone;
@JsonIgnore
private String email;
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8")
private Date createTime;
}
UserDao.java
package com.ctgu.springstart.dao;
import com.ctgu.springstart.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserDao {
/**
* 根据用户名称,查询用户信息
*
* @param username 用户名
*/
@Select("SELECT * FROM USER where username = #{username}")
// @Select("SELECT * FROM USER")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "age", column = "age"),
@Result(property = "phone", column = "phone"),
@Result(property = "email", column = "email"),
})
User findByName(String username);
// User findByName(@Param("username") String username);
}
UserService.java
package com.ctgu.springstart.service;
import com.ctgu.springstart.pojo.User;
public interface UserService {
User findUserByName(String username);
}
UserServiceImpl.java
package com.ctgu.springstart.service.impl;
import com.ctgu.springstart.dao.UserDao;
import com.ctgu.springstart.pojo.User;
import com.ctgu.springstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public User findUserByName(String username) {
return userDao.findByName(username);
}
}
UserController.java
package com.ctgu.springstart.controller;
import com.ctgu.springstart.pojo.User;
import com.ctgu.springstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/getUserInfo")
public User getUser(@RequestParam(value = "username", required = true) String username) {
return userService.findUserByName(username);
}
}
GetController.java
package com.ctgu.springstart.controller;
import com.ctgu.springstart.pojo.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
public class GetController {
private Map params = new HashMap<>();
// @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)
// public Object findUser(@PathVariable("city_id") String cityId,
// @PathVariable("user_id") String userId){
// params.clear();
// params.put("cityId", cityId);
// params.put("userId", userId);
// return params;
// }
@GetMapping(path = "/{city_id}/{user_id}")
public Object findUser(@PathVariable("city_id") String cityId,
@PathVariable("user_id") String userId){
params.clear();
params.put("cityId", cityId);
params.put("userId", userId);
return params;
}
@GetMapping(value="/v1/page_user1")
public Object pageUser(int from, int size){
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
@GetMapping(value="/v1/page_user2")
public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page", required = true) int from, int size){
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
@RequestMapping("/v1/save_user")
public Object saveUser(@RequestBody User user){
params.clear();
params.put("user", user);
return params;
}
@GetMapping("/v1/get_header")
public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
params.clear();
params.put("access_token", accessToken);
params.put("id", id);
return params;
}
@GetMapping("/v1/test_request")
public Object testRequest(HttpServletRequest request){
params.clear();
params.put("id", request.getParameter("id"));
return params;
}
}
OtherHttpController.java
package com.ctgu.springstart.controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class OtherHttpController {
private Map params = new HashMap<>();
@PostMapping("/v1/login")
public Object login(String id, String pwd){
params.clear();
params.put("id", id);
params.put("pwd", pwd);
return params;
}
@PutMapping("/v1/put")
public Object put(String id){
params.clear();
params.put("id", id);
return params;
}
@DeleteMapping("/v1/del")
public Object del(String id){
params.clear();
params.put("id", id);
return params;
}
}