使用IDEA搭建Spring Boot项目整合Mybatis

一、创建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进行测试
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值