springboot integration mybatis-plus considerations

Examples of required dependencies

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.3.2</version>
        </dependency>

        <!-- mybatis-plus-annotation -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-annotation</artifactId>
             <version>3.3.2</version>
        </dependency>

        <!-- mybatis-plus-extension -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
             <version>8.0.21</version>
        </dependency>

The normal configurations such as service mapper need to pay attention to

@SpringBootApplication
// 启动类添加指定扫描包路径,mapper包下放的就是具体的InitTableMapper 接口
// .**.表示通配符 包括子目录
@MapperScan("com.sensor.wksensorapp.**.mapper")
public class WkSensorAppApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(WkSensorAppApplication.class, args);
    }

}

The other is the configuration file

#mybaits相关配置
mybatis-plus:
  ## 注意点 是main/java/下的全路径 com/sensor/wksensorapp/**/mapping/*.xml
  mapper-locations: classpath*:com/sensor/wksensorapp/**/mapping/*.xml, classpath:/META-INF/modeler-mybatis-mappings/*.xml
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: true
    lazy-loading-enabled: true
    multiple-result-sets-enabled: true
    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
  global-config:
    banner: false
    db-config:
      id-type: assign_id
      table-underline: true
    enable-sql-runner: true
  configuration-properties:
    prefix:
    #如果数据库为postgresql,则需要配置为blobType: BINARY
    blobType: BLOB
    #如果数据库为oracle或mssql,则需要配置为boolValue: 1
    boolValue: true

Secondly, there is no need to use any @Mapper or @Repository in the mapper interface

public interface InitTableMapper extends BaseMapper<InitTable> {
    
    
     int selectCount();
}
public interface InitTableService extends IService<InitTable> {
    
    
     int selectCout();
}
@Service
public class InitTableServiceImpl extends ServiceImpl<InitTableMapper, InitTable> implements InitTableService {
    
    
    @Resource
    private InitTableMapper initTableMapper;
    
    @Override
    public int selectCout(){
    
    
        return  initTableMapper.selectCount();
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sensor.wksensorapp.inittable.mapper.InitTableMapper">
    <select id="selectCount2" resultType="java.lang.Integer">
        select count(*) from z_init_table
    </select>
</mapper>

There will generally be a common parent class for things like

@Data
public class BaseEntity {
    
    

    private static final long serialVersionUID = 1L;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 创建人
     */
    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 更新人
     */
    @TableField(fill = FieldFill.UPDATE)
    private Long updateUser;

}

If you want to automatically add something when inserting and modifying, you can implement an interface and intercept the addition

/**
 * 在添加是将realId,自动赋值uuid
 */
@Component
public class CustomMetaObjectHandler implements com.baomidou.mybatisplus.core.handlers.MetaObjectHandler {
    
    
    @Override
    public void insertFill(MetaObject metaObject) {
    
    
        /**
         * 统一为真实的主键设置新增时自动赋值
         */
        this.setFieldValByName("realId", UuidUtils.getUUID(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    
    

    }
}


If this error occurs, it is likely to be a configuration problem

Invalid bound statement (not found): com.sensor.wksensorapp.inittable.mapper.InitTableMapper.selectCount
Invalid bound statement (not found): com.sensor.wksensorapp.inittable.mapper.InitTableMapper.getBaseMapper

Guess you like

Origin blog.csdn.net/weixin_43051544/article/details/131854818