MyBatis-Plus 工具使用之EntityWrapper

1、项目中引入jar包,我这里使用Maven构建

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plusartifactId>
    <version>仓库最高版本号version>
dependency>


<repository>
    <id>snapshotsid>
    <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"/>
        
        <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 ew = new EntityWrapper();
            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 userList = this.selectList(ew);
        }
}

更多资料,请查看: mybaits-plus官方文档

你可能感兴趣的:(JAVA)