# vonce-sqlbean
**Repository Path**: rtpay/vonce-sqlbean
## Basic Information
- **Project Name**: vonce-sqlbean
- **Description**: Sqlbean是一款使用Java面向对象思想来编写并生成Sql语句的工具,在此基础上对Mybatis和Spring Jdbc实现了类似于JPA的轻量级插件支持,无需编写DAO层,助你快速进行项目开发。
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: https://gitee.com/iJovi/vonce-sqlbean
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 22
- **Created**: 2022-03-28
- **Last Updated**: 2022-03-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## Sqlbean
#### 介绍
###### Sqlbean是一款使用Java面向对象思想来编写并生成Sql语句的工具,在此基础上对Mybatis和Spring Jdbc实现了类似于JPA的轻量级插件支持。其中内置大量常用SQL执行的方法,可以非常方便的达到你想要的目的,相对复杂的SQL语句也得以支持,在常规的项目开发几乎做到不写DAO层,可以有效的提高项目开发的效率,让开发者更专注于业务代码的编写。
###### 🚀特点: 零入侵, 多数据源, 动态Schema, 读写分离, 自动建表, 连表查询, 乐观锁, 分页, 支持Mybatis和Spring Jdbc
###### 💻环境: JDK7+, Mybatis3.2.4+, (Spring MVC 4.1.2+, Spring Boot 1.x, Spring Boot 2.x)
###### 💿数据库: Mysql, MariaDB, Oracle, Sqlserver2008+, PostgreSQL, DB2, Derby, Sqlite, HSQL, H2
###### Sqlbean For Android请移步这里👉 [gitee](https://gitee.com/iJovi/vonce-sqlbean-android "vonce-sqlbean-android"), [github](https://github.com/Jovilam77/vonce-sqlbean-android "vonce-sqlbean-android")
#### 简单上手
###### 1.引入Maven依赖
cn.vonce
vonce-sqlbean-spring
1.5.1-RELEASE
###### 2.标注实体类
```java
//标识表名
@SqlTable("d_essay")
public class Essay {
//标识id字段
@SqlId(type = IdType.SNOWFLAKE_ID_16)
//@SqlColumn("id")
private Long id;
//@SqlColumn("user_id")
private String userId;
//@SqlColumn("content")
private String content;
//@SqlColumn("creation_time")
private Date creationTime;
//标识乐观锁字段
@SqlVersion
//@SqlColumn("update_time")
private Date updateTime;
/**省略get set方法*/
}
```
###### 3.无需Dao层,Service层接口只需继承SqlBeanService<实体类, ID>
```java
public interface EssayService extends SqlBeanService {
//已内置大量常用查询、更新、删除、插入方法,这里可以写自己封装的方法
}
```
###### 4.Service实现类只需继承MybatisSqlBeanServiceImpl<实体类, ID>和实现你的Service接口
```java
//使用Spring Jdbc的话将继承的父类改成SpringJdbcSqlBeanServiceImpl即可
@Service
public class EssayServiceImpl extends MybatisSqlBeanServiceImpl implements EssayService {
}
```
###### 5.Controller层
```java
@RequestMapping("essay")
@RestController
public class EssayController {
@Autowired
private EssayService essayService;
//查询
@GetMapping("select")
public RS select() {
//查询列表 全部
List list = essayService.selectAll();
//查询列表 根据条件查询 方式一
list = essayService.selectByCondition("& > ?", $Essay.id, 20);
//查询列表 根据条件查询 方式二 推荐
list = essayService.selectByCondition(Wrapper.where(Cond.gt($Essay.id, 10)).and(Cond.lt($Essay.id, 20)));
//查询单条 根据id
Essay essay = essayService.selectById(1L);
//查询单条 根据条件查询 方式一
essay = essayService.selectOneByCondition("& = ?", $Essay.id, 1);
//查询单条 根据条件查询 方式二 推荐
essay = essayService.selectOneByCondition(Wrapper.where(Cond.eq($Essay.id, 333)));
//复杂查询
Select select = new Select();
//指定查询的字段
select.column(SqlEssay.id).column($Essay.content);
//指定查询的表 可不写
//select.setTable(Essay.class);
//看需求指定连表 这里不演示
//select.join("","");
//id 大于 1 这里的id建议用$Essay.id常量替代
select.where("id", 1, SqlOperator.GREATER_THAN);
//并且 内容等于222 这里的content建议用$Essay.content常量替代
select.wAND("content", "222");
//条件也可用包装器 复杂条件推荐使用
//select.setWhere(Wrapper.where(Cond.gt($Essay.id, 1)).and(Cond.eq($Essay.content, "222")));
//也可使用表达式 如果这三种条件同时出现 那么此方式优先级最高 上面包装器次之
//select.setWhere("& = ? AND & = ?", $Essay.id, 1, $Essay.content, "222");
//根据id倒序
select.orderBy("id", SqlSort.DESC);
//用于查询Map 多条结果时会报错
Map map = essayService.selectMap(select);
//用于查询Map列表
List