本文首發於頭條號【Happyjava】。Happy的掘金地址:https://juejin.im/user/5cc2895df265da03a630ddca,Happy的我的博客:http://blog.happyjava.cn。歡迎轉載,但須保留此段聲明。java
來自官方對於mybatis plus的介紹:MyBatis-Plus(簡稱 MP)是一個 MyBatis 的加強工具,在 MyBatis 的基礎上只作加強不作改變,爲簡化開發、提升效率而生。其支持如下特性:mysql
mybatis plus,對於mybatis的加強不是一點半點的,這二者結合使用,真的能夠大幅的提高開發效率。目前,也有衆多互聯網公司正在使用mybatis plus。下面,就讓咱們快速上手mybatis plus吧。spring
id | name | age | |
---|---|---|---|
1 | Jone | 18 | test1@baomidou.com |
2 | Jack | 20 | test2@baomidou.com |
3 | Tom | 28 | test3@baomidou.com |
4 | Sandy | 21 | test4@baomidou.com |
5 | Billie | 24 | test5@baomidou.com |
建表語句:sql
DROP TABLE IF EXISTS user; CREATE TABLE user ( id BIGINT(20) NOT NULL COMMENT '主鍵ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年齡', email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱', PRIMARY KEY (id) );
數據:數據庫
DELETE FROM user; INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com');
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency>
一個是mysql的依賴,一個是mybatis-plus的依賴springboot
@Data public class User { private Long id; private String name; private Integer age; private String email; }
@Mapper public interface UserMapper extends BaseMapper<User> { }
上面的步驟作完,就能夠開始使用mybatis plus了。mybatis
@RunWith(SpringRunner.class) @SpringBootTest public class MybatisPlusApplicationTests { @Autowired private UserMapper userMapper; @Test public void test1() { List<User> users = userMapper.selectList(null); users.forEach(e -> { System.out.println(e.toString()); }); } }
輸出結果:app
經過Select Insert Update Delete 註解,能夠寫增刪查改語句,以下:分佈式
@Mapper public interface UserMapper extends BaseMapper<User> { @Select("select * from user where id = #{id}") User findUserById(int id); }
經過xml的方式寫sql的話,須要先配置xml的位置:工具
mybatis-plus.mapper-locations=classpath:mappers/*.xml
示例xml配置以下:userMapper.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="happy.mybatisplus.mapper.UserMapper"> <select id="findUserByName" parameterType="string" resultType="happy.mybatisplus.entity.User"> SELECT * FROM user WHERE name = #{name} </select> </mapper>
這些都屬於mybatis的範疇了,這裏就不作過多介紹。
下面列出一些mybatis-plus中經常使用的東西:
一、指定實體類對應的數據庫表名
@TableName(value = "user")
在實體類與數據庫代表不是對應的狀況下使用,如:
@Data @TableName(value = "user") public class UserEntity { private Long id; private String name; private Integer age; private String email; }
二、指定字段對應的數據庫字段名
@TableField(value = "age")
如:
@Data @TableName(value = "user") public class UserEntity { private Long id; private String name; @TableField(value = "age") private Integer userAge; private String email; }
三、插入數據,獲得主鍵
調用mybatis plus 提供的insert方法,執行後,會把主鍵設置到入參的id屬性中,以下:
四、分頁
配置分頁插件:
@Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }
分頁示例:
這裏演示了springboot快速使用mybatis-plus,mybatis-plus有很是豐富的用法,這裏無法一一列舉。有興趣的朋友,能夠直接到官方文檔裏查看。