欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于freemarker自动生成mapper多条件查询语句

程序员文章站 2024-03-02 00:02:04
...

1,创建mapper.xml 模板文件

<?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">
<!--${tableconment}mapper -->
<mapper namespace="${tablename}mapper">

    <!--查询${tableconment}列表 -->
    <select id="selectlist" parameterType="java.util.HashMap"
            resultType="java.util.HashMap">
        select
        <#list tableColumnList as tablecolumn>
            <#if  tablecolumn_index= tableColumnList?size-1>
                <!--  ${tablecolumn.columncomment}   -->
                a.${tablecolumn.columnname?lower_case}
            <#else >
                <!--  ${tablecolumn.columncomment}   -->
                a.${tablecolumn.columnname?lower_case},
            </#if>
        </#list>
        from
        ${tableschema}.${tablename} as a
        <#if veanddepquanxian?? && veanddepquanxian=true>
            left join vehicle v on a.vehicleId =v.vehicleId and v.deleted=false
        </#if>

        <#if depquanxian??  &&depquanxian=true>
            left join department d on a.depId =d.depId and d.deleted=false
        </#if>
        where 1=1
        <#list tableColumnList as tablecolumn>
            <#if tablecolumn.datatype=="int" && tablecolumn.columnname?lower_case !="deleted">
                <if test="${tablecolumn.columnname?lower_case} != null ">
                    and a.${tablecolumn.columnname?lower_case}=${r"#"}{${tablecolumn.columnname?lower_case}}
                </if>
            </#if>
            <#if  tablecolumn.columnname?lower_case =="deleted">
                and a.deleted=false
            </#if>
            <#if tablecolumn.datatype=="varchar">
                <if test="${tablecolumn.columnname?lower_case} != null ">
                    and a.${tablecolumn.columnname?lower_case}=${r"#"}{${tablecolumn.columnname?lower_case}}
                    <!-- and a.${tablecolumn.columnname?lower_case} like '%${r"$"}{${tablecolumn.columnname?lower_case}}%'-->
                </if>
            </#if>
            <#if tablecolumn.datatype=="datetime">
                <if test="start${tablecolumn.columnname?lower_case} != null ">
                    and a.${tablecolumn.columnname?lower_case} >= ${r"#"}{start${tablecolumn.columnname?lower_case} }
                </if>
                <if test="end${tablecolumn.columnname?lower_case} != null ">
                    and <![CDATA[ a.${tablecolumn.columnname?lower_case} < ${r"#"}{end${tablecolumn.columnname?lower_case} }]]>
                </if>
            </#if>
        </#list>

      
        <#if ordercol??>
            order by a.${ordercol} desc
        </#if>

    </select>


</mapper>

2,查询数据库获取对应表的字段,主键,类型,注释等

  String sql = "select table_schema tableschema,table_name tablename,\n" +
                "column_name columnname,column_key columnkey,data_type datatype,column_comment columncomment  from\n" +
                "information_schema.COLUMNS where table_schema =? and table_name=? ";
@Data
public class TableColumn {

    private String tableschema;
    private String tablename;
    private String columnname;//字段名
    private String columnkey; //主键,外键等
    private String datatype; //字段类型
    private String columncomment;//字段注释

}
//获取表注释
    public TableNameVo searchTableName(String table_schema, String table_name) {
        String sql = " select table_name tablename,table_comment tableconment from INFORMATION_SCHEMA.TABLES  where table_schema = ?\n" +
                "AND table_name =?";
        TableNameVo tableNameVo = mysqlDao.queryFirst(sql, TableNameVo.class, table_schema, table_name);
        return tableNameVo;
    }
@Data
public class TableNameVo {
private String tablename;
private String tableconment;
}

3,查询出来表的内容之后

  //模板内容的类
  
public class TableVo {

    private String pricolname;//主键名称
    private String tableschema;//所在数据库
    private String tablename;//表名
    private String packpath;//class所在package位置
    private String classname;//自定义的class名称

    private PackageVo packageVo;//各个类所在的package位置
    private String entityName;//大写的类名
    private String tableconment;//表注释

    private String ordercol;//排序字段

    private List<TableColumn> tableColumnList;//字段内容




上面是用来写入ftl的数据,可以*想象加入更多数据

4,根据模板文件生成对应文件,这里可以是html也可以是任意文件

 /**
     * //写入模板
     * @param ftlpath 模板路径
     * @param classpath 要写入的文件路径
     * @param tableVo 查出来的表的内容,字段名,表名注释等
     */
    private void writetem(String ftlpath, String classpath, TableVo tableVo) {
        Writer out = null;
        // step4 加载模版文件
        try {
            Template template = configuration.getTemplate(ftlpath);
            // step5 生成数据
            File docFile = new File(classpath);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(tableVo, out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }
相关标签: java java