1. 程式人生 > >Spring boot + Mybatis(基於xml配置方式) WEB專案

Spring boot + Mybatis(基於xml配置方式) WEB專案

一.工具(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.啟動測試