[原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持

摘要:
前言今天在开发练习项目时遇到两个mybatis使用问题第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名第二个问题是Pojo中使用JodaDateTime类型的字段,mybatis并不天然支持DateTime,这个问题想必有众多开发者都遇到过两个问题均得到有效解决,在此进行总结记录Mapper中针对多参数的方法正确声明方式UserMapperinterfac

前言

今天在开发练习项目时遇到两个mybatis使用问题

第一个问题是mapper方法参数问题,在参数大于一个时,mybatis不会自动识别参数命名

第二个问题是Pojo中使用Joda DateTime类型的字段,mybatis并不天然支持DateTime,这个问题想必有众多开发者都遇到过

两个问题均得到有效解决,在此进行总结记录


Mapper中针对多参数的方法正确声明方式

UserMapper interface中包含如下方法定义,用于删除用户权限

// 错误定义,此为演示
void deleteAuthority(String username, Authority authority);

对应UserMapper.xml中内容如下

<delete id="deleteAuthority">
	DELETE FROM authorities WHERE username = #{username} AND authority = #{authority};
</delete>

执行时出错,提示无法找到username与authority参数

修复方式,在方法声明中,参数增加@Param注解,如下

void deleteAuthority(@Param("username") String username, @Param("authority") Authority authority);

支持Joda DateTime

例如 Pojo/Model 类型定义如,ctime表示用户创建时间,使用joda DateTime类型

public class User {
    private String username;
    private String password;
    private DateTime ctime;
    private List<UserAuthority> authorities;

    ... 各种 get set 方法
}

mapper中addUser方法定义如下,对于ctime字段,Mybatis默认不支持DateTime类型,且没有提供对应的TypeHander,因此需自行实现,如下的实现必然报错

错误案例
<insert   parameterType="user">
	INSERT INTO users(username, password, ctime) VALUES(#{username}, #{password}, #{ctime})
</insert>

自定义实现DateTimeTypeHandler

参考此处外国友人的讨论 Mybatis Joda Time Support

参考其源码 LukeL99/joda-time-mybatis的实现

以下是是本人对DateTimeTypeHandler的实现,前人基础上稍作重构

public class DateTimeTypeHandler implements TypeHandler<DateTime> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, DateTime dateTime, JdbcType jdbcType)
            throws SQLException {
        if (dateTime != null) {
            preparedStatement.setTimestamp(i, new Timestamp(dateTime.getMillis()));
        } else {
            preparedStatement.setTimestamp(i, null);
        }
    }

    @Override
    public DateTime getResult(ResultSet resultSet, String s) throws SQLException {
        return toDateTime(resultSet.getTimestamp(s));
    }

    @Override
    public DateTime getResult(ResultSet resultSet, int i) throws SQLException {
        return toDateTime(resultSet.getTimestamp(i));
    }

    @Override
    public DateTime getResult(CallableStatement callableStatement, int i) throws SQLException {
        return toDateTime(callableStatement.getTimestamp(i));
    }

    private static DateTime toDateTime(Timestamp timestamp) {
        if (timestamp != null) {
            return new DateTime(timestamp.getTime(), DateTimeZone.UTC);
        } else {
            return null;
        }
    }
}

正确使用方式,在mapper xml中需要指定DateTime类型参数对应的 typeHandler

<insert   parameterType="user">
    INSERT INTO users(username, password, ctime) VALUES(#{username}, #{password},
    #{ctime, typeHandler=DateTimeTypeHandler})
</insert>

这里的DateTimeTypeHandler为使用完全限定名(无命名空间),原因是我已经在配置中加好了alias,方法请参考本人上一篇博客,即在构建sqlSessionFactoryBean时,通过setTypeAliases方法指定使用的类型


免责声明:文章转载自《[原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇27 _ 条件变量sync.Cond (上)SQL对字段出现NULL值的处理下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Python3 读取和写入excel

/py_工作/销售/包括/天气。csv'_工作簿函数读取并返回所有工作表● 以列表形式读取_仅:判断是否读取_仅以模式打开Excel文档● 编码:...

(转)在CentOS中修改中文字符集

本文介绍在linux的shell环境下优化linux中文显示的方法。在CentOS7以前的版本下,默认的字符集的路径一般保存在/etc/sysconfig/i18n文件中。但是在CentOS7版本中,字符集配置文件位于/etc/locale.conf。通过source命令即可使修改生效:[ruby]viewplaincopy#source/etc/local...

JAVA 实现CLOB转String

CLOB定义了用于在数据库中保存文件的类型。SQLCLOB是一种内置类型,它将一个大型字符对象作为列值存储在数据库表的一行中。默认情况下,驱动程序使用SQLlocator实现Clob对象,这意味着Clob对象包含指向SQLCLOB数据的逻辑指针,而不是数据本身。Clob对象在其创建的事务期间有效。在一些数据库系统中,文本也用作CLOB的别名。例如,SQL S...

Wayland 源码解析之代码结构

Wayland实现的代码组成可以分为以下四个部分:1.Wayland库的核心部分,大部分Wayland协议实现都位于该库中。1) 该工具程序分析Wayland协议文件并生成相应的头文件和代码文件。源代码文件列表:wayland/cursor/wayland cursor。通道/光标/通道光标。cwyland/cursor/os兼容性。cwyland/curs...

001_Three.js中的跨域问题

】当请求的资源和请求脚本不在同一域中时,将发生跨域。有关详细信息,请参见链接。这是一个需要进一步考虑的问题。它是一个装载机。它加载本地资源。为什么要跨域请求?...

SQL Server 查看版本信息

SQLServer查看版本信息3种方法:1)使用命令行查看[Win+R]键-˃打开cmd2)使用SSMS查看打开并连接SSMS后查看3)通过服务器属性查看使用SSMS打开并连接指定数据库后,查看服务器属性...