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: 只要我們寫(xiě)的操作MyBatis的接口標(biāo)準(zhǔn)了 @Mapper 就會(huì)被自動(dòng)掃描進(jìn)來(lái)
- 可以修改配置文件中 mybatis 開(kāi)始的所有配置;
@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
可以不寫(xiě)全局;配置文件,所有全局配置文件的配置都放在configuration配置項(xiàng)中即可
- 導(dǎo)入mybatis官方starter
- 編寫(xiě)mapper接口。標(biāo)準(zhǔn)@Mapper注解
- 編寫(xiě)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位置即可
- 編寫(xiě)Mapper接口并標(biāo)注@Mapper注解
- 簡(jiǎn)單方法直接注解方式
- 復(fù)雜方法編寫(xiě)mapper.xml進(jìn)行綁定映射
- @MapperScan("com.atguigu.admin.mapper") 簡(jiǎn)化,其他的接口就可以不用標(biāo)注@Mapper注解
二、整合 MyBatis-Plus 完成CRUD
1、什么是MyBatis-Plus
MyBatis-Plus(簡(jiǎn)稱 MP)是一個(gè)MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(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ù)庫(kù)中查出user表中的用戶進(jìn)行展示
//構(gòu)造分頁(yè)參數(shù)
Page<User> page = new Page<>(pn, 2);
//調(diào)用page進(jìn)行分頁(yè)
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ù)庫(kù)訪問(wèn)功能
- SpringBoot整合MybatisPlus的教程詳解
- SpringBoot+MybatisPlus+代碼生成器整合示例
- Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程解析
- SpringBoot整合MyBatisPlus配置動(dòng)態(tài)數(shù)據(jù)源的方法
- SpringBoot整合MybatisPlus的簡(jiǎn)單教程實(shí)現(xiàn)(簡(jiǎn)單整合)
- Springboot接入MyBatisPlus的實(shí)現(xiàn)
相關(guān)文章
springboot項(xiàng)目打包成jar包的圖文教程
有時(shí)候我們會(huì)用IDEA來(lái)開(kāi)發(fā)一些小工具,需要打成可運(yùn)行的JAR包,這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目打包成jar包的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
springboot熱部署知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了關(guān)于springboot熱部署的知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。2019-06-06
Java 數(shù)據(jù)結(jié)構(gòu)七大排序使用分析
這篇文章主要介紹了Java常用的排序算法及代碼實(shí)現(xiàn),在Java開(kāi)發(fā)中,對(duì)排序的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來(lái)詳細(xì)說(shuō)說(shuō)Java常見(jiàn)的排序算法,需要的朋友可以參考一下2022-04-04
SpringBoot3集成iText實(shí)現(xiàn)PDF導(dǎo)出功能
不知道小伙伴們?cè)陧?xiàng)目中有沒(méi)有遇到過(guò)導(dǎo)出 PDF 的需求,小編在之前的 tienchin 項(xiàng)目中有一個(gè)合同導(dǎo)出的功能,需要將文檔導(dǎo)出為PDF,將文檔導(dǎo)出為 PDF 有很多方案,不同方案的優(yōu)缺點(diǎn)也各不相同,今天小編就和大家演示一個(gè),感興趣的小伙伴跟著小編一起來(lái)看看吧2024-10-10
基于OpenID?Connect及Token?Relay實(shí)現(xiàn)Spring?Cloud?Gateway
這篇文章主要介紹了基于OpenID?Connect及Token?Relay實(shí)現(xiàn)Spring?Cloud?Gateway,Spring?Cloud?Gateway旨在提供一種簡(jiǎn)單而有效的方式來(lái)路由到API,并為API提供跨領(lǐng)域的關(guān)注點(diǎn),如:安全性、監(jiān)控/指標(biāo)和彈性2022-06-06
java實(shí)現(xiàn)mysql操作類分享 java連接mysql
這篇文章主要介紹了java實(shí)現(xiàn)的mysql操作類示例,大家在連接數(shù)據(jù)的時(shí)候可以直接使用了2014-01-01
Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門(mén)教程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門(mén),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

