零基礎搭建boot+MybatisPlus的詳細教程
1.準備工作
1.1 創(chuàng)建數(shù)據庫表
創(chuàng)建表
CREATE TABLE `login`( `id` INT(4) primary key auto_increment, `login_id` VARCHAR(50) UNIQUE, `city` VARCHAR(50) DEFAULT '富平', `password` VARCHAR(50) )
在可視化工具中添加數(shù)據(我不太會寫sql)
1.2 創(chuàng)建boot項目
1.3 創(chuàng)建實體類(映射數(shù)據庫表)
2.使用mybatisPlus(操作數(shù)據庫)
2.1 添加mybatisPlus依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.2 配置數(shù)據庫信息
spring: datasource: url: jdbc:mysql://localhost:3306/test0314?characterEncoding=utf-8&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver
2.3 創(chuàng)建mapper接口
該接口中提供了常用的crud方法,我們只需要從容器中獲取mapper操作數(shù)據即可
package com.hand.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.hand.demo.entity.User; /** * 用戶數(shù)據訪問層接口 * */ public interface UserMapper extends BaseMapper<User> { }
2.4 配置mapper掃描
- 在啟動類中配置我們的mapper在哪個包
- 兩種方法:@Mapper注解(麻煩);@MapperScan(在主啟動類上進行配置)
@SpringBootApplication @MapperScan("com.hand.demo.mapper") public class Demo0318Application { public static void main(String[] args) { SpringApplication.run(Demo0318Application.class, args); } }
2.5 test
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>
在test包下
package com.hand.demo; import com.hand.demo.entity.User; import com.hand.demo.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class Demo0318ApplicationTests { @Autowired private UserMapper userMapper; /** * 獲取UserMapper實現(xiàn)類對象(mybatisPlus容器會使用動態(tài)代理生成該接口的實現(xiàn)類對象,并注入到spring容器中 * 所以我們只需要在這定義一個成員變量,通過注解自動注入即可) * */ @Test public void testQueryAll() { List<User> userList = userMapper.selectList(null); System.out.println(userList); } }
3. 常用設置
3.1 設置表映射規(guī)則
設置表前綴配置
3.2 主鍵生成策略(默認基于雪花算法)
@TableId(type = IdType.AUTO) private Long id;
3.3 全局設置
mybatis-plus: global-config: db-config: table-prefix: id-type: auto
3.4 字段與列名的駝峰映射(默認開啟)
mybatis-plus: global-config: db-config: table-prefix: id-type: auto configuration: map-underscore-to-camel-case: false
3.5 日志設置
mybatis-plus: global-config: db-config: table-prefix: id-type: auto configuration: map-underscore-to-camel-case: false log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.基操
4.1 插入 insert()
4.2 刪除 deleteXxx() map
4.3 更新 updateXxx()
5.Wrapper(條件構造器)
5.1
Wrapper AbstractWrapper QueryWrapper UpdateWrapper
QueryWrapper的select可以設置需要查詢的列
6. service層使用
- 不需要手動注入該泛型內的mapper
- 如果需要別的mapper手動注入就行
package com.hand.demo.service; import com.baomidou.mybatisplus.extension.service.IService; import com.hand.demo.entity.User; public interface UserService extends IService<User> { }
package com.hand.demo.service.Impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.hand.demo.entity.User; import com.hand.demo.mapper.UserMapper; import com.hand.demo.service.UserService; @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
@Autowired private UserService userService; @Test public void testService() { List<User> list = userService.list(); System.out.println(list); }
- 也有自己的批量操作等(batch)
- 自定義方法(多表關聯(lián))
7. 代碼生成器(未完待續(xù))
- 每個接口都在繼承相同的BaseMapper,IService(代碼冗余,繁瑣)
- MybatisPlus提供的代碼生成器,一鍵生成mvc三層所有代碼
- 如何使用,引入下邊的包
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency>
到此這篇關于零基礎搭建boot+MybatisPlus的文章就介紹到這了,更多相關boot+MybatisPlus搭建內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決swaggerUI頁面沒有顯示Controller方法的坑
這篇文章主要介紹了解決swaggerUI頁面沒有顯示Controller方法的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Service層異常拋到Controller層處理還是直接處理問題分析
這篇文章主要為大家介紹了Service層異常拋到Controller層處理還是直接處理的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Linux下Java開發(fā)環(huán)境搭建以及第一個HelloWorld
這篇文章主要介紹了Linux下Java開發(fā)環(huán)境搭建以及第一個HelloWorld的實現(xiàn)過程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09Springboot中spring-boot-starter-quartz的使用及說明
這篇文章主要介紹了Springboot中spring-boot-starter-quartz的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12