Sqlbean是一款使用Java面向对象思想来编写并生成Sql语句的工具,在此基础上对Mybatis和Spring Jdbc实现了类似于JPA的轻量级插件支持。其中内置大量常用SQL执行的方法,可以非常方便的达到你想要的目的,相对复杂的SQL语句也得以支持,在常规的项目开发几乎做到不写DAO层,可以有效的提高项目开发的效率,让开发者更专注于业务代码的编写。
🚀特点: 零入侵, 多数据源, 动态Schema, 读写分离, 自动建表, 连表查询, 乐观锁, 分页, 支持Mybatis和Spring Jdbc
💻环境: JDK8+, 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, github
简单上手
1.引入Maven依赖
<dependency>
<groupId>cn.vonce</groupId>
<artifactId>vonce-sqlbean-spring</artifactId>
<version>1.5.8</version>
</dependency>
2.标注实体类
@SqlTable("d_essay")
public class Essay {
@SqlId(type = IdType.SNOWFLAKE_ID_16)
private Long id;
private String userId;
private String content;
private Date creationTime;
private Date updateTime;
/**省略get set方法*/
}
3.无需Dao层,Service层接口只需继承SqlBeanService<实体类, id类型>
public interface EssayService extends SqlBeanService<Essay, Long> {
//已内置大量常用查询、更新、删除、插入方法,这里可以写自己封装的方法
}
4.Service实现类只需继承MybatisSqlBeanServiceImpl<实体类, id类型>和实现你的Service接口
//使用Spring Jdbc的话将继承的父类改成SpringJdbcSqlBeanServiceImpl即可
@Service
public class EssayServiceImpl extends MybatisSqlBeanServiceImpl<Essay, Long> implements EssayService {
}
5.Controller层
@RequestMapping("essay")
@RestController
public class EssayController {
@Autowired
private EssayService essayService;
//查询
@GetMapping("select")
public RS select() {
//查询列表
List<Essay> list = essayService.selectAll();
list = essayService.selectByCondition(Wrapper.where(gt(Essay$.id, 10)).and(lt(Essay$.id, 20)));
//查询一条
Essay essay = essayService.selectById(1L);
essay = essayService.selectOneByCondition(Wrapper.where(eq(Essay$.id, 333)));
//复杂查询
Select select = new Select();
select.column(Essay$.id).column(Essay$.content);
select.where().gt(Essay$.id, 1).and().eq(Essay$.content, "222");
select.orderByDesc(Essay$.creation_time);
list = essayService.select(select);
//用于查询Map
Map<String, Object> map = essayService.selectMap(select);
List<Map<String, Object>> mapList = essayService.selectMapList(select);
return super.successHint("获取成功", list);
}
//分页
@GetMapping("getList")
public Map getList(HttpServletRequest request) {
// 查询对象
Select select = new Select();
ReqPageHelper<Essay> pageHelper = new ReqPageHelper<>(request);
pageHelper.paging(select, essayService);
return pageHelper.toResult("获取列表成功");
}
//更新
@PostMapping("update")
public RS update(Essay essay) {
//根据bean内部id更新
long i = essayService.updateByBeanId(essay);
//根据条件更新
//i = essayService.updateByCondition(Wrapper.where(gt(Essay$.id, 1)).and(eq(Essay$.content, "222")));
if (i > 0) {
return super.successHint("更新成功");
}
return super.othersHint("更新失败");
}
//删除
@PostMapping("deleteById")
public RS deleteById(Integer[] id) {
//根据id删除
long i = essayService.deleteById(id);
//根据条件删除
//i = essayService.deleteByCondition(Wrapper.where(gt(Essay$.id, 1)).and(eq(Essay$.content, "222")));
if (i > 0) {
return super.successHint("删除成功");
}
return super.othersHint("删除失败");
}
//插入
@PostMapping("add")
public RS add() {
List<Essay> essayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Essay essay = new Essay(i, "name" + i);
essayList.add(essay);
}
essayService.insert(essayList);
return successHint("成功");
}
}
本次更新内容:
1:修复@SqlJoin连表查询指定查询字段失效问题;
2:修改获取全部字段的顺序,父类字段显示在前面;
3:修复只有一个小写字母时驼峰转下划线失效问题;
4:修复连表查询VO类没有标识注解或继承类时导致的问题;
5:修复如果有连表查询时自动生成的where条件没有生成全名导致异常的问题;
6:修复其他内容;