手册

目录

存储器(Repository)

为了能够更方便的维护和执行SQL语句,JDBC模块提供了存储器的支持,可以通过@Repository注解自定义SQL或自定义SQL语句或从配置文件中加载SQL并自动执行。

  • @Repository注解:

    • 参数说明:

      dsName:数据源名称,默认为空则采用默认数据源;

      item:从资源文件中加载item指定的配置项,默认为空;

      value:自定义SQL配置(value的优先级高于item);

      type:操作类型,默认为查询,可选值:Type.OPT.QUERY或Type.OPT.UPDATE

  • 存储器示例代码:

    • 存储器:

      @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 getSQLResults(String hash, IResultSet results) throws Exception {
              return results;
          }
      
          /**
           * 读取配置文件中的SQL
           */
          @Repository(item = "demo_query")
          public List getAttachments(String hash, IResultSet... results) throws Exception {
              final List _returnValues = new ArrayList();
              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("cfgs/default.repo.xml")
      public class DefaultRepoConfig extends DefaultConfiguration {
      }
    • SQL配置文件default.repo.xml内容:

      
      
          
              
                  
              
          
      
    • 在控制器中调用:在浏览器中访问http://localhost:8080/hello查看执行结果

      @Controller
      @RequestMapping("/hello")
      public class HelloController {
      
          /**
           * 注入存储器
           */
          @Inject
          private DemoRepository _repo;
      
          @RequestMapping("/")
          public IView hello() throws Exception {
              // 调用存储器方法
              return View.jsonView(_repo.getAttachments("44f5b005c7a94a0d42f53946f16b6bb2"));
          }
      }
  • 说明:

    • 存储器类通过声明@Repository注解被框架自动扫描并加载;
    • 与其它被容器管理的@Bean一样支持拦截器、事务、缓存等注解;
    • 存储器类方法的参数至少有一个参数(方法有多个参数时,采用最后一个参数)用于接收SQL执行结果;
    • 查询类型SQL的执行结果数据类型为IResultSet,更新类型SQL的执行结果数据类型为int
    • 用于接收SQL执行结果的方法参数支持变长类型,如:IResultSet resultsIResultSet... results是一样的;

科技资讯

更多

精选课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

61.7万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

39.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

70.9万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

61.6万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

23万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

33.9万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

125万人学习

关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号