springboot 整合 mybatis(無spring開發經驗版本)

springboot 整合 mybatis(無spring開發經驗版本)

目錄結構

目錄結構

目錄解釋

controller 定義路由

service 業務邏輯處理

entity 實體類 與數據庫中的表一一對應

mapper 數據庫操作,定義對數據庫各種CUDR的接口,myBatis框架會自動生成實體類

mapping 數據庫操作的XML文件,通過namespace 與 mapper一一對應,通過每一項中的id對應接口類中定義的方法,通過每一項中的resultType對應實體類,表明數據返回的對象。

相關代碼

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 19961110
    url: jdbc:mysql://47.95.110.227:3308/news?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  configuration:
    # 開始駝峯命名與下劃線命名的轉換
    map-underscore-to-camel-case: true

#showSql
logging:
  level:
    com:
      example:
        mapper : debug

indexController.Java

package com.crxk.myBatisTset.controller;

import com.crxk.myBatisTset.service.catService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class indexController {

    @Autowired
    private catService catService;

    @RequestMapping("/getCat")
    public String getCat(int id){
        return catService.getCat(id).toString();
    }
}

catService.java

package com.crxk.myBatisTset.service;

import com.crxk.myBatisTset.entity.Cat;
import com.crxk.myBatisTset.mapper.catMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class catService {
    @Autowired
    catMapper catMapper;
    public Cat getCat(int id){
        return catMapper.getCat(id);
    }
}

cat.java

package com.crxk.myBatisTset.entity;

public class Cat {
    int catId;
    String catName;

    public Cat() {
    }

    public Cat(int catId, String catName) {
        this.catId = catId;
        this.catName = catName;
    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "catId=" + catId +
                ", catName='" + catName + '\'' +
                '}';
    }
}

catMapper.java

package com.crxk.myBatisTset.mapper;


import com.crxk.myBatisTset.entity.Cat;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface catMapper {
    Cat getCat(int id);
}

catMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.crxk.myBatisTset.mapper.catMapper">

    <select id="getCat" resultType="com.crxk.myBatisTset.entity.Cat">
        select * from cat where cat_id = #{id}
    </select>

</mapper>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章