MybatisPlus操作模板
生活随笔
收集整理的這篇文章主要介紹了
MybatisPlus操作模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 添加相關依賴
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion><exclusion><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId></exclusion></exclusions></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1</version></dependency><!--mysql依賴--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--lombok用來簡化實體類--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency></dependencies>2. 編寫配置文件
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false&serverTimezone=UTCusername: rootpassword: 090xxxx server:port: 8074 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: auto3. 編寫實體類
package com.atguigu.pojo; @Data public class User {private Long id;private String name;private Integer age;private String email;//插入時自動填充@TableField(fill = FieldFill.INSERT)private Date createTime;//插入與更新時自動填充@TableField(fill = FieldFill.INSERT_UPDATE)private Date updateTime;@Version //樂觀鎖@TableField(fill=FieldFill.INSERT) //插入時自動填充private Integer version;//邏輯刪除@TableLogicprivate Integer deleted; }4. 編寫映射文件
package com.atguigu.mapper;@Repository public interface UserMapper extends BaseMapper<User> { }5.編寫配置MP插件
package com.atguigu.config;import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @FileName: MpConfig* @Author Steven* @Date: 2021/3/12*/ @Configuration @MapperScan("com.atguigu.mapper") public class MpConfig {/*** 樂觀鎖插件* /@Beanpublic OptimisticLockerInterceptor optimisticLockerInterceptor(){return new OptimisticLockerInterceptor();}/*** 分頁插件* @return 返回分頁攔截器*/@Beanpublic PaginationInterceptor paginationInterceptor(){return new PaginationInterceptor();}}6.編寫配置自動填充類
/*** @FileName: MyMetaObjectHandler* @Author Steven* @Date: 2021/3/12*/ @Component public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName("createTime",new Date(),metaObject);this.setFieldValByName("updateTime",new Date(),metaObject);this.setFieldValByName("version",1,metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("updateTime",new Date(),metaObject);} }編寫測試類
package com.atguigu; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class MybatisTests1 {@AutowiredUserMapper userMapper;@Testpublic void listUser(){List<User> users = userMapper.selectList(null);System.out.println(users);}@Testpublic void insert(){User user=new User();user.setName("張宏");user.setAge(24);user.setEmail("test11@baomidou.com");userMapper.insert(user);}@Testpublic void update(){User user=new User();user.setId(1370148840212037635L);user.setName("吳婷");userMapper.updateById(user);}@Testpublic void testOptimisticLock(){User user = userMapper.selectById(1370148840212037636L);user.setName("張琳");int updateById = userMapper.updateById(user);System.out.println(updateById);}@Testpublic void testBatch(){List<User> userList = userMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4));System.out.println(userList);}/*** 簡單條件查詢*/@Testpublic void testMap(){Map<String,Object> hashmap=new HashMap<>();hashmap.put("name","Jone");hashmap.put("age",18);List<User> userList = userMapper.selectByMap(hashmap);System.out.println(userList);}/*** 分頁查詢*/@Testpublic void testSelectPage(){Page<User> page=new Page<>(1,3);Page<User> userPage = userMapper.selectPage(page, null);//當前頁long current = userPage.getCurrent();//查詢當前數據集合List<User> userList = userPage.getRecords();userList.stream().map(User::getName).forEach(System.out::println);//記錄總數long total = userPage.getTotal();//是否有上一頁boolean hasPrevious = userPage.hasPrevious();//是否有下一頁boolean hasNext = userPage.hasNext();System.out.println("當前頁:"+current);System.out.println("當前數據集合:"+userList);System.out.println("總記錄數:"+total);System.out.println("是否有上一頁:"+hasPrevious);System.out.println("是否有下一頁:"+hasNext);}@Testpublic void testSelectMap(){Page<Map<String,Object>> page=new Page<>(1,3);Page<Map<String,Object>> userPage= userMapper.selectMapsPage(page, null);//當前數據集合List<Map<String, Object>> records = userPage.getRecords();records.forEach(System.out::println);//當前頁long current = userPage.getCurrent();//記錄總數long total = userPage.getTotal();//是否有上一頁boolean hasPrevious = userPage.hasPrevious();//是否有下一頁boolean hasNext = userPage.hasNext();//總頁數long size = userPage.getSize();System.out.println("當前頁:"+current);System.out.println("是否有上一頁:"+hasPrevious);System.out.println("是否有下一頁:"+hasNext);System.out.println("總頁數:"+size);System.out.println("總記錄數:"+total);}/*** 根據ID刪除數據*/@Testpublic void testDeleteById(){int result = userMapper.deleteById(1370148840212037634L);System.out.println(result);}/*** 根據ID批量刪除數據*/@Testpublic void testDeleteByIds(){int deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(1370144581273784322L, 1370145757251784706L));System.out.println(deleteBatchIds);}/*** 簡單條件刪除*/@Testpublic void testDeleteByMap(){Map<String,Object> map=new HashMap<>();map.put("name","張宏");map.put("email","test11@baomidou.com");int result = userMapper.deleteByMap(map);System.out.println(result);}/*** 邏輯刪除*/@Testpublic void testLogicDelete(){int id = userMapper.deleteById(1370148840212037633L);System.out.println(id);}/*** 邏輯刪除查詢*/@Testpublic void testLogicDeleteSelect(){List<User> userList = userMapper.selectList(null);System.out.println(userList);}@Testpublic void testQuery(){QueryWrapper<User> queryWrapper=new QueryWrapper<>();queryWrapper//isNotNull:不等于空.isNotNull("email")//ge:年齡大于等于18.ge("age",18)//gt:大于.gt("age",12)//le:小于等于.le("id",10)//lt:小于.lt("id",8)//eq:等于.eq("id",4)//ne:不等于.ne("age",29)//in:在某個范圍內.in("id", Arrays.asList(1,2,3,4,5))//notIn:不在某個區間.notIn("age",Arrays.asList(9,10,11))//在某個區間內.between(true,"id",1,7)//模糊匹配類似于:%n%.like("name","n")//按年齡降序.orderByDesc("age")//按條件升序或降序.orderBy(false,true,"id");List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}} 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的MybatisPlus操作模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全局统一返回结果包装信息
- 下一篇: SpringBoot整合easyexce