### 什么是“知启蒙数据库映射”? ---------------------------------------       “知启蒙数据库映射(ZhiqimORM)”是Zhiqim Development Kit面向数据库开发的多例服务,包括ZSQL规范和三大映射关系(表映射、字段映射和指令映射),有比MyBatis更简单的动态SQL,和比Hibernate更彻底的对象关系映射:
1、数据库二维表和Java对象之间的注解定义映射,表和视图分别对应Java的_Table、_View;
2、数据库表列和Java字段的数据格式映射,支持6种数据库的9种数据格式;
3、数据库SQL语句和Java指令映射,基于ZTable、ZView的创建、清空、存在、分页、列表、单条、数目、求和、插入、更新、替换、删除等指令。

### 知启蒙数据库映射有哪些优点? --------------------------------------- 1、ZhiqimORM仅依赖JDK1.7+和ZhiqimKernel。15年的坚持,值得信赖。
2、ZhiqimORM是基于ZhiqimKernel设计的多例服务,支持配置多个数据库,且自带JDBC连接池。
3、ZhiqimORM的ZSQL语句可以通过XML配置,和Java代码分离,更易查看和检查,支持不同后缀适配不同的数据库;
4、ZhiqimORM的ZSQL/ZDBO规范通过(?、##、$$)三个通配符建立SQL语句和Java对象的映射关联,简洁清晰。
5、ZhiqimORM提供了标准的ZTable规则,对应数据库表设计,大量的基于表的增、删、改、查、列表、分页、统计无需编写SQL语句;
6、ZhiqimORM提供了标签的ZTView规则,对应多表关联设计,大量基于视图的查询、列表、分页和统计无需编写SQL语句。

### 知启蒙数据库映射目前支持的6种数据库 ---------------------------------------
编号 数据库名称 常用的驱动 连接URL
1 mysql com.mysql.jdbc.Driver jdbc:mysql://127.0.0.1:3306/zhiqim?useUnicode=true&characterEncoding=UTF-8
2 oracle oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@127.0.0.1:1521:zhiqim
3 mssql com.microsoft.jdbc.sqlserver.SQLServerDriver jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=zhiqim
4 postpresql org.postgresql.Driver jdbc:postgresql://127.0.0.1:5432/zhiqim
5 sqlite org.sqlite.JDBC jdbc:sqlite:./db/zhiqim.db
6 hsql org.hsqldb.jdbc.JDBCDriver jdbc:hsqldb:./db/zhiqim

### 知启蒙数据库映射支持的9种数据格式映射 ---------------------------------------
编号 名称 JAVA类型 MySQL Oracle MSSQL PostpreSQL SQLite HSQL
基本类型5种
1 boolean boolean tinyint(1) NUMBER(1) bit bool integer boolean
2 byte int tinyint(4) NUMBER(4) tinyint int2 integer tinyint
3 short int smallint(6) NUMBER(6) smallint int2 integer smalli
nt
4 int int int NUMBER(11) int int4 integer integer
5 long long bigint NUMBER(20) bigint int8 integer bigint
对象类型2种
6 string java.lang.
String
varchar
char
text
mediumtext
longtext
VARCHAR2
CHAR
CLOB
varchar
char
text
varchar
char
text
varchar
char
text
varchar
char
clob
7 datetime java.sql.
Timestamp
datetime DATE datetime timestamp datetime timestamp
特殊类型2种
8 decimal double decimal(m,n) NUMBER(m,n) decimal(m,n) decimal(m,n) numeric(m,n) decimal
(m,n)
9 binary byte[] mediumblob BLOB image bytea blob blob

### 知启蒙数据库映射使用举例                                                                                       下载留言本示例 ``` /** * 首页 * * @version v1.0.0 @author zouzhigang 2016-9-1 新建与整理 */ public class IndexAction implements Action { @Override public void execute(HttpRequest request) throws Exception { int page = request.getParameterInt("page", 1); int pageSize = request.getContextAttributeInt("page.size", 10); Selector selector = new Selector(); selector.addOrderbyDesc("messageTime"); PageResult result = Global.get(ZTable.class).page(Message.class, page, pageSize, selector); request.setAttribute("result", result); } } ```
### 知启蒙数据库映射指令集 ``` /********************************************************************************************/ //exist 表是否存在 /********************************************************************************************/ /** * 是否存在[表类]对应的[实际表] * * @param clazz 表类 * @return =true表示存在,=false表示不存在 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public boolean exist(Class clazz) throws ORMException, SQLException; /** * 是否存在[表类]对应的[实际表]月表,支持表名中替换字段指定为$MONTH$,如LOG_TRACE$MONTH$ * * @param clazz 表类 * @param month 月份 * @return =true表示存在,=false表示不存在 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public boolean existMonth(Class clazz, String month) throws ORMException, SQLException; /** * 是否存在[表类]对应的[实际表],支持表名中有一个替换字段,如LOG_TRACE$ID$ * * @param clazz 表类 * @param replaceKey 替换键 * @param replaceValue 替换值 * @return =true表示存在,=false表示不存在 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public boolean exist(Class clazz, String replaceKey, String replaceValue) throws ORMException, SQLException; /** * 是否存在[表类]对应的[实际表]可替换表,支持表名中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param clazz 表类 * @param replaceMap 可替换字段表 * @return =true表示存在,=false表示不存在 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public boolean exist(Class clazz, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //create 创建表 /********************************************************************************************/ /** * 创建[表类]对应的[实际表] * * @param clazz 表类 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void create(Class clazz) throws ORMException, SQLException; /** * 创建[表类]对应的[实际表]月表 * * @param clazz 表类 * @param month 月份 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void createMonth(Class clazz, String month) throws ORMException, SQLException; /** * 创建[表类]对应的[实际表]可替换表,支持表名中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param clazz 表类 * @param replaceMap 可替换字段表 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void create(Class clazz, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //truncate 清空表 /********************************************************************************************/ /** * 清空[表类]对应的[实际表] * * @param clazz 表类 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void truncate(Class clazz) throws ORMException, SQLException; /** * 清空[表类]对应的[实际表],支持表名中替换字段指定为$MONTH$,如LOG_TRACE$MONTH$ * * @param clazz 表类 * @param month 分月表时使用 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void truncateMonth(Class clazz, String month) throws ORMException, SQLException; /** * 清空[表类]对应的[实际表],支持表名中有一个替换字段,如LOG_TRACE$ID$ * * @param clazz 表类 * @param replaceKey 替换键 * @param replaceValue 替换值 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void truncate(Class clazz, String replaceKey, String replaceValue) throws ORMException, SQLException; /** * 清空[表类]对应的[实际表],支持表名中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param clazz 表类 * @param replaceMap 替换表 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public void truncate(Class clazz, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //insert 插入数据 /********************************************************************************************/ /** * 增加数据,传入标准[表类]对象 * * @param data 表对象 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int insert(Object data) throws ORMException, SQLException; /** * 增加数据,支持表名中替换字段指定为$MONTH$,如LOG_TRACE$MONTH$ * * @param data 表类 * @param month 月份 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int insertMonth(Object data, String month) throws ORMException, SQLException; /** * 增加数据,支持表或字段中有一个替换字段,如LOG_TRACE$ID$ * * @param data 表对象 * @param replaceKey 替换键 * @param replaceValue 替换值 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int insert(Object data, String replaceKey, String replaceValue) throws ORMException, SQLException; /** * 增加数据,支持表或字段中中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param data 表对象 * @param replaceMap 适配表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int insert(Object data, MapSS replaceMap) throws ORMException, SQLException; /** * 替换数据,传入标准[表类]对象和更新器(取fieldMap和replaceMap)当存在时指定修改的值 * * @param data 表对象 * @param updater 更新器 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int insertOrUpdate(Object data, Updater updater) throws ORMException, SQLException; /** * 批量增加数据 * * @param dataList 表对象列表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int[] insertBatch(List dataList) throws ORMException, SQLException; /** * 批量增加数据,支持表或字段中中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param dataList 表对象列表 * @param replaceMap 适配表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int[] insertBatch(List dataList, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //replace 替换数据,MYSQL支持,如果有数据先删除后增加 /********************************************************************************************/ /** * 替换数据,传入标准[表类]对象 * * @param data 表对象 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int replace(Object data) throws ORMException, SQLException; /** * 替换数据,支持表名中替换字段指定为$MONTH$,如LOG_TRACE$MONTH$ * * @param data 表对象 * @param month 月份 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int replaceMonth(Object data, String month) throws ORMException, SQLException; /** * 替换数据,支持表或字段中有一个替换字段,如LOG_TRACE$ID$ * * @param data 表对象 * @param replaceKey 替换键 * @param replaceValue 替换值 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int replace(Object data, String replaceKey, String replaceValue) throws ORMException, SQLException; /** * 替换数据,支持表或字段中中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param data 表对象 * @param replaceMap 适配表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int replace(Object data, MapSS replaceMap) throws ORMException, SQLException; /** * 批量替换数据 * * @param dataList 表对象列表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int[] replaceBatch(List dataList) throws ORMException, SQLException; /** * 批量替换数据,支持表或字段中中有多个替换字段,如LOG_TRACE_$MONTH$_$ID$ * * @param dataList 表对象列表 * @param replaceMap 适配表 * @return int 表示插入的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int[] replaceBatch(List dataList, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //update 更新数据 /********************************************************************************************/ /** * 更新数据,指定更新器需要更新的字段、条件和可替换表 * * @param clazz 表类 * @param updater 更新器 * @return int 表示更新的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int update(Class clazz, Updater updater) throws ORMException, SQLException; /** * 更新数据,指定对象,根据主键进行更新,主键值不更新 * * @param data 表对象 * @return int 表示更新的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int update(Object data) throws ORMException, SQLException; /** * 更新数据,指定对象,根据主键进行更新,主键值不更新,支持表名中替换字段指定为$MONTH$,如LOG_TRACE$MONTH$ * * @param data 表对象 * @param month 月份 * @return int 表示更新的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int updateMonth(Object data, String month) throws ORMException, SQLException; /** * 更新数据,指定对象,根据主键进行更新,主键值不更新,支持表或字段中有一个替换字段,如LOG_TRACE$ID$ * * @param data 表对象 * @param replaceKey 替换键 * @param replaceValue 替换值 * @return int 表示更新的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int update(Object data, String replaceKey, String replaceValue) throws ORMException, SQLException; /** * 更新数据,指定对象,根据主键进行更新,主键值不更新,支持可替换表 * * @param data 表对象 * @param replaceMap 适配表 * @return int 表示更新的条数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int update(Object data, MapSS replaceMap) throws ORMException, SQLException; /********************************************************************************************/ //delete 删除数据 /********************************************************************************************/ /** * 删除数据,多个主键时使用 * * @param clazz 表类 * @param ids 关键属性为数组,多个主键 * @return 返回删除数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int delete(Class clazz, Object... ids) throws ORMException, SQLException; /** * 删除数据, 根据条件 * * @param clazz 表类 * @param selector 对象选择器 * @return 返回删除数 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int delete(Class clazz, Selector selector) throws ORMException, SQLException; /********************************************************************************************/ //count 查询数目 /********************************************************************************************/ /** * 查询数目,多个主键时使用 * * @param clazz 表类 * @param ids 关键属性值,支持多个 * @return 存在的数目 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int count(Class clazz, Object... ids) throws ORMException, SQLException; /** * 查询数目,整表查询 * * @param clazz 表类 * @return int 数目值 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int count(Class clazz) throws ORMException, SQLException; /** * 查询数目,根据条件、可替换表查询 * * @param clazz 表类 * @param selector 对象查询器 * @return int 数目值 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public int count(Class clazz, Selector selector)throws ORMException, SQLException; /********************************************************************************************/ //sum 计算总和 /********************************************************************************************/ /** * 计算总和 * * @param clazz 表类 * @param field 表字段 * @return 计算总和 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public long sum(Class clazz, String field) throws ORMException, SQLException; /** * 计算总和 * * @param clazz 表类 * @param selector 对象查询器 * @param field 表字段 * @return 计算总和 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public long sum(Class clazz, Selector selector, String field) throws ORMException, SQLException; /** * 计算多个总和 * * @param clazz 表类 * @param selector 对象查询器 * @param fields 多个表字段 * @return 计算多个总和 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public long[] sum(Class clazz, Selector selector, String... fields) throws ORMException, SQLException; /********************************************************************************************/ //item 查询一条数据 /********************************************************************************************/ /** * 查询一个表对象,支持多个主键 * * @param clazz 表类 * @param ids 关键属性值 * @return 返回表对象 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public T item(Class clazz, Object... ids) throws ORMException, SQLException; /** * 查询一个表对象,并指定返回属性,查询第一行 * * @param clazz 表类 * @return 返回表对象 * @throws ORMException 映射异常,如果传入的属性不在配置文件中则异常 * @throws SQLException 数据库异常 */ public T item(Class clazz) throws ORMException, SQLException; /** * 查询一个表对象,并指定返回属性,查询条件和排序条件 * * @param clazz 表类 * @param selector 对象查询器 * @return 返回表对象 * @throws ORMException 映射异常,如果传入的属性不在配置文件中则异常 * @throws SQLException 数据库异常 */ public T item(Class clazz, Selector selector) throws ORMException, SQLException; /********************************************************************************************/ //list 查询列表 /********************************************************************************************/ /** * 查询表对象列表,全表查询 * * @param clazz 表类 * @return 返回表对象列表 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public List list(Class clazz) throws ORMException, SQLException; /** * 查询表对象列表,并指定返回属性,查询条件和排序条件 * * @param clazz 表类 * @param selector 对象查询器 * @return 返回表对象列表 * @throws ORMException 映射异常,如果传入的属性不在配置文件中则异常 * @throws SQLException 数据库异常 */ public List list(Class clazz, Selector selector) throws ORMException, SQLException; /** * 查询表对象列表,查询指定的位置的数据 * * @param clazz 表类 * @param pageNo 页码 * @param pageSize 页数 * @return 返回表对象列表 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public List list(Class clazz, int pageNo, int pageSize) throws ORMException, SQLException; /** * 查询表对象列表,并指定位置的,条件和排序条件 * * @param clazz 表类 * @param pageNo 页码 * @param pageSize 页数 * @param selector 对象查询器 * @return 返回表对象列表 * @throws ORMException 映射异常,如果传入的属性不在配置文件中则异常 * @throws SQLException 数据库异常 */ public List list(Class clazz, int pageNo, int pageSize, Selector selector) throws ORMException, SQLException; /********************************************************************************************/ //page 分页显示 /********************************************************************************************/ /** * 查询表对象分页信息 * * @param clazz 表类 * @param pageNo 页码 * @param pageSize 页数 * @return 分页信息,包括总页数,页码,页数和查询的记录 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public PageResult page(Class clazz, int pageNo, int pageSize) throws ORMException, SQLException; /** * 查询表对象分页信息 * * @param clazz 表类 * @param pageNo 页码 * @param pageSize 页数 * @param selector 对象查询器 * @return 分页信息,包括总页数,页码,页数和查询的记录 * @throws ORMException 映射异常 * @throws SQLException 数据库异常 */ public PageResult page(Class clazz, int pageNo, int pageSize, Selector selector) throws ORMException, SQLException; ```
### 知启蒙数据库映射ZhiqimSQL示例 ``` ```
### 知启蒙技术框架与交流 --------------------------------------- ![知启蒙技术框架架构图](https://images.gitee.com/uploads/images/2018/0907/171951_230c3a33_2134900.png "知启蒙技术框架架构图.png")

QQ群:加入QQ交流群,请点击[【458171582】](https://jq.qq.com/?_wv=1027&k=5DWlB3b)

教程:欲知更多知启蒙数据库映射,[【请戳这里】](https://www.zhiqim.com/gitcan/zhiqim/zhiqim_orm/tutorial/index.htm)