Mybatis迷你版--QueryObjectFactory
今天在看JDBC4.2新规范,然后无意之间就碰到了这个东西QueryObjectFactory, 市面上orm框架有很多,在这里我就不一一列举了。那么今天我来记录一下QueryObjectFactory。官网地址:点这里
一、快速入门
1、将下载好的jar包加入项目
2、我们利用一下jdbc4.0的规范在项目中建立如下文件
在这里我们指定要加载的驱动:com.mysql.jdbc.Driver
3、编写接口:
package com.bdqn.lyrk.java.study; import sf.qof.BaseQuery;
import sf.qof.Insert;
import sf.qof.Query; import java.util.List; public interface StudentDao extends BaseQuery { @Query(sql = "select stuName {%%.stuName},id {%%.id} from student_0")
List<StudentEntity> listStudents(); @Insert(sql = "INSERT INTO student_0 (id,stuName,password) values({%1.id},{%1.stuName},{%1.password})")
void save(List<StudentEntity> studentEntities); }
这里面我们使用注解的方式配置SQL语句。在这里{}表示占位符,{%%.属性} 通常用于设置查询结果对应的对象属性上,{%1} 通常代表方法内的参数
4、Main方法
package com.bdqn.lyrk.java.study; import sf.qof.QueryObjectFactory;
import sf.qof.dialect.HSQLDbDialect; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class Main {
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/MySchool?characterEncoding=utf-8&useSSL=false", "root", "xxx"); StudentDao studentDao = QueryObjectFactory.createQueryObject(StudentDao.class);
QueryObjectFactory.setSQLDialect(new HSQLDbDialect());
studentDao.setConnection(connection);
studentDao.setBatchSize(3);
studentDao.listStudents().forEach(s -> System.out.println(s.getStuName())); List<StudentEntity> studentEntities = new ArrayList<>();
for (int i = 0; i < 5; i++) {
StudentEntity studentEntity = new StudentEntity();
studentEntity.setId(i + 10);
studentEntity.setStuName("test3");
studentEntity.setPassword("abcd");
studentEntities.add(studentEntity);
}
studentDao.save(studentEntities);
}
}
执行成功后,拿到如下结果:
5、 设置参数的方式与坑
在这里面QueryFactory给我们设置了如下几种数据类型
registerResultMapper("string", AbstractCharacterMapping.StringMapping.class, AbstractCharacterMapping.StringMapping.getTypes());
registerResultMapper("char", AbstractCharacterMapping.CharacterMapping.class, AbstractCharacterMapping.CharacterMapping.getTypes());
registerResultMapper("boolean", AbstractNumberMapping.BooleanMapping.class, AbstractNumberMapping.BooleanMapping.getTypes());
registerResultMapper("byte", AbstractNumberMapping.ByteMapping.class, AbstractNumberMapping.ByteMapping.getTypes());
registerResultMapper("short", AbstractNumberMapping.ShortMapping.class, AbstractNumberMapping.ShortMapping.getTypes());
registerResultMapper("int", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes());
registerResultMapper("integer", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes());
registerResultMapper("long", AbstractNumberMapping.LongMapping.class, AbstractNumberMapping.LongMapping.getTypes());
registerResultMapper("float", AbstractNumberMapping.FloatMapping.class, AbstractNumberMapping.FloatMapping.getTypes());
registerResultMapper("double", AbstractNumberMapping.DoubleMapping.class, AbstractNumberMapping.DoubleMapping.getTypes());
registerResultMapper("date", AbstractDateTimeMapping.DateMapping.class, AbstractDateTimeMapping.DateMapping.getTypes());
registerResultMapper("time", AbstractDateTimeMapping.TimeMapping.class, AbstractDateTimeMapping.TimeMapping.getTypes());
registerResultMapper("timestamp", AbstractDateTimeMapping.TimestampMapping.class, AbstractDateTimeMapping.TimestampMapping.getTypes()); registerParameterMapper("string", AbstractCharacterMapping.StringMapping.class, AbstractCharacterMapping.StringMapping.getTypes());
registerParameterMapper("char", AbstractCharacterMapping.CharacterMapping.class, AbstractCharacterMapping.CharacterMapping.getTypes());
registerParameterMapper("boolean", AbstractNumberMapping.BooleanMapping.class, AbstractNumberMapping.BooleanMapping.getTypes());
registerParameterMapper("byte", AbstractNumberMapping.ByteMapping.class, AbstractNumberMapping.ByteMapping.getTypes());
registerParameterMapper("short", AbstractNumberMapping.ShortMapping.class, AbstractNumberMapping.ShortMapping.getTypes());
registerParameterMapper("int", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes());
registerParameterMapper("integer", AbstractNumberMapping.IntegerMapping.class, AbstractNumberMapping.IntegerMapping.getTypes());
registerParameterMapper("long", AbstractNumberMapping.LongMapping.class, AbstractNumberMapping.LongMapping.getTypes());
registerParameterMapper("float", AbstractNumberMapping.FloatMapping.class, AbstractNumberMapping.FloatMapping.getTypes());
registerParameterMapper("double", AbstractNumberMapping.DoubleMapping.class, AbstractNumberMapping.DoubleMapping.getTypes());
registerParameterMapper("date", AbstractDateTimeMapping.DateMapping.class, AbstractDateTimeMapping.DateMapping.getTypes());
registerParameterMapper("time", AbstractDateTimeMapping.TimeMapping.class, AbstractDateTimeMapping.TimeMapping.getTypes());
registerParameterMapper("timestamp", AbstractDateTimeMapping.TimestampMapping.class, AbstractDateTimeMapping.TimestampMapping.getTypes());
注意我们通过以下几种方式设置数据类型:
{数据类型%1} 或者 {数据类型%%.属性的方式}
坑:注意以下关于日期数据类型代码:
/*
* Copyright 2007 brunella ltd
*
* Licensed under the LGPL Version 3 (the "License");
* you may not use this file except in compliance with the License.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package sf.qof.mapping; import java.util.Date;
import java.util.HashSet;
import java.util.Set; public abstract class AbstractDateTimeMapping extends AbstractBaseMapping implements Mapping, ParameterMapping,
ResultMapping { private static final Set<Class<?>> types = new HashSet<Class<?>>();
static {
types.add(Date.class);
} public static Set<Class<?>> getTypes() {
return types;
} public void accept(Mapper mapper, MappingVisitor visitor) {
visitor.visit(mapper, this);
} public abstract void accept(Mapper mapper, DateTimeMappingVisitor visitor); public static class DateMapping extends AbstractDateTimeMapping {
public void accept(Mapper mapper, DateTimeMappingVisitor visitor) {
visitor.visit(mapper, this);
}
} public static class TimeMapping extends AbstractDateTimeMapping {
public void accept(Mapper mapper, DateTimeMappingVisitor visitor) {
visitor.visit(mapper, this);
}
} public static class TimestampMapping extends AbstractDateTimeMapping {
public void accept(Mapper mapper, DateTimeMappingVisitor visitor) {
visitor.visit(mapper, this);
}
}
}
在静态代码块中所有的日期类型都对应的是java.util.Date ,因此我们不能用Timestamp或者java.sql.Date作为JavaBean的数据类型
二、总结与展望
1. 使用注解通常情况下适配一些比较简单的查询,复杂的查询不建议使用
2.这个适合于小型项目的迅速开发与上线,大型项目建议选择成熟的orm框架
3.虽然说select * 不建议使用的,但是如何解决select * 下映射关系?
Mybatis迷你版--QueryObjectFactory的更多相关文章
- ASP.NET Core管道深度剖析(2):创建一个“迷你版”的管道来模拟真实管道请求处理流程
从<ASP.NET Core管道深度剖析(1):采用管道处理HTTP请求>我们知道ASP.NET Core请求处理管道由一个服务器和一组有序的中间件组成,所以从总体设计来讲是非常简单的,但 ...
- 自用迷你版的Deferred
啥也不说贴代码,项目用 /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) ...
- 迷你版jQuery——zepto核心源码分析
前言 zepto号称迷你版jQuery,并且成为移动端dom操作库的首选 事实上zepto很多时候只是借用了jQuery的名气,保持了与其基本一致的API,其内部实现早已面目全非! 艾伦分析了jQue ...
- 迷你版Deferred
直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
- 迷你版 smarty --模板引擎和解析
http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...
- 一个用 C 语言写的迷你版 2048 游戏,仅仅有 500个字符
Jay Chan 用 C 语言写的一个迷你版 2048 游戏,仅仅有 487 个字符. 来围观吧 M[16],X=16,W,k;main(){T(system("stty cbreak&qu ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- (转)金蝶KIS迷你版、标准版在查询数量金额明细账时提示“发生未知错误,系统当前操作被取消,请与金蝶公司联系”
金蝶KIS迷你版.标准版在查询数量金额明细账时提示“发生未知错误,系统当前操作被取消,请与金蝶公司联系” 2013-07-10 12:17:51| 分类: 金蝶专题|举报|字号 订阅 金 ...
随机推荐
- 如何使用ILAsm与ILDasm修改.Net exe(dll)文件
一.背景 最近项目组新上项目,交付的时间比较急迫,原本好的分支管理习惯没有遵守好,于是出现下面状况: 多个小伙伴在不同的分支上开发. 原本QA环境也存在一个阻碍性的bug A 一位同事在QA环境发布了 ...
- 学大伟业 国庆Day2
期望得分:30+100+0=130 实际得分:30+100+20=150 忍者钩爪 (ninja.pas/c/cpp) [问题描述] 小Q是一名酷爱钩爪的忍者,最喜欢飞檐走壁的感觉,有一天小Q发现一个 ...
- HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享
[数据恢复故障描述] 客户服务器属于HP品牌DL380系列,存储是由6块73GB SAS硬盘组成的RAID5,操作系统是WINDOWS 2003 SERVER,主要作为企业部门内部的文件服务器来 ...
- django的FBV和CBV
title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...
- New UWP Community Toolkit - RotatorTile
概述 UWP Community Toolkit 中有一个为图片或磁贴提供轮播效果的控件 - RotatorTile,本篇我们结合代码详细讲解 RotatorTile 的实现. RotatorTi ...
- SpringBoot应用的监控与管理
spring-boot-starter-actuator模块 /health /autoconfig /beans /configprops:应用配置属性信息 /env:环境属性,如:环境变量.jvm ...
- Spring Security入门(1-13)Spring Security的投票机制和投票器
1.三种表决方式,默认是 一票制AffirmativeBased public interface AccessDecisionManager { /** * 通过传递的参数来决定用户是否有访问对应受 ...
- Python之编码
一.Python2与Python3的区别 1.从宏观上考虑,Python2重复代码太多,错误率高,不够规范.Python崇尚的是语言简洁.优美.清晰.Python3更加规范,重复代码少: 2.Pyth ...
- Python入门之三元表达式\列表推导式\生成器表达式\递归匿名函数\内置函数
本章目录: 一.三元表达式.列表推导式.生成器表达式 二.递归调用和二分法 三.匿名函数 四.内置函数 ================================================ ...
- 关于HTML
我的PHP学习之旅 学习PHP已经有一段时间了,今天才想好好的总结一下这一路走来的点点滴滴,也想把我的学习方法及经验分享给大家,希望能对你有所帮助. 首先学习的是PHP前端部分,这里需要学习的知识有H ...