Spring Boot + Mybatis(支持xml配置方式和注解两种方式) WEB项目

版权声明:本文为博主搜索整合文章。 https://blog.csdn.net/weixin_37794901/article/details/83445718

一.工具(idea)

二.创建项目

最后finish;

3.配置与编写demo

    目录:

3.1 springBoot配置文件:application.yml;也可使用properties文件

spring:
  #数据源配置
  datasource:
    url: jdbc:mysql://xxxxxxxxxxxxxx
    username: xxxx
    password: xxxxxx
    driver-class-name: com.mysql.jdbc.Driver

#访问路径配置
server:
  port: 8080
  servlet:
    context-path: /boot


mybatis:
  typeAliasesPackage: com.example.demo.entity
  mapperLocations: classpath:mapper/*.xml

3.2 controller

package com.example.springboot.controller;

import com.example.springboot.dao.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class DemoController {

   @Autowired DemoMapper demoMapper;

   @RequestMapping(value = "/hello2")
   public Map testSpringBoot(){
      Map<String, Object> result = new HashMap<String, Object>();
      result.put("kitchen",demoMapper.select());
      return result;
   }
}

3.3 mapper

package com.example.springboot.dao;

import com.example.springboot.entity.Kitchen;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface DemoMapper {

   List<Kitchen> select();
}

3.4 Mybatis的 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.example.springboot.dao.DemoMapper">

    <select id="select" resultType="com.example.springboot.entity.Kitchen" >
        SELECT * FROM hotkidstore_production.`store_kitchen`
    </select>
</mapper>

4.启动测试: 项目路径在yml文件中的context-path配置

最后发现这种配置,也可以支持mapper接口中使用注解sql的方式(非xml方式),简单的crud都可以蛮方便:

例:

猜你喜欢

转载自blog.csdn.net/weixin_37794901/article/details/83445718