一、创建Spring Boot项目
选择Spring Web、MySql Driver、MyBatis Framework
改成本地maven
这样一个基础的Spring Boot搭建成功
接下来就是创建分层
application.properties 配置数据库连接信息
#端口号
server.port=80
#连接数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bootplus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username =root
spring.datasource.password =123456
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
接下来添加文件
package com.hsy.springbootmybatis.demo.bean;
public class Pet {
private String id;
private String name;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Pet{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", type='" + type + '\'' +
'}';
}
}
package com.hsy.springbootmybatis.demo.bean;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class Result implements Serializable {
private int code = 0;
private String message;
private Map<String, Object> attr = new HashMap();
private Result() {
}
public static Result create() {
return new Result();
}
public int getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
public void raise(int code, String message) {
this.code = code;
this.message = message;
}
public void put(String key, Object obj) {
this.attr.put(key, obj);
}
public Map<String, Object> getAttr() {
return this.attr;
}
}
package com.hsy.springbootmybatis.demo.Controller;
import com.hsy.springbootmybatis.demo.Service.PetService;
import com.hsy.springbootmybatis.demo.bean.Pet;
import com.hsy.springbootmybatis.demo.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class PetController {
@Autowired
private PetService petService;
@ResponseBody
@RequestMapping(value = "/", method = RequestMethod.GET)
public String helloWorld() {
return "hello world";
}
/**
* 通过id查询信息
*/
@ResponseBody
@GetMapping(value = "/getInfoById")
public Object getID() {
Pet pet=petService.getById("1");
Result result=Result.create();
if (pet == null){
result.raise(-1,"id不存在");
return result;
}
result.put("name",pet.getName());
result.put("type",pet.getType());
result.put("message",pet.toString());
result.raise(1,"成功");
return result;
}
}
package com.hsy.springbootmybatis.demo.Mapper;
import com.hsy.springbootmybatis.demo.bean.Pet;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
@Mapper
public interface PetMapper {
/**通过id查询*/
@Select("SELECT * FROM pet WHERE id = #{id}")
Pet getById(@Param("id") String id);
}
package com.hsy.springbootmybatis.demo.Service.Impl;
import com.hsy.springbootmybatis.demo.Mapper.PetMapper;
import com.hsy.springbootmybatis.demo.Service.PetService;
import com.hsy.springbootmybatis.demo.bean.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PetServiceImpl implements PetService {
@Autowired
private PetMapper petMapper;
@Override
public Pet getById(String id) {
return petMapper.getById(id);
}
}
package com.hsy.springbootmybatis.demo.Service;
import com.hsy.springbootmybatis.demo.bean.Pet;
public interface PetService {
Pet getById(String id);
}
启动类添加扫描
@SpringBootApplication(scanBasePackages = {“com.hsy.springbootmybatis.demo”})
@MapperScan(“com.hsy.springbootmybatis.demo.Mapper”)
最后PostMan进行测试