随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

mybatis执行原生sql

复制代码
package com.lynch.asap.config.mapper;

import org.apache.ibatis.annotations.Param;

public interface SqlMapper {
    void dynamicsInsert(@Param("paramSQL") String sql);
    
    void dynamicsUpdate(@Param("paramSQL") String sql);
    
    void dynamicsDelete(@Param("paramSQL") String sql);
}
复制代码
复制代码
<?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.lynch.asap.config.mapper.SqlMapper">
    <insert id="dynamicsInsert">
        ${paramSQL}
    </insert>
    
    <insert id="dynamicsUpdate">
        ${paramSQL}
    </insert>

    <delete id="dynamicsDelete">
        ${paramSQL}
    </delete>

</mapper>
复制代码
复制代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.yunping.asap.WebApplication;
import com.yunping.asap.config.DataSourceContext;
import com.yunping.asap.config.model.CfSerialClass;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class SqlMapperTest {

    @Autowired
    private SqlMapper sqlMapper;
    
    @Test
    public void dynamicsInsert() {
        DataSourceContext.setRouterKey("asap-test1");
        
        String sql = "INSERT INTO  `cf_constant` (`cc_id`, `cc_code`, `cc_name`, `ccc_id`, `ccc_code`, `ap_stage`, `cc_key`, `cc_value`, `cc_sort`, `begin_time`, `end_time`, `cc_createtime`, `cc_creator`, `update_time`, `modifier`, `so_org`, `so_group`) VALUES ('aaaa', NULL, NULL, '4bf5d5ae2fa6450b916c854c73ad5171', 'FJLX', NULL, 'ht', '合同', NULL, NULL, NULL, '2019-06-06 14:08:22', NULL, '2019-06-06 14:08:23', NULL, '', NULL)";
        sqlMapper.dynamicsInsert(sql);
    }
    
    @Test
    public void dynamicsUpdate() {
        DataSourceContext.setRouterKey("asap-test1");
        
        String sql = "UPDATE `cf_constant` SET `cc_id`='aaaa', `cc_code`=NULL, `cc_name`=NULL, `ccc_id`='4bf5d5ae2fa6450b916c854c73ad5171', `ccc_code`='FJLX', `ap_stage`=NULL, `cc_key`='ht', `cc_value`='合同1111111111111111', `cc_sort`=NULL, `begin_time`=NULL, `end_time`=NULL, `cc_createtime`='2019-06-06 14:08:22', `cc_creator`=NULL, `update_time`='2019-06-06 14:08:23', `modifier`=NULL, `so_org`='', `so_group`=NULL WHERE (`cc_id`='aaaa')";
        sqlMapper.dynamicsUpdate(sql);
    }
    
    @Test
    public void dynamicsDelete() {
        DataSourceContext.setRouterKey("asap-test1");
        
        String sql = "delete from `cf_constant` where cc_id='aaaa'";
        sqlMapper.dynamicsDelete(sql);
    }
}
复制代码

 

posted on   Ruthless  阅读(18774)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2018-08-21 Eclipse搭建SpringBoot之HelloWorld
2018-08-21 spring-boot教程
2017-08-21 nginx反向代理如何获取真实IP?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示