Repository


In order to more conveniently maintain and execute SQL statements, the JDBC module provides memory support, which can be annotated with custom SQL or custom SQL statements or loaded from a configuration file through @Repository SQL and executed automatically.

  • @Repository annotation:

    • Parameter description:

      dsName: Data source name, default is empty Use the default data source;

      item: Load the configuration item specified by item from the resource file, the default is empty;

      value: Custom SQL configuration (value has a higher priority than item);

      type: operation type, default is query, optional value: Type.OPT.QUERY or Type.OPT.UPDATE

  • Storage example code:

    • Storage:

      @Repository
      public class DemoRepository implements IRepository {
      
          /**
           * 注入SQL配置文件(任意配置对象均可)
           */
          @Inject
          private DefaultRepoConfig _repoCfg;
      
          /**
           * 返回SQL配置文件对象, 若不需要配置文件请不要实现IRepository接口
           */
          public IConfiguration getConfig() {
              return _repoCfg;
          }
      
          /**
           * 自定义SQL
           */
          @Repository("select * from ymcms_attachment where hash = ${hash}")
          public IResultSet<Object[]> getSQLResults(String hash, IResultSet<Object[]> results) throws Exception {
              return results;
          }
      
          /**
           * 读取配置文件中的SQL
           */
          @Repository(item = "demo_query")
          public List<Attachment> getAttachments(String hash, IResultSet<Object[]>... results) throws Exception {
              final List<Attachment> _returnValues = new ArrayList<Attachment>();
              if (results != null && results.length > 0) {
                  ResultSetHelper _help = ResultSetHelper.bind(results[0]);
                  if (_help != null)
                      _help.forEach(new ResultSetHelper.ItemHandler() {
                          @Override
                          public boolean handle(ResultSetHelper.ItemWrapper wrapper, int row) throws Exception {
                              _returnValues.add(wrapper.toEntity(new Attachment()));
                              return true;
                          }
                      });
              }
              return _returnValues;
          }
      }
    • SQL configuration file object:

      @Configuration("cfgs/default.repo.xml")
      public class DefaultRepoConfig extends DefaultConfiguration {
      }
    • ##SQL configuration file

      default.repo.xmlContent:

      <?xml version="1.0" encoding="UTF-8"?>
      <properties>
          <category name="default">
              <property name="demo_query">
                  <value><![CDATA[select * from ymcms_attachment where hash = ${hash}]]></value>
              </property>
          </category>
      </properties>
    • Call in the controller: Visit

      http://localhost:8080/hello in the browser to view the execution results

      @Controller
      @RequestMapping("/hello")
      public class HelloController {
      
          /**
           * 注入存储器
           */
          @Inject
          private DemoRepository _repo;
      
          @RequestMapping("/")
          public IView hello() throws Exception {
              // 调用存储器方法
              return View.jsonView(_repo.getAttachments("44f5b005c7a94a0d42f53946f16b6bb2"));
          }
      }
  • ## Description:
  • The storage class is declared by
      @Repository
    • Annotations are automatically scanned and loaded by the framework; supports interceptors, transactions, caches and other annotations like other container-managed
    • @Bean
    • ; storage class methods The parameter has at least one parameter (when the method has multiple parameters, the last parameter is used) used to receive the SQL execution result;
    • The query type SQL execution result data type is
    • IResultSet<Object[]>
    • , the update type SQL execution result data type is int; The method parameters used to receive SQL execution results support variable length types, such as:
    • IResultSet<Object[ ]> results
    • and IResultSet<Object[]>... results are the same;