springboot动态修改mybatis的sql

@Service
public class TestServiceImpl {
    @Autowired
    org.apache.ibatis.session.SqlSessionFactory sessionFactory;
   
    /**
    *@author lixiao
    *@date 2023/4/27 10:38
    *@Description: 修改单个
    */
    public void test1(){
        try {
            Configuration configuration = sessionFactory.getConfiguration();
            //Mapper的全路径加方法名
            String id = "com.xxx.xx.XxxMapper.test";
            MappedStatement mappedStatement = configuration.getMappedStatement(id, false);
            RawSqlSource sqlSource = (RawSqlSource)mappedStatement.getSqlSource();
            Field sqlSource1 = sqlSource.getClass().getDeclaredField("sqlSource");
            sqlSource1.setAccessible(true);
            Object o = sqlSource1.get(sqlSource);
            Field sql1 = o.getClass().getDeclaredField("sql");
            sql1.setAccessible(true);
            BoundSql boundSql = sqlSource.getBoundSql(mappedStatement);
            String sql = boundSql.getSql();
            String newSql = sql.replace("cn", "en");
            sql1.set(o,newSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    *@author lixiao
    *@date 2023/4/27 10:37
    *@Description: 修改多个
    */
    public void test(){
        Configuration configuration = sessionFactory.getConfiguration();
        Collection<MappedStatement> mappedStatements = configuration.getMappedStatements();
        Field[] declaredFields = this.getClass().getDeclaredFields();
        List<String> strings = new ArrayList<>();
        //这个时筛选要修改哪些sql做准备的,
        for (Field declaredField : declaredFields) {
            strings.add(declaredField.getType().getName());
        }
        //循环mappedStatements,修改里面的sql
        for (Object mappedStatement1 : mappedStatements) {
            try {
                MappedStatement mappedStatement = null;

                mappedStatement = (MappedStatement) mappedStatement1;

                if (mappedStatement == null) {
                    continue;
                }
                if (!strings.contains(mappedStatement.getId().substring(0,mappedStatement.getId().lastIndexOf(".")))) {
                    continue;
                }
                RawSqlSource sqlSource = (RawSqlSource)mappedStatement.getSqlSource();
                Field sqlSource1 = sqlSource.getClass().getDeclaredField("sqlSource");
                sqlSource1.setAccessible(true);
                Object o = sqlSource1.get(sqlSource);
                Field sql1 = o.getClass().getDeclaredField("sql");
                sql1.setAccessible(true);
                BoundSql boundSql = sqlSource.getBoundSql(mappedStatement);
                String sql = boundSql.getSql();
                String newSql = sql.replace("cn", "en");
                sql1.set(o,newSql);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以给你一些建议:首先,确保你的SpringBoot项目已经正确配置了MyBatisSQLServer的依赖;其次,使用MyBatis的`SqlSession.getMapper()`方法获取Mapper接口,然后调用Mapper接口中定义的存储过程方法。 ### 回答2: Spring Boot是一个开源的Java开发框架,而MyBatis是一个轻量级的持久化框架,可以与Spring Boot框架无缝集成。要在Spring Boot项目中调用SQL Server存储过程,可以按照以下步骤进行操作: 1. 在Spring Boot项目的pom.xml文件中添加对MyBatisSQL Server驱动的依赖,例如: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>8.2.2.jre8</version> </dependency> ``` 2. 在application.properties或application.yml文件中配置SQL Server的连接信息,例如: ```yaml spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydatabase spring.datasource.username=sa spring.datasource.password=yourpassword spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver ``` 3. 创建一个可以调用存储过程的Mapper接口,例如: ```java @Mapper public interface MyProcedureMapper { @Options(statementType = StatementType.CALLABLE) @Select("{CALL my_procedure(#{param1, mode=IN})}") void callMyProcedure(@Param("param1") String param1); } ``` 4. 在需要调用存储过程的地方注入Mapper接口,并调用对应的方法,例如: ```java @Service public class MyService { @Autowired private MyProcedureMapper myProcedureMapper; public void doSomething() { myProcedureMapper.callMyProcedure("parameter value"); } } ``` 通过以上步骤,就可以在Spring Boot项目中调用SQL Server存储过程了。需要注意的是,根据实际情况修改SQL Server的连接信息和存储过程的调用方式。 ### 回答3: Spring Boot结合MyBatis调用SQL Server存储过程需要进行以下步骤: 1. 首先,在Spring Boot的pom.xml文件中引入MyBatisSQL Server的驱动依赖: ```xml <dependencies> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- SQL Server --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>8.2.2.jre8</version> </dependency> </dependencies> ``` 2. 在Spring Boot的application.properties文件中配置SQL Server数据库连接信息: ```properties spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver ``` 3. 创建XML映射文件,配置调用存储过程的SQL语句。假设存储过程名称为"YourStoredProcedure",并且有一个输入参数"param1"和一个输出参数"param2": ```xml <!-- YourMapper.xml --> <mapper namespace="com.example.mapper.YourMapper"> <select id="callStoredProcedure" statementType="CALLABLE"> {call YourStoredProcedure(#{param1, mode=IN}, #{param2, mode=OUT, jdbcType=VARCHAR})} </select> </mapper> ``` 4. 创建接口YourMapper.java,定义调用存储过程的方法: ```java // YourMapper.java package com.example.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface YourMapper { void callStoredProcedure(@Param("param1") String param1, @Param("param2") String[] param2); } ``` 5. 在Spring Boot的Service类中注入YourMapper,并调用存储过程方法: ```java // YourService.java package com.example.service; import com.example.mapper.YourMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class YourService { private final YourMapper yourMapper; @Autowired public YourService(YourMapper yourMapper) { this.yourMapper = yourMapper; } public void callStoredProcedure(String param1, String[] param2) { yourMapper.callStoredProcedure(param1, param2); } } ``` 以上就是使用Spring Boot结合MyBatis调用SQL Server存储过程的基本步骤。根据实际情况,可以在存储过程中定义更多的输入和输出参数,并在映射文件和接口中进行相应的配置和调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值