随笔 mybaits 提取公用的sql内容

1.将公用的内容提取出来,使用sql标签提取,然后使用include引用

 <resultMap type="mptest.mybatistest.entity.GoodVo" id="userMap" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <collection property="orderList" ofType="mptest.mybatistest.entity.Order">
            <result column="orderId" property="orderId" />
            <result column="orderFee" property="orderFee" />
        </collection>
    </resultMap>

    <sql id="field" >
        u.id,
        u.name
    </sql>


    <select id="getGoodsList" resultMap="userMap" >
        select
       <include refid="field"></include> ,
       o.orderId,
       o.orderFee
        FROM
        user_test u left join  order_id_test  o on u.id=o.id
        where u.id = 1
    </select>

2.表在使用的时候可能起别名,有别名的情况:

 <resultMap type="mptest.mybatistest.entity.GoodVo" id="userMap" >
        <result column="id" property="id" />
        <result column="name" property="name" />
        <collection property="orderList" ofType="mptest.mybatistest.entity.Order">
            <result column="orderId" property="orderId" />
            <result column="orderFee" property="orderFee" />
        </collection>
    </resultMap>
    <sql id="field" >
        u.id,
        u.name
    </sql>

    <sql id="field1" >
       ${alias}.orderId,
       ${alias}.orderFee
    </sql>

    <select id="getGoodsList" resultMap="userMap" >
        select
       <include refid="field"></include>,
        <include refid="field1">
            <property name="alias" value="o"/>
        </include>
        FROM
        user_test u left join  order_id_test  o on u.id=o.id
        where u.id = 1
    </select>

    1)sql和include配合使用,sql标签里和其他增删改查标签一样支持动态sql标签

    2)include标签可以通过property子标签给sql标签里传值,sql标签里用 ${} 获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过使用MyBatis的拦截器(Interceptor)来实现动态配置MyBatis执行的SQL。拦截器可以在SQL执行之前、之后或者发生异常时进行拦截和处理。 下面是一个使用拦截器实现动态配置SQL的示例: 1. 创建一个实现了`org.apache.ibatis.plugin.Interceptor`接口的自定义拦截器类,例如`DynamicSqlInterceptor`。 ```java import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.*; import java.util.Properties; @Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) }) public class DynamicSqlInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 在执行SQL之前进行拦截和处理 MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = invocation.getArgs()[1]; // 动态修改SQL语句或参数 // ... // 执行原始的SQL语句 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // 可以读取配置文件中的参数配置 // ... } } ``` 2. 在Spring Boot的配置文件中,配置MyBatis使用该拦截器。 ```properties # application.properties mybatis.configuration.interceptors=com.example.DynamicSqlInterceptor ``` 3. 在Spring Boot应用程序启动类上添加`@MapperScan`注解,指定扫描Mapper接口的包路径。 ```java import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,当执行MyBatisSQL时,会先经过拦截器的处理,你可以在拦截器中根据需要动态修改SQL语句或参数。 需要注意的是,这只是一个示例,具体的拦截逻辑和动态修改SQL的方式需要根据实际需求进行实现。另外,还可以通过自定义其他类型的拦截器来实现更多的功能扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值