SpringBoot整合Mybatis與MybatisPlus方法詳細(xì)講解
一、整合MyBatis操作
官網(wǎng):MyBatis · GitHub
SpringBoot官方的Starter:spring-boot-starter-*
第三方的starter的格式: *-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
1、配置模式
全局配置文件
- SqlSessionFactory: 自動(dòng)配置好了
- SqlSession:自動(dòng)配置了 SqlSessionTemplate 組合了SqlSession
- @Import(AutoConfiguredMapperScannerRegistrar.class);
- Mapper: 只要我們寫的操作MyBatis的接口標(biāo)準(zhǔn)了 @Mapper 就會(huì)被自動(dòng)掃描進(jìn)來
- 可以修改配置文件中 mybatis 開始的所有配置;
@EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置項(xiàng)綁定類。
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}
@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties# 配置mybatis規(guī)則
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置
Mapper接口--->綁定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.atguigu.admin.mapper.AccountMapper">
<!-- public Account getAcct(Long id); -->
<select id="getAcct" resultType="com.atguigu.admin.bean.Account">
select * from account_tbl where id=#{id}
</select>
</mapper>配置 private Configuration configuration; mybatis.configuration下面的所有,就是相當(dāng)于改mybatis全局配置文件中的值
# 配置mybatis規(guī)則
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
可以不寫全局;配置文件,所有全局配置文件的配置都放在configuration配置項(xiàng)中即可
- 導(dǎo)入mybatis官方starter
- 編寫mapper接口。標(biāo)準(zhǔn)@Mapper注解
- 編寫sql映射文件并綁定mapper接口
- 在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建議;配置在mybatis.configuration)
2、注解模式
@Mapper
public interface CityMapper {
@Select("select * from city where id=#{id}")
public City getById(Long id);
public void insert(City city);
}3、混合模式
@Mapper
public interface CityMapper {
@Select("select * from city where id=#{id}")
public City getById(Long id);
//綁定在mapper映射文件中
public void insert(City city);
}總結(jié):
- 引入mybatis-starter
- 配置application.yaml中,指定mapper-location位置即可
- 編寫Mapper接口并標(biāo)注@Mapper注解
- 簡單方法直接注解方式
- 復(fù)雜方法編寫mapper.xml進(jìn)行綁定映射
- @MapperScan("com.atguigu.admin.mapper") 簡化,其他的接口就可以不用標(biāo)注@Mapper注解
二、整合 MyBatis-Plus 完成CRUD
1、什么是MyBatis-Plus
MyBatis-Plus(簡稱 MP)是一個(gè)MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。
建議安裝 MybatisX 插件
2、整合MyBatis-Plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>自動(dòng)配置
- MybatisPlusAutoConfiguration 配置類,MybatisPlusProperties 配置項(xiàng)綁定。mybatis-plus:xxx 就是對(duì)mybatis-plus的定制
- SqlSessionFactory 自動(dòng)配置好。底層是容器中默認(rèn)的數(shù)據(jù)源
- mapperLocations 自動(dòng)配置好的。有默認(rèn)值。classpath*:/mapper/**/*.xml;任意包的類路徑下的所有mapper文件夾下任意路徑下的所有xml都是sql映射文件。 建議以后sql映射文件,放在 mapper下
- 容器中也自動(dòng)配置好了 SqlSessionTemplate
- @Mapper 標(biāo)注的接口也會(huì)被自動(dòng)掃描;建議直接 @MapperScan("com.atguigu.admin.mapper") 批量掃描就行
優(yōu)點(diǎn):
- 只需要我們的Mapper繼承 BaseMapper 就可以擁有crud能力
3、CRUD功能
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Long id,
@RequestParam(value = "pn",defaultValue = "1")Integer pn,
RedirectAttributes ra){
userService.removeById(id);
ra.addAttribute("pn",pn);
return "redirect:/dynamic_table";
}
@GetMapping("/dynamic_table")
public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,Model model){
//表格內(nèi)容的遍歷
// response.sendError
// List<User> users = Arrays.asList(new User("zhangsan", "123456"),
// new User("lisi", "123444"),
// new User("haha", "aaaaa"),
// new User("hehe ", "aaddd"));
// model.addAttribute("users",users);
//
// if(users.size()>3){
// throw new UserTooManyException();
// }
//從數(shù)據(jù)庫中查出user表中的用戶進(jìn)行展示
//構(gòu)造分頁參數(shù)
Page<User> page = new Page<>(pn, 2);
//調(diào)用page進(jìn)行分頁
Page<User> userPage = userService.page(page, null);
// userPage.getRecords()
// userPage.getCurrent()
// userPage.getPages()
model.addAttribute("users",userPage);
return "table/dynamic_table";
}Service
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}
public interface UserService extends IService<User> {
}到此這篇關(guān)于SpringBoot整合Mybatis與MybatisPlus方法詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis與MybatisPlus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合mybatisplus和druid的示例詳解
- Springboot整合mybatisplus的項(xiàng)目實(shí)戰(zhàn)
- SpringBoot整合MyBatisPlus詳解
- springboot整合mybatisplus的方法詳解
- 解決SpringBoot整合MybatisPlus分模塊管理遇到的bug
- SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫訪問功能
- SpringBoot整合MybatisPlus的教程詳解
- SpringBoot+MybatisPlus+代碼生成器整合示例
- Springboot整合MybatisPlus的實(shí)現(xiàn)過程解析
- SpringBoot整合MyBatisPlus配置動(dòng)態(tài)數(shù)據(jù)源的方法
- SpringBoot整合MybatisPlus的簡單教程實(shí)現(xiàn)(簡單整合)
- Springboot接入MyBatisPlus的實(shí)現(xiàn)
相關(guān)文章
Idea自動(dòng)生成Entity實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Idea自動(dòng)生成Entity實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java如何將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了Java將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
java中l(wèi)ambda表達(dá)式的分析與具體用法
這篇文章主要給大家介紹了關(guān)于java中l(wèi)ambda表達(dá)式具體用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Springboot?注解EqualsAndHashCode詳解
注解@EqualsAndHashCode主要用于自動(dòng)生成equals方法和hashCode方法,callSuper屬性為true時(shí),生成的方法會(huì)包括父類字段,為false則只包含當(dāng)前類字段,IDEA工具中有檢查提示并可自動(dòng)修復(fù)相關(guān)代碼,確保注解正確使用,更多詳解可查閱相關(guān)文檔2024-10-10
SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud之微服務(wù)容錯(cuò)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
詳解JAVA中ListIterator和Iterator的辨析
這篇文章主要為大家詳細(xì)介紹了JAVAListIterator和Iterator的辨析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
Java數(shù)據(jù)結(jié)構(gòu)之樹和二叉樹的相關(guān)資料
這篇文章主要介紹了Java?數(shù)據(jù)結(jié)構(gòu)之樹和二叉樹相關(guān)資料,文中通過示例代碼和一些相關(guān)題目來做介紹,非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下!2023-01-01
java向es中寫入數(shù)據(jù)報(bào)錯(cuò)org.elasticsearch.action.ActionReque問題
這篇文章主要介紹了java向es中寫入數(shù)據(jù)報(bào)錯(cuò)org.elasticsearch.action.ActionReque問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
解決sharding JDBC 不支持批量導(dǎo)入問題
這篇文章主要介紹了解決sharding JDBC 不支持批量導(dǎo)入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

