今天搞一个小Demo的时候,卡了半小时,网上各种查资料,最后发现是个非常傻逼的错误。
想了下原因,主要是:
以前项目基本都用application.properties,自己写Demo都是用yml+通用Mapper,很少用XML。而今天yml+通用Mapper写到一半,突然想用Mapper.xml写个SQL,然后就出问题了。
具体什么问题的?后面再说。
新建一个SpringBoot项目:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bravo</groupId>
<artifactId>time_format</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>time_format</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 6060
logging:
level:
com:
bravo:
debug
spring:
datasource:
url: jdbc:mysql:///test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
启动类
@SpringBootApplication
@MapperScan("com.bravo") // 扫描通用Mapper接口
public class TimeFormatApplication {
public static void main(String[] args) {
SpringApplication.run(TimeFormatApplication.class, args);
}
}
POJO
@Data
@Table(name = "t_time_format")
public class TimeFormat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SELECT LAST_INSERT_ID()")
private Integer id;
private String name;
@Column(name = "timestamp_format")
private Date timestampFormat;
@Column(name = "datetime_format")
private Date datetimeFormat;
}
Controller
@RestController
public class TimeController {
@Autowired
private TimeFormatMapper timeFormatMapper;
/**
* JSON提交 考察入参格式
*
* @param timeFormat
* @return
*/
@PostMapping("/postTimeJson")
public TimeFormat PostTimeJson(@RequestBody TimeFormat timeFormat) {
return timeFormat;
}
/**
* 查询所有记录(考察返回值格式)
*
* @return
*/
@GetMapping("/getAllTime")
public List<TimeFormat> getAllTime() {
List<TimeFormat> timeFormats = this.timeFormatMapper.selectAll();
System.out.println(timeFormats);
return timeFormats;
}
}
Mapper(Service省略)
public interface TimeFormatMapper extends Mapper<TimeFormat> {
}
启动项目访问/getAllTime接口:
说明通用Mapper没问题。
然后出于某些原因,我想手写SQL,于是新建了一个TimeFormatMapper.xml:
<?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.bravo.timeformat.TimeFormatMapper">
<resultMap id="BaseResultMap" type="com.bravo.timeformat.TimeFormat">
<!--
WARNING - @mbg.generated
-->
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="timestamp_format" jdbcType="TIMESTAMP" property="timestampFormat"/>
<result column="datetime_format" jdbcType="TIMESTAMP" property="datetimeFormat"/>
</resultMap>
<!--根据时间查询-->
<select id="getByDate" resultType="com.bravo.timeformat.TimeFormat">
SELECT
id, `name`, timestamp_format, datetime_format
FROM
t_time_format t
<where>
<if test="timestampFormat != null">
and t.timestamp_format = #{timestampFormat}
</if>
<if test="datetimeFormat != null">
and t.datetime_format = #{datetimeFormat}
</if>
</where>
</select>
</mapper>
在Mapper接口中写了一个方法:
点击箭头也能跳转到XML。
在Controller中调用:
修改application.xml,配置mapper.xml的扫描路径:
Postman调用报错:
虽然我猜肯定是XML扫描问题,但我以为是路径写错了,检查了一下,并没有错误。application.xml中写的是classpath:mapper/*.xml,而我的XML确实在这下面:
XML本身namespace啥的都是对的,点击能跳转到对应的接口和POJO。
找了5分钟,没找出问题。算了,扫描不行的话就在resources下新建同名的目录吧:
重新启动项目:
搞定。
但是我仍然很纠结为啥刚才扫描失败,在网上各种查,答案五花八门,我都试了一遍,没用...
后来重新把yml改成properties试了一下:
logging.level.com.bravo.timeformat=debug
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///test
spring.datasource.username=root
spring.datasource.password=root
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
mybatis.mapper-locations=classpath:mapper/*.xml
发现能扫描到mapper/*.xml,于是肯定是yml出问题了。
重新审视yml的配置,被自己气到:
不知道为啥,mybatis配置写到了spring下面去了...当时顺着提示就回车了,看都没看光标在哪。改正后:
搞定了。
yml虽然提示挺强大,但太依赖格式的缩进了。我自己不是很喜欢python的代码格式,个人认为没有花括号并不意味着简洁,反而让代码的可读性下降不少。
再举例几个很久以前使用MyBatis时遇到过的坑。
坑1:
resources下建文件夹不能用.,而应该用/。
比如,我们在java目录下建多级package是这样的:
但resources下你如果也这样,得到的其实是一个文件夹,而不是多级文件夹:
正确的做法是:
坑2:
要注意数据库和POJO的驼峰映射。比如,如果我把resultMap改为resultType:
<!--根据时间查询-->
<select id="getByDate" resultType="com.bravo.timeformat.TimeFormat">
SELECT
id, `name`, timestamp_format, datetime_format
FROM
t_time_format t
<where>
<if test="timestampFormat != null">
and t.timestamp_format = #{timestampFormat}
</if>
<if test="datetimeFormat != null">
and t.datetime_format = #{datetimeFormat}
</if>
</where>
</select>
重新查询会发现时间为null:
原因在于数据库字段是timestamp_format,而POJO是timestampFormat,会映射失败。
通用Mapper的接口是没问题的,因为通用Mapper会自动映射:
但getByDate()这个方法的映射不是通用Mapper管的,需要我们自己手动开启MyBatis的驼峰命名:
2020-03-21 22:52:41