# Neo:Orm框架
Neo是一个基于JDBC开发的至简化框架,采用ActiveRecord的思想进行设计,名字源于《黑客帝国》男主角名字,为连接虚拟与现实的救世主,取名Neo寓意为链接数据和逻辑。源头来自于之前接触的一些不错的Orm框架,也有来自于业内的对于至简化编码影响,也有对于CQRS思想的借鉴。
## 使用文档
[Neo文档介绍](https://simons.gitbook.io/neo/)
[最新Neo文档介绍](https://www.yuque.com/simonalong/neo)
## 快速入门
该框架秉承大道至简理念,采用一个Neo对象对应一个DataSource方式,然后这个Neo对象拥有对表的各种操作。
### maven引入
当前已经发布到maven中央仓库,直接使用即可,目前最低版本0.3.0,不同版本的api差距不小,建议使用最新版本。目前版本还未脱离实验阶段,还未到达稳定版,如果有什么问题请及时反馈
```xml
<dependency>
<groupId>com.github.simonalong</groupId>
<artifactId>Neo</artifactId>
<version>${latest.release.version}</version>
</dependency>
```
### 快速入门
一个DB对应的一个对象Neo,操作表,则填入对应的表名即可
```sql
CREATE TABLE `neo_table1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`group` char(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '数据来源组,外键关联lk_config_group',
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT '任务name',
`user_name` varchar(24) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '修改人名字',
`age` int(11) DEFAULT NULL,
`sl` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `group_index` (`group`)
) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
```
示例:
```java
public void testDemo1() {
String url = "jdbc:mysql://127.0.0.1:3306/neo?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String user = "neo_test";
String password = "neo@Test123";
String tableName = "neo_table1";
// 连接
Neo neo = Neo.connect(url, user, password);
// 或者添加driverClassName字段
// Neo neo = Neo.connect(url, user, password, "com.mysql.cj.jdbc.Driver");
// 插入
NeoMap data = neo.insert(tableName, NeoMap.of("group", "value", "name", "name1"));
data.put("group", "value1");
// 更新
neo.update(tableName, data);
// 删除
neo.delete(tableName, data);
// 查询一行
neo.one(tableName, data);
// 查询多行
neo.list(tableName, data);
// 查询指定列的一个值
neo.value(tableName, "group", data);
// 查询指定列的多个值
neo.values(tableName, "group", data);
// 查询分页
neo.page(tableName, data, NeoPage.of(1, 20));
// 分页个数
table.count(tableName, data);
// 执行sql
neo.execute("select * from %s where `group` =?", tableName, "group1");
// 事务
neo.tx(()->{
neo.update(tableName, NeoMap.of("id", 12, "group", "value1"));
neo.one(tableName, 12);
neo.update("neo_table2", NeoMap.of("name", 12));
});
// 批量插入
List<NeoMap> list = new ArrayList<>();
list.add(NeoMap.of("group", "group1", "name", "name1", "user_name", "user_name1"));
list.add(NeoMap.of("group", "group2", "name", "name2", "user_name", "user_name2"));
list.add(NeoMap.of("group", "group3", "name", "name3", "user_name", "user_name3"));
neo.batchInsert(tableName, list);
// 批量更新,group是条件
neo.batchUpdate(tableName, list, Columns.of("group"));
}
```
指定表的话,就更简单,一个表对应一个对象NeoTable
```java
public void testDemo2() {
String url = "jdbc:mysql://127.0.0.1:3306/neo?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String user = "neo_test";
String password = "neo@Test123";
String tableName = "neo_table1";
// 连接
Neo neo = Neo.connect(url, user, password);
NeoTable table = neo.getTable(tableName);
// 插入
NeoMap data = table.insert(NeoMap.of("group", "value"));
data.put("group", "value1");
// 更新
table.update(data);
// 删除
table.delete(data);
// 查询一行
table.one(data);
// 查询多行
table.list(data);
// 查询指定列的一个值
table.value("group", data);
// 查询指定列的多个值
table.values("group", data);
// 查询分页
table.page(data, NeoPage.of(1, 20));
// 分页个数
table.count(data);
// 批量插入
List<NeoMap> list = new ArrayList<>();
list.add(NeoMap.of("group", "group1", "name", "name1", "user_name", "user_name1"));
list.add(NeoMap.of("group", "group2", "name", "name2", "user_name", "user_name2"));
list.add(NeoMap.of("group", "group3", "name", "name3", "user_name", "user_name3"));
table.batchInsert(list);
// 批量更新,group是条件
table.batchUpdate(list, Columns.of("group"));
}
```
其中批量更新部分,我们采用最新的方式可以在数据量巨大时候,性能非常好,如下
```sql
update table_name a join
(
select 1 as `id`, 'holy' as `name`
union
select 2 as `id`, 'shit' as `name`
) b using(`id`)
set a.`name`=b.`name`;
}
```
表示更新表`table_name`,其中条件为`id`,对应修改的值为`name`
### 指定实体
上面我们对数据的操作全都是基于map,下面我们基于实体DO对数据库进行操作
```java
/**
* 指定表的话,就更简单
*/
@Test
public void testDemo3() {
String url = "jdbc:mysql://127.0.0.1:3306/neo?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String user = "neo_test";
String password = "neo@Test123";
String tableName = "neo_table1";
// 连接
Neo neo = Neo.connect(url, user, password);
NeoTable table = neo.getTable(tableName);
// 实体数据
DemoEntity3 entity = new DemoEntity3().setGroup("group1").setUsName("name1");
// 插入
DemoEntity3 result = table.insert(entity);
result.setUsName("name2");
// 更新
table.update(result);
// 删除
table.delete(result);
// 查询一行
table.one(result);
// 查询多行
table.list(result);
// 查询指定列的
table.value("group", NeoMap.of("user_name", "name2"));
// 查询指定列的多个值
table.values("group", NeoMap.of("user_name", "name2"));
// 查询分页,第一个参数是搜索条件
table.page(NeoMap.of("user_name", "name2"), NeoPage.of(1, 20));
// 分页个数
table.count(data);
// 批量插入
List<DemoEntity3> list = new ArrayList<>();
list.add(new DemoEntity().setGroup("group1").setName("name1").setUserName("userName1"));
list.add(new DemoEntity().setGroup("group2").setName("name2").setUserName("userName2"));
list.add(new DemoEntity().setGroup("group3").setName("name3").setUserName("userName3"));
table.batchInsertEntity(list);
// 批量更新
List<DemoEntity3> updateList = new ArrayList<>();
updateList.add(new DemoEntity().setId(1L).setGroup("group1").setName("name1").setUserName("userName1"));
updateList.add(new DemoEntity().setId(2L).setGroup("group2").setName("name2").setUserName("userName2"));
updateList.add(new DemoEntity().setId(3L).setGroup("group3").setName("name3").setUserName("userName3"));
table.batchUpdateEntity(list, Columns.of("id"));
}
```
### 复杂查询
除了基本的查询外,对于复杂的条件查询,可以采用稍微复杂点的搜索方式,不过使用也还是很简单
version >= 0.6.0
```java
/**
* 复杂表达式的搜索
* <p>其中有默认的过滤原则
*/
@Test
public void oneTest() {
NeoMap dataMap = NeoMap.of("group", "group_insert_express", "name", "name_insert_express");
neo.insert(TABLE_NAME, dataMap);
SearchQuery searchQuery;
searchQuery = new SearchQuery().and("group", "group_insert_express", "name", "name_insert_express");
Assert.assertEqu