1. 程式人生 > >springboot整合mybatis實現增刪改查流程以及易錯點

springboot整合mybatis實現增刪改查流程以及易錯點

Springboot+mybatis+mysql實現增刪改查操作。

在昨天學習的springboot的入門程式上進行擴充套件開發。首先建立好專案目錄結構,如圖:

整個流程類似ssm整合,controller—》service—》dao ,介面訪問xml。這個寫完的結構如下:

   

裡面具體原始碼跟ssm寫的差不多,我大概貼出一些

這是入口,@MapperScan註解是掃描mapper介面的意思。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;


@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

mapper介面,@Mapper註解是標識,能夠自動載入被掃描。

package com.example.demo.dao;

import com.example.demo.pojo.Person;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface PersonMapper {
    void insert(Person person);
    Person select(Integer id);
    int update(Person person);
    int delete(Integer id);
    List<Person> findAll();
}

Controller類,@RestController註解,所有返回都為json

package com.example.demo.controller;

import com.example.demo.dao.PersonMapper;
import com.example.demo.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Controller01 {

    @Autowired(required = false)
    private PersonMapper mapper;

    @RequestMapping("/insert")
    public String insert(){
        Person p=new Person();
        p.setName("www");
        p.setTel("12315");
        mapper.insert(p);
        return "success";
    }

    @RequestMapping("/findAll")
    public List<Person> findAll(){
        List<Person> personList=mapper.findAll();
        return personList;
    }

    @RequestMapping("/select")
    public Person select(Integer id){
        Person p=mapper.select(id);
        return p;
    }

    @RequestMapping("delete")
    public int delete(Integer id){
        int i=mapper.delete(id);
        return i;
    }

    @RequestMapping("/update")
    public String  update(Person p){
        mapper.update(p);
        return "SUCCESS";
    }

}

至於xml檔案自己編寫即可。大概流程和註解就是這樣,springboot工程就是不用手動配置那麼多資訊,通過註解就行。流程簡單,也會出錯。

springboot有如下幾點還是非常值得注意的。

把基本模組全部寫完之後,會考慮spring依賴注入的問題。在spring中有一個容器,那個用的是一個application.xml來配置資料來源,事務,資料工廠一系列的配置。Springboot基本上完全自動把服務做好了,這裡需要配置的就是資料來源,檔案位置application.property。整合mybatis還需要把xml的位置配置到裡面,不然當你請求時它會報錯,報錯就是說找不到這個配置檔案,Invalid bound statement (not found):這個原因就是你的xml沒載入進去,或者載入進去了,但位置是不對的,也會出現這種錯誤。http://static.oschina.net/uploads/img/201608/02093607_K6lF.jpg

解決方法:pom.xml加入如下配置,不加這些配置導致的後果,你在編譯完之後會發現target資料夾下面沒有你的xxx.xml檔案,原因是因為我們的mapper配置檔案是放在java下的,如果你放在resouce檔案下是沒問題的,能載入。放在java下需要配置,通知它編譯到classes裡面去,因為程式執行走的是編譯後的位元組檔案。編譯完後,xml也編譯了。第二個就是需要看下你編譯後文件的位置是否與你配置的位置一致,如果一致,就可以正常訪問。

以上是mybatis整合需要注意的地方。

其次就是關於註解的學習,大部分跟springmvc的rest風格相似,有些註解需要以後實際開發中學習。

@MapperScan("com.example.demo.dao")

掃描這個包下mapper介面,同時也可以掃描多個介面。

@RestController

控制器標識,所有請求響應格式為json