可以基于IDEA的Spring Initializr进行SpringBoot项目的创建,或者移步至Boot官网构建一个简单的web starter项目:https://start.spring.io/

①导入MyBatis-Plus相关的依赖包、数据库驱动、lombok插件包:

pom.xml 文件配置

复制代码

mysql
mysql-connector-java

org.projectlombok
lombok

com.baomidou
mybatis-plus-boot-starter
3.0.5

<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>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope>
</dependency>

复制代码

②配置数据库驱动、日志级别

application.properties配置

复制代码

mysql5 驱动不同,默认驱动:com.mysql.jdbc.Driver

spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus_0312?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
#mysql8 驱动不同:com.mysql.cj.jdbc.Driver、需要增加时区的配置:serverTimezone=GMT%2B8,mysql8的驱动向下兼容mysql5
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
复制代码

1.3.入门Hello World进行数据库操作
基于官网示例来构建数据库表单及POJO数据类:https://baomidou.com/guide/quick-start.html#初始化工程

MybatisPlusApplication启动类:

复制代码
@SpringBootApplication
//配置Mapper接口类扫描
@MapperScan(“com.fengye.mapper”)
//配置Spring Bean注解扫描
@ComponentScan(basePackages = “com.fengye.mapper”)
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
复制代码

UserMapper类:

@Repository //持久层注解,表示该类交给Springboot管理
public interface UserMapper extends BaseMapper {
}

User类:

复制代码
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
复制代码

基础CRUD操作:

复制代码
@SpringBootTest
class MybatisPlusApplicationTests {

@Autowired  //需要配置SpringBoot包扫描,否则此处使用@Autowired会报警告
//@Resource
private UserMapper userMapper;@Test
void testSelect() {System.out.println(("----- selectAll method test ------"));List<User> userList = userMapper.selectList(null);Assert.assertEquals(5, userList.size());userList.forEach(System.out::println);
}@Test
void testInsert(){System.out.println("----- insert method test ------");User user = new User();user.setName("枫夜爱学习");user.setAge(20);user.setEmail("241337663@qq.com");int insertId = userMapper.insert(user);System.out.println(insertId);
}@Test
void testUpdate(){System.out.println("----- update method test ------");User user = new User();user.setId(1370382950972436481L);user.setName("苞米豆最爱");user.setAge(4);user.setEmail("baomidou@github.com");int updateId = userMapper.updateById(user);System.out.println(updateId);System.out.println(user);
}@Test
void testDelete(){System.out.println("----- delete method test ------");int deleteId = userMapper.deleteById(1370386235364118529L);System.out.println(deleteId);
}

}
复制代码

1.3.主键生成策略配置
主键生成策略:

使用@TableId(type = IdType.AUTO,value = “id”) ,value属性值当实体类字段名和数据库一致时可以不写,这里的value指的是数据库字段名称,type的类型有以下几种:

复制代码
public enum IdType {
AUTO(0), //Id自增操作
NONE(1), //未设置主键
INPUT(2), //手动输入,需要自己setID值
ID_WORKER(3), //默认的全局唯一id
UUID(4), //全局唯一id uuid
ID_WORKER_STR(5); //ID_WORKER的字符串表示法

}
复制代码
目前MyBatis-Plus官方文档建议的id主键设置为:@TableId(type = IdType.INPUT)

1.4.自动填充
自动填充功能可以实现针对某个POJO类中的一些时间字段值进行自定义填充策略(非基于数据库表设置timestamp默认根据时间戳更新),实现自动插入和更新操作:

①首先需要在POJO类上需要自动填充的字段上增加@TableField(fill = FieldFill.INSERT)、@TableField(fill = FieldFill.INSERT_UPDATE)注解:

复制代码
@Data
public class User {
//设置主键为需要自己填入设置
@TableId(type = IdType.INPUT)
private Long id;
private String name;
private Integer age;
private String email;
//当创建数据库该字段时,自动执行创建该字段的默认值
@TableField(fill = FieldFill.INSERT, value = “create_time”)
private Date createTime;
//当数据库该字段发生创建与更新操作时,自动去填充数据值
@TableField(fill = FieldFill.INSERT_UPDATE, value = “update_time”)
private Date updateTime;
}
复制代码

②自定义实现类MyMetaObjectHandler实现MetaObjectHandler接口,覆写insertFill与updateFill方法:

复制代码
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
//插入时的填充策略
@Override
public void insertFill(MetaObject metaObject) {
log.info(“start insert fill …”);
/**
* 官网推荐–起始版本 3.3.0(推荐使用),本项目使用3.0.5版本
this.strictInsertFill(metaObject,“createTime”, LocalDateTime.class,LocalDateTime.now());
this.strictUpdateFill(metaObject,“updateTime”,LocalDateTime.class,LocalDateTime.now());
*/
this.setFieldValByName(“createTime”, new Date(), metaObject);
this.setFieldValByName(“updateTime”,new Date(), metaObject);
}

//更新时的填充策略
@Override
public void updateFill(MetaObject metaObject) {log.info("start update fill ....");/***  官方推荐-- 起始版本 3.3.0(推荐),本项目使用3.0.5版本* this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)*/this.setFieldValByName("updateTime", new Date(), metaObject);
}

}
复制代码

2.核心功能
2.1.批量查询
复制代码
// 测试批量查询
@Test
public void testSelectByBatchId(){
List users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}
复制代码

2.2.分页查询
①需要在配置类中注册分页插件注解:

复制代码
//注册分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(false);
return paginationInterceptor;
}
复制代码

②测试分页插件效果:

复制代码
@Test
public void testPageHelper(){
//参数:当前页;页面大小
Page page = new Page<>(1, 2);
userMapper.selectPage(page, null);
List records = page.getRecords();
records.forEach(System.out::println);
System.out.println(“当前页是:” + page.getCurrent());
System.out.println(“每页显示多少条数:” + page.getSize());
System.out.println(“总页数是:” + page.getTotal());
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

【java框架】MyBatis-Plus(1)--MyBatis-Plus快速上手开发及核心功能体验相关推荐

  1. java mybatis的作用,【java框架】MyBatis-Plus(1)--MyBatis-Plus快速上手开发及核心功能体验-博客...

    1.MyBatis-Plus入门开发及配置 1.1.MyBatis-Plus简介 MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变, ...

  2. Java框架搭建-Maven、Mybatis、Spring MVC整合搭建

    Java框架搭建-Maven.Mybatis.Spring MVC整合搭建 1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/e ...

  3. 基于java框架的图书分享系统的设计与开发计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署

    基于java框架的图书分享系统的设计与开发计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署 基于java框架的图书分享系统的设计与开发计算机毕业设计源码+系统+lw文档+mysql数据库 ...

  4. react 快速上手开发_React中测试驱动开发的快速指南

    react 快速上手开发 by Michał Baranowski 通过MichałBaranowski React中测试驱动开发的快速指南 (A quick guide to test-driven ...

  5. Vue快速上手笔记1 - 使用初体验

    Vue快速上手笔记1 - 使用初体验 博主:李俊才 邮箱:291148484@163.com 若本文中存在的错误请告知博主更正 希望对大家有所帮助 专题目录:https://blog.csdn.net ...

  6. 【Java框架】 Hibernate与Mybatis对比

    Hibernate与Mybatis对比 今天同事跟我说现在的小规模公司很少用hibernate,大部分都用mybatis.平时也经常接触这两方面,正好最近不怎么忙,查看网上其他相关技术文档 ,梳理下M ...

  7. 如何快速上手开发微信小程序?

    开发微信小程序其实很简单,对于初学者建议要首先了解一些前端开发的相关的知识(vue),比较熟悉html.css.JavaScript,还有就是要熟悉数据库的操作,基本的增删改查要会. 开发微信小程序快 ...

  8. JAVA框架——struts(一)struts快速入门,struts访问流程,struts配置文件详解,动态方法调用

    一. Struts2框架概述 是一种基于MVC模式的轻量级web框架.本质是一个Servlet.作为控制器建立模型与视图的数据交互.Struts2以WebWord为核心,采用拦截器的机制处理客户的请求 ...

  9. Java 线上问题排查神器 Arthas 快速上手与原理浅谈

    [Arthas 官方社区正在举行征文活动,参加即有奖品拿哦~点击投稿] 作者 | 杨桢栋,笔名叫蛮三刀把刀,是一名一线互联网码农,留美访学一年,主要关注后端开发,数据安全,爬虫,物联网,边缘计算等方向 ...

最新文章

  1. python语言只采用解释一种翻译方式对吗_python-guide翻译
  2. XAMPP下的MYSQL解决中文乱码问题
  3. android文件添加一行代码怎么写,Android:以编程方式添加Textview,而不是将文本包装到下一行(示例代码)...
  4. 携程初赛 携程全球数据中心建设 球面上两点的最短距离 + 最小生成树
  5. robocopy 备份_robocopy的用法,数据库局域网备份
  6. jsp 网页计数器代码
  7. Qt项目--截屏软件
  8. 论window10如何获得最高权限
  9. Retrofit2 详解和使用(一)
  10. CSAPP:malloclab (显式空闲链表 LIFO+首次适配)
  11. Java编程思想读书笔记——复用类
  12. html laber上下居中,laber是什么意思
  13. 搭建内网ntp时间同步服务器
  14. VisualSVN破解
  15. 怎么理解socket ?
  16. 04 现实生活中,你应该如何套利?
  17. 对象赋值时this指向问题 obj1.say = obj2.say;obj1.say()
  18. 用ARM进行汇编语言编程(3)逻辑移位和轮换,条件与分支
  19. 独角兽项目 3 - 反叛军
  20. 技术分享|如何做嵌入式系统的自动化测试

热门文章

  1. Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序 原创 2016年09月29日 11:39:2
  2. 163vip邮箱登录,163邮箱怎么登陆?如何登录163vip邮箱?
  3. 自动禁用并启用所有网络连接源码
  4. 深度学习网络模型梳理
  5. Windows安装Gitea
  6. 智能灯丝灯方案为复古设计注入“ 科技基因 ”
  7. cocos2d-x lua 框架中 self.super.ctor(self, app) 和 self.super:ctor(app) 的区别
  8. 分享一个360加固脱壳模拟器
  9. MySQL教程 你想要的几乎都有
  10. DataGrip for Mac破解步骤详解,期限到2099年