1、项目中引入jar包,我这里使用Maven构建
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>仓库最高版本号</version>
</dependency>
<!--快照版本使用,正式版本无需添加此仓库-->
<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
特别说明: Mybatis及Mybatis-Spring依赖请勿加入项目配置,以免引起版本冲突!!!Mybatis-Plus会自动帮你维护!
2、springboot项目中application.yml文件中加上
mybatisplus:
enabled: true
generic:
enabled: true
dialectType: mysql
传统SSM项目,修改配置文件,将mybatis的sqlSessionFactory替换成mybatis-plus的即可,mybatis-plus只做了一些功能的扩展:
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描Mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.baomidou.springmvc.model.*"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</bean>
</array>
</property>
<!-- 全局配置注入 -->
<property name="globalConfig" ref="globalConfig" />
</bean>
3、创建Mapper、xml,创建Mapper时继承BaseMapper,xml正常(省略xml信息)
public interface UserMapper extends BaseMapper<User> {
}
4、实现类继承ServiceImpl
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
public void queryUserList(UserDto dto){
EntityWrapper<User> ew = new EntityWrapper<User>();
ew.where("deleted={0}", 1);
ew.in("user_type", "1");
ew.eq("role", "1");
ew.eq("status", "1");
ew.orderBy("id");
ew.orderBy("created_time", true);
log.info("selectList condition:{}", ew.getSqlSegment());
List<User> userList = this.selectList(ew);
}
}
更多资料,请查看: mybaits-plus官方文档