SQL模板接入

本篇将要演示SQL模板的使用,目前包含INSERT模板、SELECT模板、UPDATE模板、DELETE模板。

1. 准备工作

为了演示SQL模板接入(参考 JPA接入 中的准备工作),需要如下准备:

  1. MySQL数据库 (客户端可以使用 navicat for mysql)
  2. 新建测试数据库 fleajpatest
  3. 新建测试表 student

2. 使用讲解

2.1 SQL模板配置

SQL模板配置包含了SQL模板规则,SQL模板定义,SQL模板参数,SQL关系配置。具体配置可至GitHub,查看 flea-sql-template.xml

2.2 新增数据

相关配置可查看 :

 <param id="insert" name="SQL模板參數" desc="用于定义SQL模板中的替换参数"><!-- 表名 --><property key="table" value="student" /><!-- 这两个不填,表示表的字段全部使用--><property key="columns" value="stu_name, stu_age, stu_sex, stu_state" /><property key="values" value=":stuName:, :stuAge:, :stuSex:, :stuState:" /></param><relation id="insert" templateId="insert" paramId="insert" name="SQL关系"/>

JPA方式接入SQL模板:

 @Testpublic void testInsertSqlTemplateFromJPA() throws Exception{IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("王老五");student.setStuAge(35);student.setStuSex(1);student.setStuState(1);int ret = studentSV.insert("insert", student);LOGGER.debug("result = {}", ret);}

运行结果:

新增数据:

JDBC方式接入SQL模板:

    @Testpublic void testInsertSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("钱老六");student.setStuAge(30);student.setStuSex(1);student.setStuState(1);int ret = FleaJDBCHelper.insert("insert", student);LOGGER.debug("result = {}", ret);}

运行结果:

新增数据:

2.3 查询数据

相关配置可查看 :

 <param id="select" name="SQL模板參數" desc="用于定义SQL模板中的替换参数; 如需查询全部,则设置key=columns的属性值为 *,即可"><!-- 表名 --><property key="table" value="student" /><!-- SELECT 显示列 --><property key="columns" value="*" /><!-- WHERE 子句 , 出现 xml不能直接识别的需要转义,如 >, < 等--><property key="conditions" value="stu_name LIKE :stuName: AND stu_sex = :stuSex: AND stu_age &gt;= :minAge: AND stu_age &lt;= :maxAge:" /></param><relation id="select" templateId="select" paramId="select" name="SQL关系"/>

JPA方式接入SQL模板:

    @Testpublic void testQuerySqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("%老%");student.setStuSex(1);student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Student List = {}", studentSV.query("select", student));}

运行结果:

JDBC方式接入SQL模板:

 @Testpublic void testQuerySqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("%老%");student.setStuSex(1);student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Student List = {}", FleaJDBCHelper.query("select", student));}

运行结果:

2.4 更新数据

相关配置可查看 :

 <param id="update" name="SQL模板參數" desc="用于定义SQL模板中的替换参数"><!-- 表名 --><property key="table" value="student" /><!-- SET 子句 --><property key="sets" value="stu_name = :stuName, stu_age = :stuAge" /><!-- WHERE 子句 , 出现 xml不能直接识别的需要转义,如 >, < 等--><property key="conditions" value="stu_name LIKE :sName: AND stu_state = :stuState: AND stu_sex = :stuSex: AND stu_age &gt;= :minAge: AND stu_age &lt;= :maxAge:" /></param><relation id="update" templateId="update" paramId="update" name="SQL关系"/>

JPA方式接入SQL模板:

    @Testpublic void testUpdateSqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("王老五1");student.setStuAge(40);student.setStuState(1);student.setStuSex(1);student.put("sName", "%王老五%");student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Result = {}", studentSV.update("update", student));}

运行结果:


JDBC方式接入SQL模板:

    @Testpublic void testUpdateSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("钱老六1");student.setStuAge(35);student.setStuState(1);student.setStuSex(1);student.put("sName", "%钱老六%");student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Result = {}", FleaJDBCHelper.update("update", student));}

运行结果:

2.5 删除数据

相关配置可查看 :

 <param id="delete" name="SQL模板參數" desc="用于定义SQL模板中的替换参数"><!-- 表名 --><property key="table" value="student" /><!-- WHERE 子句 --><property key="conditions" value="stu_name LIKE :stuName: AND stu_state = :stuState: AND stu_sex = :stuSex: AND stu_age &gt;= :minAge: AND stu_age &lt;= :maxAge:" /></param><relation id="delete" templateId="delete" paramId="delete" name="SQL关系"/>

JPA方式接入SQL模板:

    @Testpublic void testDeleteSqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("%王老五%");student.setStuState(1);student.setStuSex(1);student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Result = {}", studentSV.delete("delete", student));}

运行结果:

JDBC方式接入SQL模板:

    @Testpublic void testDeleteSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("%钱老六%");student.setStuState(1);student.setStuSex(1);student.put("minAge", 20);student.put("maxAge", 40);LOGGER.debug("Result = {}", FleaJDBCHelper.delete("delete", student));}

运行结果:

2.6 分页查询

当前数据库数据如下:

相关配置可查看 :

 <param id="select_1" name="SQL模板參數" desc="用于定义SQL模板中的替换参数; 如需查询全部,则设置key=columns的属性值为 *,即可"><!-- 表名 --><property key="table" value="student" /><!-- SELECT 显示列 --><property key="columns" value="*" /><!-- WHERE 子句 , 出现 xml不能直接识别的需要转义,如 >, < 等--><property key="conditions" value="stu_name LIKE :stuName: AND stu_sex = :stuSex: AND stu_age &gt;= :minAge: AND stu_age &lt;= :maxAge: ORDER BY stu_id DESC LIMIT :pageStart:, :pageCount:" /></param><relation id="select_1" templateId="select" paramId="select_1" name="SQL关系"/>

JPA方式接入SQL模板:

 @Testpublic void testQueryPageSqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("%张三%");student.setStuSex(1);student.put("minAge", 18);student.put("maxAge", 20);int pageNum = 1;   // 第一页int pageCount = 5; // 每页5条记录student.put("pageStart", (pageNum - 1) * pageCount);student.put("pageCount", pageCount);List<Student> studentList = studentSV.query("select_1", student);LOGGER.debug("Student List = {}", studentList);LOGGER.debug("Student Count = {}", studentList.size());}

运行结果:

JDBC方式接入SQL模板:

     @Testpublic void testQueryPageSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("%李四%");student.setStuSex(1);student.put("minAge", 18);student.put("maxAge", 20);int pageNum = 1;   // 第一页int pageCount = 5; // 每页5条记录student.put("pageStart", (pageNum - 1) * pageCount);student.put("pageCount", pageCount);List<Map<String, Object>> studentList = FleaJDBCHelper.query("select_1", student);LOGGER.debug("Student List = {}", studentList);LOGGER.debug("Student Count = {}", studentList.size());}

运行结果:

2.7 单个结果查询–计数

相关配置可查看 :

 <param id="select_2" name="SQL模板參數" desc="用于定义SQL模板中的替换参数; 如需查询全部,则设置key=columns的属性值为 *,即可"><!-- 表名 --><property key="table" value="student" /><!-- SELECT 显示列 --><property key="columns" value="count(*)" /><!-- WHERE 子句 , 出现 xml不能直接识别的需要转义,如 >, < 等--><property key="conditions" value="stu_name LIKE :stuName: AND stu_sex = :stuSex: AND stu_age &gt;= :minAge: AND stu_age &lt;= :maxAge:" /></param><relation id="select_2" templateId="select" paramId="select_2" name="SQL关系"/>

JPA方式接入SQL模板:

    @Testpublic void testQueryCountSqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");Student student = new Student();student.setStuName("%张三%");student.setStuSex(1);student.put("minAge", 18);student.put("maxAge", 20);LOGGER.debug("Student Count = {}", studentSV.querySingle("select_2", student));}

运行结果:

JDBC方式接入SQL模板:

    @Testpublic void testQueryCountSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");Student student = new Student();student.setStuName("%李四%");student.setStuSex(1);student.put("minAge", 18);student.put("maxAge", 20);LOGGER.debug("Student Count = {}", FleaJDBCHelper.querySingle("select_2", student));}

运行结果:

2.8 单个结果查询–总和

相关配置可查看 :

 <param id="select_3" name="SQL模板參數" desc="用于定义SQL模板中的替换参数; 如需查询全部,则设置key=columns的属性值为 *,即可"><!-- 表名 --><property key="table" value="student" /><!-- SELECT 显示列 --><property key="columns" value="sum(stu_age)" /><!-- WHERE 子句 , 出现 xml不能直接识别的需要转义,如 >, < 等--><property key="conditions" value="1=1" /></param><relation id="select_3" templateId="select" paramId="select_3" name="SQL关系"/>

JPA方式接入SQL模板:

 @Testpublic void testQuerySumSqlTemplateFromJPA() throws Exception {IStudentSV studentSV = (IStudentSV) applicationContext.getBean("studentSV");LOGGER.debug("Student Age = {}", studentSV.querySingle("select_3", new Student()));}

运行结果:

JDBC方式接入SQL模板:

    @Testpublic void testQuerySumSqlTemplateFromJDBC() throws Exception {FleaJDBCConfig.init(DBSystemEnum.MySQL.getName(), "fleajpatest");LOGGER.debug("Student Age = {}", FleaJDBCHelper.querySingle("select_3", new Student()));}

运行结果:

上述单个结果查询,展示了count和sum,其他avg,max,min等相关内容可以移步 GitHub, 查看 StudentSqlTemplateTest

flea-db使用之SQL模板接入相关推荐

  1. cmstop模板标签通过db标签的sql语句调用文章列表摘要内容

    通过cmstop的content标签无法在列表页面直接调用摘要,需要用到一个专门摘要函数{description($r[contentid])},如果不想用这个函数的话也可通过以下db语句查询arti ...

  2. ssis sql_SSIS OLE DB来源:SQL命令与表或视图

    ssis sql 介绍 (Introduction) SQL Server Integration Services provides a wide variety of features that ...

  3. 使用SQL DTS功能实现从DB/2向SQL Server传输数据

    前言] 经过自行测试SQL2000 DTS功能,感觉通过DTS工具从DB/2取数来得很方便.容易实现.现将实施步骤贴出来,以供参考. [环境] IBM AS/400e.OS/400.DB/2. ERP ...

  4. Mongo DB教程及SQL与Mongo DB查询的映射

    目录 介绍 在机器上设置Mongo DB 启动Mongo DB 下载RoboMongo MongoDB术语 MongoDB的要点 查询时间到了 MongoDB函数 MongoDB中的自动递增ID(SQ ...

  5. Microsoft OLE DB Provider for SQL Server 错误 ‘80004005‘错误原因和解决方案

    Microsoft OLE DB Provider for SQL Server 错误 '80004005'错误原因和解决方案 方法步骤: 1.在WINSOWS/TEMP目录的安全选项中,添加帐号ev ...

  6. cann't connect to db! mysql!,解决SQL Error: Can't connect to MySQL server on错误

    解决SQL Error: Can't connect to MySQL server on错误 文章来源:传奇帮手游 发布时间:2020-12-02 文章性质:原创文章 今天帮主在群里看到有一个兄弟在 ...

  7. db2分页sql_停止尝试使用内部DB框架模拟SQL OFFSET分页!

    db2分页sql 我敢肯定,到目前为止,您已经以多种方式弄错了. 而且您可能很快将无法正确处理. 那么,当您可以实施业务逻辑时,为什么还要在SQL调整上浪费您的宝贵时间呢? 让我解释- 直到最近的SQ ...

  8. 停止尝试使用内部DB框架模拟SQL OFFSET分页!

    我敢肯定,到目前为止,您已经以多种方式弄错了. 而且您可能很快将无法正确处理. 那么,当您可以实施业务逻辑时,为什么还要在SQL调整上浪费您的宝贵时间呢? 让我解释- 直到最近的SQL:2008标准 ...

  9. [DB]mysql 及sql server2005下实现分页效果的sql语句

    简要做一下总结:       为实现类似top的功能,我们在SQL Server中和MySQL中使用到的SQL语句是不同的.       1.在SQL Server中,我们使用  select top ...

  10. mybatis generator修改默认生成的sql模板

    相关连接: mybatis-generator扩展教程系列 -- 自定义sql xml文件 git项目地址 转载于:https://www.cnblogs.com/hujunzheng/p/71105 ...

最新文章

  1. windows平台下杀死指定端口的进程(转载)
  2. 最小生成树、拓扑排序、单源最短路径
  3. MySQL 高级 游标介绍
  4. linux 服务器之查看磁盘使用情况
  5. java jstl foreach用法_JSTL 中c:forEach使用
  6. 用Typescript 开发 node.js (方法2)
  7. jenkins配置记录(1)--添加用户权限
  8. AS3多人游戏开发—同步人物移动2
  9. Python- and or 的短路原则
  10. docker 命令_Docker的入门:安装Docker及记住常用的镜像命令和容器命令
  11. Load balancer does not have available server for client:xxx
  12. CAD插件学习系列教程(七) 去除CAD教育版戳记 (两种方法)
  13. 软件项目需求调研报告模板下载_强烈推荐:一个软件,即可搞定所有方案报价工作...
  14. windows上三款能打开超大文本的工具比较:Large Text File Viewer+PilotEdit+LogView
  15. 海康威视错误代码说明(四)(错误代码:47~62)
  16. matlab创建wps服务器,wps云文档服务器架设(mac版wps云文档在哪)
  17. 五篇经典好文,值得一看
  18. 【金猿人物展】龙盈智达首席数据科学家王彦博:量子科技为AI大数据创新发展注入新动能...
  19. 全球及中国汽车自动驾驶用胶粘剂行业市场发展态势与需求前景预测报告2022-2028年
  20. 4:3 背景图片素材

热门文章

  1. 刻在祁连山上的艺术二维码,真的太酷了11
  2. discuz 获取会员头像
  3. PTA 习题3.6 一元多项式的乘法与加法运算
  4. 搭建gos_如何将记录器注入gos http处理程序
  5. @Cacheable(sync=true) only allows a single cache on
  6. 控件库中的Botton控件简述
  7. 和风天气获取天气情况
  8. 关于ASCII码的理解
  9. Julia 终于正式发布了
  10. 新赛季的中超和国安,荆棘中前行