一、前期准备
1.pom依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>2.3</version></dependency>
在对mybatis-plus配置时需要将之前的mybatis和mybatis-spring配置删掉。
2.mybatis-config.xml
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration></configuration>
3.spring-dao.xml
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!--配置整合mybatis-plus过程--><!--1、配置数据库相关参数properties的属性:${url} --><context:property-placeholderlocation="classpath:jdbc.properties"/><!--2、配置数据库连接池--><beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="${jdbc.driver}"/><propertyname="jdbcUrl"value="${jdbc.url}"/><propertyname="user"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/></bean><!--mybatis的sqlsessionFactorybean:org.mybatis.spring.SqlSessionFactoryBean--><!--3、配置mybatis-plus的sqlSessionFactory--><beanid="sqlSessionFactory"class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/><propertyname="typeAliasesPackage"value="com.zhu.mybatisplus.entity"/></bean><!--4、DAO接口所在包名,Spring会自动查找其下的类--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.zhu.mybatisplus.dao"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/></bean></beans>
二、mp的初级使用
1.entity
value="tb_employee")//指定表名 (publicclassEmployee { //value与数据库主键列名一致,若实体类属性名与表主键列名一致可省略valuevalue="id",type=IdType.AUTO)//指定自增策略 (privateIntegerid; //若没有开启驼峰命名,或者表中列名不符合驼峰规则,可通过该注解指定数据库表中的列名,exist标明数据表中有没有对应列value="last_name",exist=true) (privateStringlastName; privateStringemail; privateIntegergender; privateIntegerage; }
@TableName、@TableId、@TableField这三个注解能够快速让实体类与数据库的表关联起来。@Tableld这个注解是对主键的设置,当数据库中的主键字段名和实体类中不同时,靠这个注解能够进行实体类与字段名的映射,它还可以设置自增策略等属性。
2.mapper
publicinterfaceEmplopyeeDaoextendsBaseMapper<Employee> { }
使用mp时,Mapper文件有一个简便的操作,继承BaseMapper.BaseMapper中包含了基础的增删改查方法。
// 插入一条记录intinsert(Tentity); // 根据 entity 条件,删除记录intdelete( (Constants.WRAPPER) Wrapper<T>wrapper); // 删除(根据ID 批量删除)intdeleteBatchIds( (Constants.COLLECTION) Collection<?extendsSerializable>idList); // 根据 ID 删除intdeleteById(Serializableid); // 根据 columnMap 条件,删除记录intdeleteByMap( (Constants.COLUMN_MAP) Map<String, Object>columnMap); // 根据 whereWrapper 条件,更新记录intupdate( (Constants.ENTITY) TupdateEntity, (Constants.WRAPPER) Wrapper<T>whereWrapper); // 根据 ID 修改intupdateById( (Constants.ENTITY) Tentity); // 根据 ID 查询TselectById(Serializableid); // 根据 entity 条件,查询一条记录TselectOne( (Constants.WRAPPER) Wrapper<T>queryWrapper); // 查询(根据ID 批量查询)List<T>selectBatchIds( (Constants.COLLECTION) Collection<?extendsSerializable>idList); // 根据 entity 条件,查询全部记录List<T>selectList( (Constants.WRAPPER) Wrapper<T>queryWrapper); // 查询(根据 columnMap 条件)List<T>selectByMap( (Constants.COLUMN_MAP) Map<String, Object>columnMap); // 根据 Wrapper 条件,查询全部记录List<Map<String, Object>>selectMaps( (Constants.WRAPPER) Wrapper<T>queryWrapper); // 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值List<Object>selectObjs( (Constants.WRAPPER) Wrapper<T>queryWrapper); // 根据 entity 条件,查询全部记录(并翻页)IPage<T>selectPage(IPage<T>page, (Constants.WRAPPER) Wrapper<T>queryWrapper); // 根据 Wrapper 条件,查询全部记录(并翻页)IPage<Map<String, Object>>selectMapsPage(IPage<T>page, (Constants.WRAPPER) Wrapper<T>queryWrapper); // 根据 Wrapper 条件,查询总记录数IntegerselectCount( (Constants.WRAPPER) Wrapper<T>queryWrapper);
当我们使用mp时,对于简单的增删改查,我们完全可以只继承BaseMapper接口,直接调用此接口中的方法进行操作,但是当一些业务需要的复杂增删改查时,仍需要进行Mapper文件的编写。
三、ServiceCURD
mp除了能够在mapper类中写增删改查之外,还可以直接在service及其实现层写入。
publicinterfaceIService<T>
在进行service层编写时要先引入IService,然后在其实现层
/***IService:mp提供的接口。ServiceImpl:mp提供的接口实现类。*ServiceImpl<BaseMapper<T>, T>是IService 的实现类。*/publicclassUserServiceImplextendsServiceImpl<UserMapper,User>implementsUserService{}
这样便可以直接在服务层编写增删改查方法了。
1.Save
// 插入一条记录(选择字段,策略插入)booleansave(Tentity); // 插入(批量)booleansaveBatch(Collection<T>entityList); // 插入(批量)booleansaveBatch(Collection<T>entityList, intbatchSize); 1.SaveOrUpdate
// TableId 注解存在更新记录,否插入一条记录booleansaveOrUpdate(Tentity); // 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法booleansaveOrUpdate(Tentity, Wrapper<T>updateWrapper); // 批量修改插入booleansaveOrUpdateBatch(Collection<T>entityList); // 批量修改插入booleansaveOrUpdateBatch(Collection<T>entityList, intbatchSize);
2.Remove
// 根据 entity 条件,删除记录booleanremove(Wrapper<T>queryWrapper); // 根据 ID 删除booleanremoveById(Serializableid); // 根据 columnMap 条件,删除记录booleanremoveByMap(Map<String, Object>columnMap); // 删除(根据ID 批量删除)booleanremoveByIds(Collection<?extendsSerializable>idList);
3.Update
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlsetbooleanupdate(Wrapper<T>updateWrapper); // 根据 whereWrapper 条件,更新记录booleanupdate(TupdateEntity, Wrapper<T>whereWrapper); // 根据 ID 选择修改booleanupdateById(Tentity); // 根据ID 批量更新booleanupdateBatchById(Collection<T>entityList); // 根据ID 批量更新booleanupdateBatchById(Collection<T>entityList, intbatchSize)
4.Get
// 根据 ID 查询TgetById(Serializableid); // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")TgetOne(Wrapper<T>queryWrapper); // 根据 Wrapper,查询一条记录TgetOne(Wrapper<T>queryWrapper, booleanthrowEx); // 根据 Wrapper,查询一条记录Map<String, Object>getMap(Wrapper<T>queryWrapper); // 根据 Wrapper,查询一条记录<V>VgetObj(Wrapper<T>queryWrapper, Function<?superObject, V>mapper);
5.List
// 查询所有List<T>list(); // 查询列表List<T>list(Wrapper<T>queryWrapper); // 查询(根据ID 批量查询)Collection<T>listByIds(Collection<?extendsSerializable>idList); // 查询(根据 columnMap 条件)Collection<T>listByMap(Map<String, Object>columnMap); // 查询所有列表List<Map<String, Object>>listMaps(); // 查询列表List<Map<String, Object>>listMaps(Wrapper<T>queryWrapper); // 查询全部记录List<Object>listObjs(); // 查询全部记录<V>List<V>listObjs(Function<?superObject, V>mapper); // 根据 Wrapper 条件,查询全部记录List<Object>listObjs(Wrapper<T>queryWrapper); // 根据 Wrapper 条件,查询全部记录<V>List<V>listObjs(Wrapper<T>queryWrapper, Function<?superObject, V>mapper);
6.Page
// 无条件分页查询IPage<T>page(IPage<T>page); // 条件分页查询IPage<T>page(IPage<T>page, Wrapper<T>queryWrapper); // 无条件分页查询IPage<Map<String, Object>>pageMaps(IPage<T>page); // 条件分页查询IPage<Map<String, Object>>pageMaps(IPage<T>page, Wrapper<T>queryWrapper);
7.Count
// 查询总记录数intcount(); // 根据 Wrapper 条件,查询总记录数intcount(Wrapper<T>queryWrapper);
8.query
// 链式查询 普通QueryChainWrapper<T>query(); // 链式查询 lambda 式。注意:不支持 KotlinLambdaQueryChainWrapper<T>lambdaQuery(); // 示例:query().eq("column", value).one(); lambdaQuery().eq(Entity::getId, value).list();
9.update
// 链式更改 普通UpdateChainWrapper<T>update(); // 链式更改 lambda 式。注意:不支持 Kotlin LambdaUpdateChainWrapper<T>lambdaUpdate(); // 示例:update().eq("column", value).remove(); lambdaUpdate().eq(Entity::getId, value).update(entity);
以上便是对MyBatis-Plus的初级使用,作为MyBatis的升级,MP用起来更加方便简洁,能够让我们更快速的进行开发任务,当然MP的使用不止这些,仍需继续进行学习深化。