使用mybatis-plus的五个步骤

​ 使用mybatis-plus的五个步骤

  1. 配置文件中引入映射文件

    重点:需要在application.yml文件中配置mybatis-plus.mapper-locations值,保证xml文件被读取到,即可正常运行。

    1.1当xml文件不在resources目录下

    ​ (1)mapper和mapper.xml在同一个目录下,且文件名相同

    mybatis-plus:
       mapper-locations: classpath:*/mapper/*.xml  #指定sql映射文件
    
    
    
    
        
            src/main/java
            
                **/*.xml
            
            
            true
        
    
    
    

    ​ (2)mapper和mapper.xml在同一个目录下,但文件名不相同

    
    
        src/main/resources
        
            **/*.xml
            a.properties
        
        true
    
    
    
    
        src/main/resources
        false
        
            a.properties
        
    
    
    

    ​ (3) mapper和mapper.xml不在同一个目录下

    在application.properties中做如下配置:

    1.2当xml文件在resources目录下

    mybatis.mapper-locations=classpath:/mapper/*.xml
    
    

    总结:

    1.当mapper文件和xml文件在同一个目录下(非resources目录下),需要配置pom.xml文件,使xml文件能够被打进jar包,不需要额外配置yml文件;
    2.当mapper文件在/java目录下,xml文件在resources目录下,需要配置yml文件使得xml文件能够被找到。

  2. 在引导类上添加@MapperScan扫描所有mapper/dao接口

    @SpringBootApplication
    @MapperScan(basePackages = "com.pms.mapper")
    @EnableSwagger2
    public class PmsApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(PmsApplication.class, args);
        }
    
    
    
  3. 编写mapper接口实现BaseMapper即可

    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.example.demo.entity.Notice;
    
    import java.util.List;
    
    /**
     * Mapper 接口
     *
     * @author Chill
     */
    public interface NoticeMapper extends BaseMapper {
    
    	/**
    	 * 前N条数据
    	 * @param number
    	 * @return
    	 */
    	List topList(Integer number);
    
    	/**
    	 * 自定义分页
    	 * @param page
    	 * @param notice
    	 * @return
    	 */
    	List selectNoticePage(IPage page, Notice notice);
    
    }
    
  4. 编写service接口继承IService,编写xxxService继承ServiceImpl

    import com.example.demo.entity.Notice;
    import org.springhina.core.mp.base.BaseService;
    
    import java.util.List;
    
    /**
     * 服务类
     *
     * @author Chill
     */
    public interface IDynamicService extends BaseService {
    
    	/**
    	 * master数据源的列表
    	 *
    	 * @return
    	 */
    	List masterList();
    
    	/**
    	 * slave数据源的列表
    	 *
    	 * @return
    	 */
    	List slaveList();
    
    }
    
    
    package com.example.demo.service.impl;
    
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.example.demo.entity.Notice;
    import com.example.demo.mapper.NoticeMapper;
    import com.example.demo.service.IDynamicService;
    import org.springhina.core.mp.base.BaseServiceImpl;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * DynamicServiceImpl
     *
     * @author Chill
     */
    @Service
    public class DynamicServiceImpl extends BaseServiceImpl implements IDynamicService {
    
    	@Override
    	public List masterList() {
    		return this.list();
    	}
    
    	@Override
    	@DS("slave")
    	public List slaveList() {
    		return this.list();
    	}
    
  5. 查询分页要添加分页过滤器:

    @Configuration
    public class MybatisPlusConfig {
    
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
            // paginationInterceptor.setOverflow(false);
            // 设置最大单页限制数量,默认 500 条,-1 不受限制
            // paginationInterceptor.setLimit(500);
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    }
    

你可能感兴趣的:(Java模块,db开发,架构之美,mybatis,java,spring)