ebatis
Ebatis is a relational database data access framework For Node.js.
The biggest feature of ebatis is to support dynamic SQL, transaction control, simple configuration and easy to handle.
Note:
-
ebatis requires Node.js to support es6/7 syntax, and if the version is too low, it is recommended to upgrade the node version;
-
currently only support the Mysql database.
table of contents
Hello World
Configuration
Api
Api Ebatis
-Api SqlChain
-Api SqlChainFactory
-Api Mapper
-Dynamic Sql
Example
How to use
Install module:
npm install ebatis # or npm install -g ebatis
Use it:
let Ebatis = ;let ebatis = ;# set root pathebatis;# load configuration infoebatis;
Simple Configuration:
config by js
exports = moduleexports = datasource : _name : 'default' // datasource name, default 'default' _type : 'mysql' // datasource type, requires _mode : 'pool' // datasource mode, [connection | pool], requires host : '127.0.0.1' // other parameter refence mysql configuration user : 'root' password : '**********' database : 'test' sql : mapper : `./*.xml` // dynamic sql xml sqlchain : transaction : true // open transaction timeout : -1 // timeout time, default 30000ms, if timeout > 0, Invalid timeout action. ;
config by json
config by yaml
datasource : _name : default _type : mysql _mode : pool host : 127.0.0.1, user : root, password : ********** database : test sql : mapper : ./*.xml sqlchain : transaction : true, timeout : -1
Example
1. 编写Ebatis配置文件
ebatis_config.yml
datasource : # 数据源配置信息 _name : default # 数据源名称 _type : mysql # 数据源类型 (暂时只支持mysql) _mode : pool # 数据源模式 connection | pool | cluster, default connection host : 127.0.0.1 # 数据源ip地址 user : root password : ********** database : test connectionLimit : 50 sql : # sql配置信息 mapper : ./sql/*.xml # mapper文件位置,已rootpath为基准 # or mapper : - ./sql/*.xml - ./sql/*.xml sqlchain : # sqlchain配置信息 transaction : true # 开启事务 timeout : 3000 # 设置超时时间,到期后没有执行完毕,自动回滚 printsql : false # 是否打印执行sql的信息
2. 编写Mapper文件
sql/user.xml
t_user * <!-- get all user --> SELECT FROM SELECT FROM id <= ${ user.id } <!-- insert user --> INSERT INTO t_user #{ item } ${ item } <!-- get users by type--> SELECT * FROM t_user type = 1 type = 2 type = 3 type = 0 <!-- add users --> INSERT INTO t_user #{ item } VALUES ${ item } <!-- update user info --> update name = ${ user.name } age = ${ user.age } id = ${ user.id } name = ${ user.name } <!-- delete user --> DELETE FROM WHERE id = ${ id }
2.1 生成Mapper对应的js接口文件
在程序启动后会在xml所在的目录生成对应的js接口文件,直接引入js接口文件,即可调用动态sql。
如下:
/** * Ebatis generator mapper, it's not editable. */let userMapper = /** * @returns */ { return ; } /** * @returns */ { return ; } /** * @returns */ { return ; } /** * @returns */ { return ; } /** * @returns */ { return ; } /** * @returns */ { return ; } /** * @returns */ { return ; }; exports = moduleexports = userMapper;
3. 编写逻辑代码
Callback式逻辑代码
var Ebatis = ;var SqlChainFactory = EbatisSqlChainFactory; var ebatis = ; // set root pathebatis; //ebatis.loadConfig(ebatis_config);ebatis; // 开启开发模式,自动生成Mapper的js接口文件ebatis; process; // 当ebatis完成时,调用ebatis;
[async/await/Promise式逻辑代码
var Ebatis = ;var SqlChainFactory = EbatisSqlChainFactory; var ebatis = ; // set root pathebatis; //ebatis.loadConfig(ebatis_config);ebatis; // 开启开发模式,自动生成Mapper的js接口文件ebatis; process; { // 当ebatis完成时,调用 await ebatisfinish; // 导入Mapper的js接口文件 var UserMapper = ; console; console; var sqlChain = SqlChainFactory; // 获取user命名空间下getUsers的Mapper var getUsers = sqlChain; // or var getUsers = ebatis; console; // 单独执行的动态sql没有事务 getUsersparam; await sqlChainexec; var users = await sqlChainexec; console; console; /*(async function(){ try { let scope = ebatis.getScope(); let getUsers = ebatis.getMapper('user.getUsers'); let a = await getUsers.param.promise({id : 100}); console.log(a); }catch(e){ console.log(e); } })();*/ };