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






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM