1.问题
我习惯于使用mybatis-plus,因此很少使用xml写sql,但是有时复杂的查询还是写sql比较方便,因此我在使用xml写sql后,调用时报了org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.fast.dao.FinanceOutcomeDao.getListByPayTime。
2.分析
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
很显然,没有发现xml,于是,我去官网搜索一番,最终在这里找到了答案,https://baomidou.com/pages/56bac0/#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F。
按照下面的配置方法可以做到零配置,yml中不用配置。
拓展
常用配置类
package com.fast.common.config; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.reflection.MetaObject; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.Objects; import java.util.function.Supplier; @Slf4j @Component public class MyMetaObjectHandler implements MetaObjectHandler { @Bean public PaginationInterceptor getPaginationInterceptor() { return new PaginationInterceptor(); } @Override public void insertFill(MetaObject metaObject) { // String username = SecureUtil.getUser().getName(); String username = "管理员"; this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "createBy", String.class, username); this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "updateBy", String.class, username); this.strictInsertFill(metaObject, "del", String.class, "0"); this.strictInsertFill(metaObject, "status", String.class, "0"); } @Override public void updateFill(MetaObject metaObject) { // String username = SecureUtil.getUser().getName(); String username = "管理员"; this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); this.strictUpdateFill(metaObject, "updateBy", String.class, username); } /** * 原方法,当填充字段不是null时,不会进行填充。即前端更新时携带了旧的update信息,就不会填充update */ @Override public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<Object> fieldVal) { Object obj = fieldVal.get(); if (Objects.nonNull(obj)) { metaObject.setValue(fieldName, obj); } return this; } }
解决id过长,前端精度丢失统一配置类
package com.fast.common.config; import com.fasterxml.jackson.databind.deser.std.NumberDeserializers; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.math.BigDecimal; import java.math.BigInteger; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; /** * 全局通用配置*/ @Configuration public class CommonConfig { private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss"; private static final String patternDate = "yyyy-MM-dd"; private static final String patternTime = "yyyy-MM-dd"; /** * 全局格式化 */ @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return jacksonObjectMapperBuilder -> { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(patternDateTime); DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(patternDate); DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(patternTime); //VO返回结果LocalDateTime和LocalDate格式化成字符串后再返回 jacksonObjectMapperBuilder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter)); jacksonObjectMapperBuilder.serializerByType(LocalDate.class, new LocalDateSerializer(dateFormatter)); jacksonObjectMapperBuilder.serializerByType(LocalTime.class, new LocalTimeSerializer(timeFormatter)); //Param接收日期时间数据反序列化 jacksonObjectMapperBuilder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter)); jacksonObjectMapperBuilder.deserializerByType(LocalDate.class, new LocalDateDeserializer(dateFormatter)); jacksonObjectMapperBuilder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(timeFormatter)); //VO返回结果BigInteger和Long格式化成字符串后再返回 jacksonObjectMapperBuilder.serializerByType(BigInteger.class, ToStringSerializer.instance); jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance); jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance); //VO返回BigDecimal数据格式化,数据库存几位,这里展示几位小数,mysql数据库设置保留指定位小数,默认四舍五入,前端传个15.126(15),数据库存的是15.13(15.00) jacksonObjectMapperBuilder.serializerByType(BigDecimal.class, ToStringSerializer.instance); jacksonObjectMapperBuilder.serializerByType(Double.class, ToStringSerializer.instance); jacksonObjectMapperBuilder.serializerByType(Double.TYPE, ToStringSerializer.instance); }; } @Bean public RedisTemplate<String, Object> myRedisTemplate(JedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); // 设置 Key 和 Value 的序列化规则 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 初始化 RedisTemplate 序列化完成 redisTemplate.afterPropertiesSet(); return redisTemplate; } }