mybatis plus与springboot结合

5 篇文章 0 订阅
4 篇文章 0 订阅

1.引言

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

2.依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.7.1</version>
</dependency>

3.spring boot数据源配置

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

4.代码生成器

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.分页插件

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 同 =
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 同 ‘%值%’
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
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中使用注解形式

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

serviceImpl中

@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;
	}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值