首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

MyBatis Oracle 插入:業務邏輯預處理長度截取,SQL 只負責執行

     編輯:程式設計疑難解答

核心原則

SQL 負責「怎麼存」,Java 負責「存什麼」。資料庫層不應包含複雜業務規則(長度截取、條件轉換),否則導致 SQL 膨脹、難測試、移植性差。

正確分層實作

1. Service/Mapper 接口層預處理

public interface UserMapper {
    @Insert("INSERT INTO t_user(id, code) VALUES(#{id}, #{code})")
    void insert(User user);
}

2. Service 實現類處理業務規則

@Service
public class UserServiceImpl implements UserService {
    @Autowired UserMapper userMapper;

    @Override
    @Transactional
    public void save(UserDTO dto) {
        String raw = dto.getCode(); // 前台傳入字串
        String processed = processCode(raw); // 統一處理邏輯
        User user = new User();
        user.setId(IdGenerator.nextId());
        user.setCode(processed);
        userMapper.insert(user);
    }

    private String processCode(String raw) {
        if (raw == null) return null;
        if (raw.length() > 9) return raw.substring(0, 9); // 截取前 9 位
        if (raw.length() < 9) return null;                // 不足 9 位存空值
        return raw;
    }
}

為何不在 XML 中用 <if> / substring?

缺點說明
SQL 可讀性下降業務邏輯分散在 XML 片段,難以整體審視
單元測試困難需啟動資料庫才能驗證截取邏輯
資料庫耦合Oracle SUBSTR、MySQL SUBSTRING、PG SUBSTRING 語法不一
效能誤區以為資料庫處理快,實則網路傳輸原始長字串更佔頻寬

特例:若必須在 SQL 處理(如歷史遷移、ETL)

<insert id="insertRaw" parameterType="string">
    INSERT INTO t_table(code)
    VALUES (
        <choose>
            <when test="_parameter != null and _parameter.length() > 9">
                #{_parameter.substring(0, 9)}
            </when>
            <when test="_parameter != null and _parameter.length() < 9">
                NULL
            </when>
            <otherwise>
                #{_parameter}
            </otherwise>
        </choose>
    )
</insert>

⚠️ 僅限**無業務邏輯、純資料清洗/遷移**場景,嚴禁用於核心業務流程。

總結

**長度截取、條件轉空值等屬於業務規則,請在 Java Service 層完成**。MyBatis XML 只做參數綁定與簡單動態拼接(如 where、set、foreach),保持 SQL 純粹、可測試、可移植。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved