mybatis怎么动态生成注解@Select中的sql

mapper接口:前端

package com.zkbc.core.dao.map;

import com.zkbc.core.dao.model.AuditLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * @Description: 用户审计数据处理
 * @Author: yh
 * @CreateDate: 2018/5/3 11:37
 * @UpdateUser: yh.z
 * @UpdateDate: 2018/5/3 11:37
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public interface AuditLogMapper {
    @Insert("insert into audit_log(userId,userName,ip,startTime,endTime,module,function,clazz,method,result,useTime,createTime) values(#{userId},#{userName},#{ip},#{startTime},#{endTime},#{module},#{function},#{clazz},#{method},#{result},#{useTime},now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addAuditLog(AuditLog auditLog);

    @SelectProvider(type = AuditLogProvider.class,method = "getAuditsByCondition")
    List<AuditLog> getAuditsByCondition(@Param("userId") Long userId, @Param("userName") String userName,@Param("startTime") String startTime, @Param("endTime")String endTime,@Param("startRow") Long startRow,@Param("limit") Integer limit);
    @SelectProvider(type = AuditLogProvider.class,method = "getCountByCondition")
    Long getCountByCondition(@Param("userId") Long userId, @Param("userName") String userName, @Param("startTime") String startTime, @Param("endTime") String endTime);
}

 其中add方法就是一个普通的插入sql,这里是固定的,插入的value取自AuditLog对象的属性。java

可是下面的条件查询(包含分页),会根据前端是否提供对应的查询条件来动态的生成sql,使用@Select标签就不能直接知足了,这里使用了@SelectProvider标签,里面有两个属性,一个是sql提供者的类型,另外一个是提供者的方法。sql

SQL提供者apache

AuditLogProvider :app

package com.zkbc.core.dao.map;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @Description: Description
 * @Author: 张颖辉(yh)
 * @CreateDate: 2018/5/7 18:16
 * @UpdateUser: 张颖辉(yh)
 * @UpdateDate: 2018/5/7 18:16
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public class AuditLogProvider {
    /**
     * @Description: 根据条件得到集合,按照时间倒序
     * @Author: 张颖辉(yh)
     * @Date: 2018/5/8 10:56
     * @param:
     * @return:
     * @Version: 1.0
     */
    public String getAuditsByCondition(Map<String, Object> para) {
        String sql = "select * from audit_log " +
                " where 1 = 1 ";
        sql += getCondition(para);
        sql += " order by createTime desc ";
        if (hasPara(para, "startRow") && hasPara(para, "limit")) {
            sql += " LIMIT  " + para.get("startRow") + "," + para.get("limit");
        }
        System.out.println("sql=" + sql);
        return sql;
    }

    /**
     * @Description: 根据条件得到总数
     * @Author: 张颖辉(yh)
     * @Date: 2018/5/8 10:56
     * @param:
     * @return:
     * @Version: 1.0
     */
    public String getCountByCondition(Map<String, Object> para) {
        String sql = "select count(1) from audit_log " +
                " where 1 = 1 ";
        sql += getCondition(para);
        System.out.println("sql=" + sql);
        return sql;
    }

    /**
     * @Description: 条件
     * @Author: 张颖辉(yh)
     * @Date: 2018/5/8 10:55
     * @param: [para]
     * @return: java.lang.String
     * @Version: 1.0
     */
    private String getCondition(Map<String, Object> para) {
        String condition = " ";
        // SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (hasPara(para, "userId")) {
            condition += " and userId=" + para.get("userId");
        }
        if (hasPara(para, "userName")) {
            condition += " and userName like '" + para.get("userName") + "'";
        }
        if (hasPara(para, "startTime")) {
            //condition += " and createTime>='" + sf.format(para.get("startTime"))+"' ";
            condition += " and createTime>='" + para.get("startTime") + "' ";
        }
        if (hasPara(para, "endTime")) {
            //condition += " and createTime<='" +sf.format(para.get("endTime"))+"' " ;
            condition += " and createTime<='" + para.get("endTime") + "' ";
        }
        return condition;
    }

    /**
     * @Description: map包含该key,而且value不为null
     * @Author: 张颖辉(yh)
     * @Date: 2018/5/8 11:23
     * @param: [para, key]
     * @return: boolean
     * @Version: 1.0
     */
    private boolean hasPara(Map<String, Object> para, String key) {
        if (para.containsKey(key) && para.get(key) != null)
            return true;
        return false;
    }
}

上面的代码中能够看出,全部提供者的方法要求是做用域是public,返回的是String字符串类型,这里使用的动态方法(彷佛静态方法也能够),参数是一个Map<String, Object>,根据判断是否包含该条件参数,来动态的拼接Sql并返回,其中Map的key就是Mapper接口中方法的参数前注解@Param("userId")中的值。ide

相关文章
相关标签/搜索