开发工具使用的idea2022.3,数据库是mysql5.X系列,springboot用的是2.7系列,使用了java8的joda类型的时间格式完成mybatis的插入操作。下面贴代码说明实现过程
项目结构如下图:
下面说一些关键的maven配置
<!-- springboot mybatis支持包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <!--支持对joda的支持--> <dependency> <groupId>io.eliez</groupId> <artifactId>joda-time-mybatis</artifactId> <version>2.0.2</version> </dependency>
vacationMapper类的写法:
@Mapper public interface VacationMapper { void addVactionApplyInfo(VacationApply vacationApply); }
VacationApply写法
import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.joda.time.DateTime; import java.util.Date; @Getter @Setter @ToString public class VacationApply { //请假开始时间、结束时间、请假时长、申请人、对应实例ID、请假类型、 private int vaID; private DateTime bTime; private DateTime eTime; private double len;//请假时长,转换成分钟数 private String applicant;//请假申请人 private String instanceID;//实例ID private String type;//请假类型 private String desc;//请假原因 }
vacationMapper.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.imooc.activity_workflow.mapper.VacationMapper"> <insert id="addVactionApplyInfo" parameterType="com.imooc.activity_workflow.model.VacationApply"> insert into vacationapply(`bTime`,`eTime`,`len`,`applicant`,`instanceID`,`type`,`desc`) VALUES (#{bTime,typeHandler=org.joda.time.mybatis.handlers.DateTimeTypeHandler}, #{eTime,typeHandler=org.joda.time.mybatis.handlers.DateTimeTypeHandler}, #{len},#{applicant},#{instanceID},#{type},#{desc}) </insert> </mapper>
这里注意joda格式的字段需要指定类型转换就行了。
打印的结果
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@4942e6af] will be managed by Spring
==> Preparing: insert into vacationapply(`bTime`,`eTime`,`len`,`applicant`,`instanceID`,`type`,`desc`) VALUES (?, ?, ?,?,?,?,?)
==> Parameters: 2022-11-24 06:00:00.0(Timestamp), 2022-11-24 10:00:00.0(Timestamp), 0.0(Double), bajie(String), null, 事假(String), 有事(String)
<== Updates: 1