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.啓動測試: 項目路徑在yml文件中的context-path配置

 

最後發現這種配置,也可以支持mapper接口中使用註解sql的方式(非xml方式),簡單的crud都可以蠻方便:

例:

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