笔者于2016年入Java,先后从事开发和教育行业,现就职于某供应链公司担任后端开发工程师。整理分享开发必备技术文档。
收藏等于学会,赶紧三连吧,如有问题请评论区留言或者私信哦!
这个技术官网介绍已经非常详细MyBatis-Plus,MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
我在该案例中使用的是IDEA开发工具,使用的是ssm框架大家也可以使用springboot,数据库使用的是mysql,框架的搭建整合就不做演示了,不会的可以参考我之前的博客。
我们使用mybatis无疑是操作数据库,所以我们首先需要创建一个数据库和数据表,如下
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`pass` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
对应的插入语句
INSERT INTO user (id, name, pass) VALUES (1, 'aa', 'sss');
INSERT INTO user (id, name, pass) VALUES (2, 'dd', 'dd');
INSERT INTO user (id, name, pass) VALUES (3, '朱重八', '123');
INSERT INTO user (id, name, pass) VALUES (4, '朱五四', '123');
INSERT INTO user (id, name, pass) VALUES (5, '朱元璋', '123');
INSERT INTO user (id, name, pass) VALUES (6, '朱由检', '123');
这里提供SpringMVC和SpringBpoot两种方式,我们使用IDEA创建一个Maven工程,主要修改pom.xml文件,spring与mybatis的整合文件
SpringMVC
1)pom.xml文件,引入mybatis-plus的依赖
com.baomidou
mybatis-plus
3.1.1
注意:引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题
如果你是用的是springboot项目则添加以下启动器
com.baomidou
mybatis-plus-boot-starter
3.1.1
2)整合文件
SpringMVC
配置 MapperScan
调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory
SpringBoot配置
@SpringBootApplication
@MapperScan("com.zhiyou100.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
以上就是使用mybatis-plus的配置文件的配置,接下来我们看看在代码中如何实现CRUD操作。
1)实体类
package com.zhiyou100.pojo;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class User {
private Integer id;
private String name;
private String pass;
}
小结:这里实体类使用的是lombok插件,可以不用写getter,setter等方法
2)mapper类
package com.zhiyou100.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhiyou100.pojo.User;
public interface User2Mapper extends BaseMapper {
}
小结:我们创建Mapper类,继承BaseMapper,BaseMapper中定义了一些通用的CRUD方法,我们甚至可以不用写实体类的映射文件就可以完成一些简单的CRUD操作
BaseMapper源码如下,不难应该都能看懂,但是需要注意的是你的数据库的表名和字段名必须和实体类名和属性名一致。
public interface BaseMapper extends Mapper {
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map columnMap);
int delete(@Param("ew") Wrapper wrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper updateWrapper);
T selectById(Serializable id);
List selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List selectByMap(@Param("cm") Map columnMap);
T selectOne(@Param("ew") Wrapper queryWrapper);
Integer selectCount(@Param("ew") Wrapper queryWrapper);
List selectList(@Param("ew") Wrapper queryWrapper);
List
3)测试
@RequestMapping("queryall.do")
@ResponseBody
public List queryall(){
List users = user2Mapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
return users;
}
小结:到此使用mybatis-plus完成了一个简单的单表查询,其余的添加,修改,删除,只需要调用对应的方法即可。
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
添加新的依赖,官网上说MyBatis-Plus 从 3.0.3 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖:
com.baomidou
mybatis-plus-generator
3.1.1
引入freemarker依赖:
org.freemarker
freemarker
2.3.28
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
/**
*
* 读取控制台内容
*
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//获取项目的地址
final String projectPath = System.getProperty("user.dir");
System.out.println("projectPath===>"+projectPath);
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("jobob");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
final PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
pc.setParent("com.baomidou.ant");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List focList = new ArrayList();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录");
return false;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
运行main方法,输入模块名和数据表名就会在对应的目录下生成对应的文件,将他们拷贝到项目中即可。
我们在做对数据的操作时,尤其是查询会有很多的条件,我们使用mybatis-plus如何实现条件查询?给我们提供了条件构造器,是由AbstractWrapper和他的两个子类QueryWrapper(LambdaQueryWrapper) UpdateWrapper(LambdaUpdateWrapper) 实现,用于生成 sql 的 where 条件。一下是AbstractWrapper类的部分方法截图,大家通过这写方法名就恍然大悟。
我们通过一个案例来掩饰怎么使用,其他的参考官网条件构造器 | MyBatis-Plus照葫芦画瓢即可,这里介绍一个alleq方法,顾名思义,就是所有的条件都要满足即sql语句中的and,好了其他的去官网看吧。
public List query(){
//设置条件,key是数据库中的字段名,value是值
HashMap map = new HashMap();
map.put("name","张三");
map.put("pass","123");
List users = user2Mapper.selectList(new QueryWrapper().allEq(map));
for (User user : users) {
System.out.println(user);
}
return users;
}
我们在项目中做查询必然会用到分页这个功能,我们来看看mybatis-plus中是如何实现的吧!
我们不需要在pom.xml文件中引入新的依赖。
1)springmvc配置,在mybatis的核心配置文件中即SqlMapConfig.xml文件
2)springboot配置
//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.zhiyou100.service.*.mapper*")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
1)我们在实体类映射文件中写一个sql
2)mapper类,要分页的话传入的参数需要是Page,返回值是IPage。可以继承BaseMapper也可以不继承自己思考下为什么
public interface User2Mapper extends BaseMapper {
IPage selectPage(Page page);
}
3)controller类
private List queryAll() {
//参数1:当前页,参数2:每页数据量
Page page = new Page(2,5);
//也可使用set方法设置
//page.setCurrent(2);
//page.setSize(5);
IPage userIPage = user2Mapper.selectPage(page);
//获取数据List集合。
List records = userIPage.getRecords();
return records;
}
致于前端页面上一页下一页就不写了,无非+1-1之类的操作。mybatis-plus还有很多腻害的插件,可以到官网继续学习。
页面更新:2024-05-01
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号