一、MP介绍
1、简述
MybatisPlus在Mybatis的基础上只做增强,不做改变,就像是魂斗罗中的红人和蓝人一样。
官方的介绍:为简化开发而生
2、MP的特性
3、支持的数据库
mysql、mariadb、oracle、db2、h2、hsql、sqlite、postgresql、sqlserver、presto达梦数据库、虚谷数据库、人大金仓数据库二、MybatisPlus简单使用
1、引入Maven依赖
2、编写application.yml文件
3、编写实体类User
这里使用了lombok,简化了get,set,构造
TableName注解:指定实体类对应的表名,一般在核心配置文件中添加mybatis-plus

/p>
global-config
/p>
db-config
/p>
table-prefix:tb_
TableId注解:设置id生成策略。DataNoArgsConstructorAllArgsConstructorTableName(tb_user)
publicclassUser{
TableId(type=IdType.AUTO)
privateLongid;
privateStringuserName;
TableField(select=false)//查询时不返回该字段值
privateStringpassword;
privateStringname;
privateIntegerage;
TableField(value=email)//指定数据库表中字段名
privateStringmail;
TableField(exist=false)
privateStringaddress;//数据库表中不存在
}
4、编写Mapper持久层,这里继承BaseMapper实体类
Repository
publicinterfaceUserMapperextendsBaseMapperUser{
}
这里和SpringDataJpa类似,只是不需要再传入主键类型
可以看下BaseMapper中的方法:不得不说国人的的插件就是好用,注释都是中文,不用担心看不懂了
publicinterfaceBaseMapperTextendsMapperT{
/**
*插入一条记录
*
*
paramentity实体对象
*/
intinsert(Tentity);
/**
*根据ID删除
*
*
paramid主键ID
*/
intdeleteById(Serializableid);
/**
*根据columnMap条件,删除记录
*
*
paramcolumnMap表字段map对象
*/
intdeleteByMap(
Param(Constants.COLUMN_MAP)MapString,ObjectcolumnMap);
/**
*根据entity条件,删除记录
*
*
paramwrapper实体对象封装操作类(可以为null)
*/
intdelete(
Param(Constants.WRAPPER)WrapperTwrapper);
/**
*删除(根据ID批量删除)
*
*
paramidList主键ID列表(不能为null以及empty)
*/
intdeleteBatchIds(
Param(Constants.COLLECTION)Collection?extendsSerializableidList);
/**
*根据ID修改
*
*
paramentity实体对象
*/
intupdateById(
Param(Constants.ENTITY)Tentity);
/**
*根据whereEntity条件,更新记录
*
*
paramentity
实体对象(set条件值,可以为null)
*
paramupdateWrapper实体对象封装操作类(可以为null,里面的entity用于生成where语句)
*/
intupdate(
Param(Constants.ENTITY)Tentity,Param(Constants.WRAPPER)WrapperTupdateWrapper);
/**
*根据ID查询
*
*
paramid主键ID
*/
TselectById(Serializableid);
/**
*查询(根据ID批量查询)
*
*
paramidList主键ID列表(不能为null以及empty)
*/
ListTselectBatchIds(
Param(Constants.COLLECTION)Collection?extendsSerializableidList);
/**
*查询(根据columnMap条件)
*
*
paramcolumnMap表字段map对象
*/
ListTselectByMap(
Param(Constants.COLUMN_MAP)MapString,ObjectcolumnMap);
/**
*根据entity条件,查询一条记录
*
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
TselectOne(
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据Wrapper条件,查询总记录数
*
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
IntegerselectCount(
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据entity条件,查询全部记录
*
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
ListTselectList(
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据Wrapper条件,查询全部记录
*
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
ListMapString,ObjectselectMaps(
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据Wrapper条件,查询全部记录
*p注意:只返回第一个字段的值/p
*
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
ListObjectselectObjs(
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据entity条件,查询全部记录(并翻页)
*
*
parampage
分页查询条件(可以为RowBounds.DEFAULT)
*
paramqueryWrapper实体对象封装操作类(可以为null)
*/
IPageTselectPage(IPageTpage,
Param(Constants.WRAPPER)WrapperTqueryWrapper);
/**
*根据Wrapper条件,查询全部记录(并翻页)
*
*
parampage
分页查询条件
*
paramqueryWrapper实体对象封装操作类
*/
IPageMapString,ObjectselectMapsPage(IPageTpage,
Param(Constants.WRAPPER)WrapperTqueryWrapper);
}
5、编写测试类,测试下CRUD操作
RunWith(SpringRunner.class)SpringBootTestpublicclassTestUserMapper{Autowired
privateUserMapperuserMapper;
Test
publicvoidtestInsert(){
Useruser=newUser();
user.setMail(
qq.