mybatis-plus入门了解和简单使用

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作

  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库

  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

它的框架结构:

下面是我实现的简单crud操作。

第一步先导入依赖

    <dependencies>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-plus启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency> <!--h2内存数据库-->
<!-- <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>--> <!--可以帮忙省略掉 Get/Set 方法用于简化代码-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springboot 模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

第二步建topic表并且插入数据

CREATE TABLE `topic` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`tag` varchar(255) DEFAULT NULL,
`in_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8; INSERT INTO `topic` (`id`, `title`, `content`, `tag`, `in_time`)
VALUES
(1, 'title1', 'content1', 'tag1', '2019-04-15 09:33:35'),
(2, 'title2', 'content2', 'tag2', '2019-04-15 09:33:35'),
(3, 'title3', 'content3', 'tag3', '2019-04-15 09:33:35'),

第三步创建实体类

@Data //该注释可以帮助我们建立setter和getter方法
public class Topic {
private Integer id;
private String title;
private String content;
private String tag; @DateTimeFormat(pattern = "yyyy-MM-dd")
private Date inTime;
}

第四步创建mapper文件

//需要建立这么一个接口并且继承BaseMapper即可
public interface TopicMapper extends BaseMapper<Topic> {
}

第五步编写service方法

@Service
public class TopicService {
@Autowired
private TopicMapper topicMapper;
//查询所有
public List<Topic> list() {
return topicMapper.selectList(new QueryWrapper<>());
}
//根据id查询
public Topic selectbyId(int id) {
return topicMapper.selectOne(new QueryWrapper<Topic>().eq("id", id));
}
//插入数据
public void insert(Topic topic) {
topicMapper.insert(topic);
}
//根据id删除数据
public void deletebyid(int id){
topicMapper.deleteById(id);
}
}

第六步编写控制器

@Controller
public class TopicController {
@Autowired
private TopicService topicService; @RequestMapping("/list")
@ResponseBody
public Object list() {
return topicService.list();
} @RequestMapping("/selectbyid/{id}")
@ResponseBody
public Topic selectOne(@PathVariable int id) {
return topicService.selectbyId(id);
} //添加操作是使用的是thymeleaf页面模板
@RequestMapping("/add")
public String add(Model model) {
model.addAttribute("topic", new Topic());
return "add";
} @RequestMapping("/save")
@ResponseBody
public List<Topic> sava(@ModelAttribute Topic topic) {
topicService.insert(topic);
return topicService.list();
} //删除操作
@RequestMapping("/delete/{id}")
@ResponseBody
public List<Topic> del(@PathVariable int id){
topicService.deletebyid(id);
return topicService.list();
}
}

页面模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>添加页面</title>
</head>
<body>
<form th:action="@{/save}" method="post">
<label>
<input type="text" th:field="${topic.id}" placeholder="请输入id">
</label><br>
<label>
<input type="text" th:field="${topic.title}" placeholder="请输入title">
</label><br>
<label>
<input type="text" th:field="${topic.content}" placeholder="请输入content">
</label><br>
<label>
<input type="text" th:field="${topic.tag}" placeholder="请输入tag">
</label><br>
<label>
<input type="date" th:field="${topic.inTime}" placeholder="请输入date">
</label><br>
<input type="submit" value="提交">&nbsp;
<input type="reset" value="重置">&nbsp;
</form>
</body>
</html>

启动类

@SpringBootApplication
@MapperScan("cn.jason.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

通过以上几个步骤就可以实现简单的crud操作,mybatis-plus真的强大,mapper接口文件只需要继承baseMapper即可。太简化了。

但是在导入包的时候需要注意的是,在导入时只需要导入mybatis-plus-boot-starter就可以了,还有就是mybatis-plus,其他的就不要导入了。

今天先就了解这么多,后面仔细学一学这个强大的玩意儿。

它的更多信息可以查看官网以及慕课网入门视频

mybatis-plus官网链接https://mp.baomidou.com/guide/#特性

mybatis-plus慕课网入门视频链接https://www.imooc.com/learn/1130

mybatis-plus简单了解的更多相关文章

  1. Mybatis框架简单使用

    Mybatis框架简单使用 环境搭建 新建一个JavaWeb项目,在web\WEB-INF\创建lib文件,并且在其下添加Mybatis的核心包以及依赖包,以及Mysql驱动包,junit4测试包等. ...

  2. MyBatis(1)-简单入门

    简介 什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.My ...

  3. MyBatis 使用简单的 XML或注解用于配置和原始映射

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .My ...

  4. 单独使用MyBatis的简单示例

    单独使用MyBatis的简单示例:mybaties-config.xml:MyBatis配置文件 <?xml version="1.0" encoding="UTF ...

  5. Mybatis的简单增删改查

    刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...

  6. Mybatis实现简单的CRUD(增删改查)原理及实例分析

    Mybatis实现简单的CRUD(增删改查) 用到的数据库: CREATE DATABASE `mybatis`; USE `mybatis`; DROP TABLE IF EXISTS `user` ...

  7. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  8. MyBatis学习--简单的增删改查

    jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...

  9. Mybatis的简单示例

    首先新建一个JavaWeb项目并导入mybatis依赖的jar包,同时Mybatis是对数据库的操作所以我们需要在数据库中新建一个表user用来演示. 新建完表之后我们还需要建立相对应的实体类User ...

  10. MyBatis的简单操作

    这里将的是简单的增.删.改.查等基本操作 首先创建java项目,导入相应的依赖包,这里可以参考上一篇博客 1.添加数据 在jike.book.pojo包中,新建java类 JiKeUser.java: ...

随机推荐

  1. android下载网络图片,设置宽高,等比缩放

    使用Picasso组件去下载图片会发现图片宽高会变形不受等比缩放控制,即使设置了图片的 scaleType,可能是对Picasso的api没有用对, Picasso.with(this.activit ...

  2. javascript的历史和入门

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. mysql解决删除数据后,主键不连续问题

    题记:强迫症需求,一个小技巧. 亲测有效 SET @i=; UPDATE table_name SET `); ALTER TABLE table_name AUTO_INCREMENT=; 注意:主 ...

  4. java逆向工程-mybatis-generator

    题记:在快速开发的项目中有使用到,这样可以避免冗余工作 声明:参考于https://www.cnblogs.com/smileberry/p/4145872.html 环境:必须先安装maven环境, ...

  5. FAQ_2

    FAQ-2 1.LoadRunner超时错误: 在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 错误现象1:Action.c(1 ...

  6. SAP Query创建

    一.SAP Query创建步骤 1.创建User Group User Group即用户组,可以将所创建的列表使用权限分配给具体用户,Tcode:SQ03 在使用者群組初始界面里输入自定义名称,单击[ ...

  7. style属性

    style加样式是加在行间,取样式也是在行间取: 我们来看下面这段代码: <!DOCTYPE HTML> <html> <head> <meta charse ...

  8. 《Linux命令行大全》 笔记记录

    1.Shell是什么 2.(文件目录)导航 3.Linux系统 4.操作文件和目录 5.命令的使用 6.重定向 7.透过shell看世界 扩展 引用 8.高级键盘技巧 9.权限 10.进程 11.环境 ...

  9. 看我如何通过邮箱获取IP定位-复现

    环境准备:一台部署了phpstudy 的vps 脚本内容:get_ip.php 该脚本可以生成一个十分隐蔽的图片,并获取客户端的一些敏感信息. 代码内容如下: <?php //show_sour ...

  10. 【经典问题】maximum subset sum of vectors

    AtCoder Beginner Contest 139 Task F Engines 题目大意 给定 $n$ 个二维向量,从中选出若干个,使得它们的和的模最大. 分析 这是一个经典问题,还有一种提法 ...