PostgreSQL数据类型转换

当前端传过来的数据类型和数据库里的数据类型不一致时,需要在数据插入进行数据类型

使用 CAST() 函数

CAST ( expression AS type )
--- 例如
select cast(233 as numeric);

mapper文件中

<?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.fengyun.medicallab.lab.mapper.EmployMapper">

<insert id="addEmployees" useGeneratedKeys="true" keyProperty="id">
    INSERT
        INTO
        employees (emp_name, emp_age, emp_no, hire_date, sal, deptno, mgr,
        user_name, email, phone_number, sex, emp_password, status, remark)
    VALUES(#{empName},#{empAge},#{empNo},#{hireDate}, CAST(#{sal} as money),#{deptno},#{mgr},
    #{userName},#{email},#{phoneNumber},#{sex},#{empPassword},#{status},#{remark})
</insert>

</mapper>

还可以使用 CREATE CAST语句自定义转换,详见:

http://postgres.cn/docs/10/sql-createcast.html

参考资料:
https://blog.csdn.net/lewky_liu/article/details/85085257?utm_medium=distribute.pc_relevant.none-task-blog-title-5&spm=1001.2101.3001.4242



来自为知笔记(Wiz)


posted on 2020-10-18 23:38  白衣风云  阅读(5768)  评论(0编辑  收藏  举报

导航