Loading...

1.引言

最近一直在用mybatis plus(简称MP),感觉真心好用,完全脱离了xml文件,在此介绍给大家!!!

2.依赖

1
2
3
4
5
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>

3.spring boot数据源配置

1
2
#mybatis-plus.mapper-locations=com/example/demo/mapper/xml/*.xml
#mybatis-plus.type-aliases-package=com.example.demo

4.代码生成器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.demo.main;

import java.sql.SQLException;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class MyBatisPlusGenerator {

public static void main(String[] args) throws SQLException {

String author = "zjx";
String outputDir = "D:\\Java\\Work\\kshf-gh-dis\\src\\main\\java";// 项目所在路径
boolean entityLombokModel = true;// 是否生成lombok类型的实体类(建议生成lombok,修改表时只需要添加字段就可以了)
boolean fileOverride = false;// 文件覆盖
String url = "jdbc:mysql://localhost:3306/kshf-gh-dis?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false&useUnicode=true";// 数据库url
String username = "root";// 数据库username
String password = "000000";// 数据库password
String[] tables = { "hortimax" };// 表名,可以为数组
String parent = "com.example.demo";// 包路径

// 1. 全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(entityLombokModel) // 是否支持AR模式
.setAuthor(author) // 作者
.setOutputDir(outputDir) // 生成路径
.setFileOverride(fileOverride) // 文件覆盖
.setIdType(IdType.AUTO) // 主键策略
.setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I
// IEmployeeService
.setBaseResultMap(entityLombokModel)// 生成基本的resultMap
.setBaseColumnList(entityLombokModel);// 生成基本的SQL片段
// 2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型
.setDriverName("com.mysql.cj.jdbc.Driver").setUrl(url).setUsername(username).setPassword(password);
// 3. 策略配置globalConfiguration中
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(entityLombokModel) // 全局大写命名
.setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
.setEntityLombokModel(entityLombokModel)// 生成lombok类型的实体类
.setInclude(tables); // 生成的表
// 4. 包名策略配置
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent(parent).setMapper("mapper")// dao
.setService("service")// servcie
.setController("controller")// controller
.setEntity("entity");// entity
// 5. 整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig);
// 6. 执行
ag.execute();
}

}

5.分页插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.demo.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

@Configuration
@MapperScan({ "com.example.demo.mapper" })
@EnableTransactionManagement
public class DB {

/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}

6.开始使用

6.1 自带crud操作

Mapper
service

6.2 条件构造器

介绍几种常用的

1. eq 同 =
1
2
3
QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>();
queryWrapper.eq("name", "zjx").eq("TS", "2019-02-22");
Form form = this.getOne(queryWrapper);

此处sql等同于

select * from Form where name = “zjx” and ts = “2019-02-22”;

2. ne 同 <>
3. ge gt le lt
  • ge 同 >=
  • gt 同 >
  • le 同 <=
  • lt 同 <
    4. LIKE 同 ‘%值%’
    1
    2
    3
    QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>();
    queryWrapper.like("name", "zjx");
    Form form = this.getOne(queryWrapper);
    此处sql等同于

    select * from Form where name like “%zjx%” ;

    5. in 同 in
    1
    2
    3
    QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>();
    queryWrapper.in("id", 1,2,3);
    Form form = this.getOne(queryWrapper);
    此处sql等同于

    select * from Form where id in (1,2,3);

    6. orderByAsc.orderByDesc,groupBy

    orderByAsc(“id”, “name”) —> order by id ASC,name ASC
    orderByDesc(“id”, “name”) —> order by id DESC,name DESC
    groupBy(“id”, “name”) —> group by id,name

6.3 分页

mapper中使用注解形式

1
2
3
//强调  sql语句千万不要加分号;
@Select("select * from form where name = #{name}")
public List<Form> getList(String name, IPage<Form> page )

serviceImpl中

1
2
3
4
5
6
7
@Override
public IPage<Form> getList(String name, int pageNum, int pageSize) {
IPage<Form> page = new Page<Form>(pageNum, pageSize);
List<Form> iList = FormMapper.getList(name, page);
page.setRecords(iList);
return page;
}

如果你喜欢这篇文章,请点个赞,加个关注吧!!!