Spring boot整合mybatis實(shí)現(xiàn)過程圖解
導(dǎo)入mybatis jar包
右鍵pom.xml
模擬springboot底層實(shí)現(xiàn)類
1.
定義接口
@Mapper public interface GoodsDao { /** * 基于商品id刪除商品 * @param id 商品id * @return 刪除行數(shù) * 數(shù)據(jù)層方法對(duì)象的sql映射 */ @Delete("delete from tb_goods where id=#{id}") //當(dāng)傳入的參數(shù)只有一個(gè)且不是數(shù)組時(shí) //#{id}這個(gè)地方的變量可以不是傳入的參數(shù)名(自己隨意) int deleteById(Integer id); }
測試
@SpringBootTest public class TestGoods { @Autowired private GoodsDao gd; @Test void TestGoods() { int i =gd.deleteById(10); System.out.println(i); } }
2.
自己實(shí)現(xiàn)
接口方法
@Mapper public interface GoodsDao { /** * 基于商品id刪除商品 * @param id 商品id * @return 刪除行數(shù) * 數(shù)據(jù)層方法對(duì)象的sql映射 */ @Delete("delete from tb_goods where id=#{id}") int deleteById(Integer id); }
@Component public class GoodsDaoImpl { @Autowired private SqlSession sqlSession; public int deleteById(Integer id) { return sqlSession.delete("com.cy.demo.goods.dao.GoodsDao.deleteById", id); //sqlSession.delete("com.cy.demo.goods.dao.deleteById",id) } }
@SpringBootTest public class GoodsDaoImpTest { @Autowired private GoodsDaoImpl gdi; @Test void testdelete() { int i = gdi.deleteById(9); System.out.println(i); } }
直接導(dǎo)mapper文件找對(duì)應(yīng)的元素
3.
當(dāng)sql語句比較復(fù)雜時(shí)使用映射文件
接口:
/** *GoodsDao.java * ids可以接受多個(gè)參數(shù) * 在mapper文件中直接使用array來接受, * @param ids * @return */ int deleteObject(/*@Param("ids")*/Integer...ids); //當(dāng)mybatis過低時(shí)需要加上@Param("ids")才能識(shí)別
不加@Param("ids")報(bào)錯(cuò)
使用xml映射
獲取xml頭文件(去官網(wǎng))
<?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.cy.demo.goods.dao.GoodsDao"> <delete id="deleteObject"> delete from tb_goods <where> <if test="ids!=null and ids.length>0"> id in <foreach collection="ids" open="(" close=")" separator="," item="i"> #{i} </foreach> </if> or 1=2 </where> </delete> </mapper>
配置:
測試:
@Autowired private GoodsDao gd; @Test void deleteObject() { int rows=gd.deleteObject(1,2,3); System.out.println(row); }
當(dāng)我們?cè)趫?zhí)行此方法時(shí),其實(shí)現(xiàn)類內(nèi)部會(huì)檢測接口方法上是否有定義sql映射
假如沒有,然后基于接口類全名找到對(duì)應(yīng)的映射文件(mapper映射文件的id),然后在基于方法名
再找到對(duì)應(yīng)映射文件的元素,進(jìn)而獲取sql映射
錯(cuò)誤解決:
binding異常還有可能時(shí)參數(shù)異常,還有可能是配置文件有問題
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java string類型轉(zhuǎn)換boolean類型的方法
下面小編就為大家?guī)硪黄猨ava string類型轉(zhuǎn)換boolean類型的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Java?BigDecimal類的一般使用、BigDecimal轉(zhuǎn)double方式
這篇文章主要介紹了Java?BigDecimal類的一般使用、BigDecimal轉(zhuǎn)double方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java中LinkedHashMap的實(shí)現(xiàn)詳解
LinkedHashMap是Java中的一個(gè)Map容器,它繼承自HashMap,并且還可以對(duì)元素進(jìn)行有序存儲(chǔ),本文將介紹LinkedHashMap的實(shí)現(xiàn)原理以及使用方法,并且提供相應(yīng)的測試用例和全文小結(jié),需要的可以參考下2023-09-09Spring Cloud Feign實(shí)例講解學(xué)習(xí)
這篇文章主要介紹了Spring Cloud Feign實(shí)例講解學(xué)習(xí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02SpringCloud聲明式Feign客戶端調(diào)用工具使用
這篇文章主要為大家介紹了SpringCloud聲明式Feign客戶端調(diào)用工具使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08