Giter VIP home page Giter VIP logo

nutz-sqltpl's Introduction

nutz-sqltpl SQL模板实现

配合NutzCodeInsight插件使用更香

源码

支持多种模板引擎

默认采用beetl引擎(注意,这不是与BeetlSql的集成),其他模版引擎可以自己扩展,详情看源码

#用法 然后在pom.xml中

<dependency>
  <groupId>com.github.threefish</groupId>
  <artifactId>nutz-sqltpl</artifactId>
</dependency>

然后在ioc.js中

var ioc = {
   sqlTplIocEventListener: {
           type: "com.github.threefish.nutz.sqltpl.SqlTplIocEventListener",
           args: [{refer: '$ioc'}]
       },
       beetlSqlTemplteEngineImpl: {
           type: "com.github.threefish.nutz.sqltpl.BeetlSqlTemplteEngineImpl",
           events: {
               create: "init"
           },
           fields: {
                statementStart : "<exp>",//可修改(如需要使用xmltag的只支持<exp></exp> xml tag,不支持其他xml tag)
                statementEnd :"</exp>"//可修改
            }
       }
    }
}

然后,在MainSetup.init内加入下面的语句, 启用热加载(注意:JAR包中的xml无法进行热加载)

SqlsTplHolder.DEVELOPER_MODE = true;

然后,你需要一个Service文件 实现 ISqlDaoExecuteService 接口

@IocBean(args = {"refer:dao"}, name = "companyService")
@SqlsXml("CompanyService.xml")
public class CompanyServiceImpl extends BaseServiceImpl<Company> implements CompanyService, ISqlDaoExecuteService {
    /**
    * 1、我是必须要有的
    * 2、可以不实现 ISqlDaoExecuteService 接口,用 SqlsTplHolder 直接渲染sql自己再进行操作
    */
    private SqlsTplHolder sqlsTplHolder;

    public CompanyServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public SqlsTplHolder getSqlsTplHolder() {
        return this.sqlsTplHolder;
    }

    @Override
    public Dao getDao() {
        return dao;
    }

    @Override
    public Entity getEntity() {
        return super.getEntity();
    }

    @Override
    public Class getEntityClass() {
        return super.getEntityClass();
    }
    /**
     *  分页查询列表
     * @param param
     * @param pager
     * @return 
     */
    @Override
    public List<NutMap> queryAllBySql(NutMap param, Pager pager) {
        //此处queryAll对应
        return queryMapBySql("queryAll", param);
    }
}

你需要一个XML文件来管理当前service的Sql(请把我和CompanyServiceImpl放在一起,或采用相对路径自己摸索)

当模版语法 if()... for()...等等 取值表达式 ${}

来看一下例子

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE Sqls PUBLIC "nutz-sqltpl"
           "https://threefish.gitee.io/nutz-sqltpl/nutz-sqltpl.dtd">
   <Sqls>
        <!--var是当前文件的共享变量,var中不能有表达式-->
       <var name="tableName">logistics_company</var>
       <!--wrap=true 表示将会清除换行符\n 默认不清除-->
       <sql id="queryAll" wrap="true">
           SELECT * from ${tableName}
           <exp> if(isNotEmpty(name)){ </exp>
           where name like @name
           <exp>}</exp>
       </sql>
   </Sqls>

NutzCodeInsight

nutz-sqltpl's People

Contributors

threefish avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.