Mybatis迷你版--QueryObjectFactory

简介:   今天在看JDBC4.2新规范,然后无意之间就碰到了这个东西QueryObjectFactory, 市面上orm框架有很多,在这里我就不一一列举了。那么今天我来记录一下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);


}
View Code

这里面我们使用注解的方式配置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);
    }
}
View Code

执行成功后,拿到如下结果:

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());
View Code

  注意我们通过以下几种方式设置数据类型:

  {数据类型%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);
    }
  }
}
View Code

  在静态代码块中所有的日期类型都对应的是java.util.Date ,因此我们不能用Timestamp或者java.sql.Date作为JavaBean的数据类型

二、总结与展望

1. 使用注解通常情况下适配一些比较简单的查询,复杂的查询不建议使用

2.这个适合于小型项目的迅速开发与上线,大型项目建议选择成熟的orm框架

3.虽然说select * 不建议使用的,但是如何解决select * 下映射关系?

 

目录
相关文章
|
7月前
|
存储 缓存 Java
干翻Mybatis源码系列之第八篇:Mybatis二级缓存的创建和存储
干翻Mybatis源码系列之第八篇:Mybatis二级缓存的创建和存储
|
7月前
|
SQL Java 数据库连接
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
162 0
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
|
SQL 缓存 Java
|
4月前
|
XML Java 数据库连接
mybatis源码研究、搭建mybatis源码运行的环境
这篇文章详细介绍了如何搭建MyBatis源码运行的环境,包括创建Maven项目、导入源码、添加代码、Debug运行研究源码,并提供了解决常见问题的方法和链接到搭建好的环境。
mybatis源码研究、搭建mybatis源码运行的环境
|
7月前
|
缓存 Java 数据库连接
一文彻底搞懂Mybatis系列(十六)之MyBatis集成EhCache
一文彻底搞懂Mybatis系列(十六)之MyBatis集成EhCache
118 0
|
7月前
|
SQL Java 数据库连接
干翻Mybatis源码系列之第十篇:Mybatis Plugins基本概念与拦截概述
干翻Mybatis源码系列之第十篇:Mybatis Plugins基本概念与拦截概述
|
7月前
|
Java 关系型数据库 数据库连接
干翻Mybatis源码系列之第十二篇:基于Mybatis Plugins做一个乐观锁
干翻Mybatis源码系列之第十二篇:基于Mybatis Plugins做一个乐观锁
|
SQL Java 数据库连接
Mybatis-Plus 开发提速器:mybatis-plus-generator-ui 你不了解下
在基于Mybatis的开发模式中,很多开发者还会选择Mybatis-Plus来辅助功能开发,以此提高开发的效率。虽然Mybatis也有代码生成的工具,但Mybatis-Plus由于在Mybatis基础上做了一些调整,因此,常规的生成工具生成的代码还有一些不太符合预期。而且对于多数据库的支持不是很好。
Fluent Mybatis, 原生Mybatis, Mybatis Plus三者功能对比
使用fluent mybatis可以不用写具体的xml文件,通过java api可以构造出比较复杂的业务sql语句,做到代码逻辑和sql逻辑的合一。不再需要在Dao中组装查询或更新操作,在xml或mapper中再组装参数。那对比原生Mybatis, Mybatis Plus或者其他框架,FluentMybatis提供了哪些便利呢?
Fluent Mybatis, 原生Mybatis, Mybatis Plus三者功能对比
|
SQL XML Java
mybatis-plus 简介、安装、配置(1)【just Like】
mybatis-plus 简介、安装、配置(1)【just Like】
248 0
mybatis-plus 简介、安装、配置(1)【just Like】