springboot 整合 mybatis(无spring开发经验版本)

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>

相关推荐