springboot整合mybatis流程詳解
1.mybatis是什么
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲(chǔ)過程以及高級(jí)映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡(jiǎn)單的 XML 或注解來配置和映射原始類型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 對(duì)象)為數(shù)據(jù)庫中的記錄。
2.整合
兩種方式:
- 新建一個(gè)mybaits-config.xml文件,內(nèi)容配置其中
- 在springboot核心配置文件application.yaml中,配置mybatis內(nèi)容(這邊只展示第二種)
2.0 前期工作:保證可以連接上數(shù)據(jù)庫
導(dǎo)入依賴:
<!--數(shù)據(jù)庫啟動(dòng)器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: xxx
url: jdbc:mysql://localhost:3306/mybatis
springboot中默認(rèn)使用hikari連接池,號(hào)稱最快的連接池。連接池還有DBCP,c3p0,druid…
2.1 導(dǎo)入依賴
<!--引入 mybatis-spring-boot-starter 的依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
2.2 創(chuàng)建包和類
mapper層:
@Mapper public interface EmployeeMapper { public Employee getEmpById(Integer id); }
mapper層對(duì)應(yīng)的xm文件:
<?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.mapper.EmployeeMapper"> <select id="getEmpById" resultType="com.example.entity.Employee"> select * from employee where id = #{id} </select> </mapper>
2.3 在application.yaml配置mybatis
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml # 找到mapper層對(duì)應(yīng)的xml文件
config-location: mybatis-config.xml # mybatis配置文件,resource目錄下
mybaits的屬性設(shè)置參考文檔:https://mybatis.net.cn/configuration.html#settings
3.使用注解版mybaits
在mapper接口的方法上,使用注解增刪改查@Update()、 @Insert()、 @Select()、@Delete()
@Insert("insert into employee (name,age,position) values(#{name},{age},#{position})") void insert(Employee employee); @Select("select * from employee where id = #{id}") void selectById(Integerid);
4.實(shí)戰(zhàn)過程
- 引入mybatis-spring-boot-start
- 配置application.yaml中,指定mapper-locations位置
- 編寫mapper接口并標(biāo)注@Mapper注解
- 簡(jiǎn)單方法直接使用注解
- 復(fù)雜方法編寫在mapper.xml進(jìn)行綁定映射
- @MapperScan(“com.lmh.mapper”)簡(jiǎn)化,該目錄下的mapper接口就可不添加@Mapper注解
到此這篇關(guān)于springboot整合mybatis流程詳解的文章就介紹到這了,更多相關(guān)springboot mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)和異步調(diào)用
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)和異步調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04java底層JDK?Logging日志模塊處理細(xì)節(jié)深入分析
這篇文章主要為大家介紹了java底層JDK?Logging日志模塊處理細(xì)節(jié)深入分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)
這篇文章主要為大家詳細(xì)介紹了java基于springboot+vue3如何大文件的分片上傳與下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-06-06Java 實(shí)戰(zhàn)項(xiàng)目錘煉之網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+jsp+servlet+mysql+ajax實(shí)現(xiàn)一個(gè)網(wǎng)上商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Spring Boot郵箱鏈接注冊(cè)驗(yàn)證及注冊(cè)流程
這篇文章給大家介紹Spring Boot郵箱鏈接注冊(cè)驗(yàn)證問題及注冊(cè)流程分析,通過實(shí)例代碼給大家分享實(shí)現(xiàn)過程,感興趣的朋友跟隨小編一起看看吧2021-07-07java數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列示例介紹
大家好,本篇文章主要講的是java數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列示例介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署
這篇文章主要介紹了IntelliJ IDEA使用maven實(shí)現(xiàn)tomcat的熱部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07