# easy-mybatis
**Repository Path**: xiaoyudeguang/easy-mybatis
## Basic Information
- **Project Name**: easy-mybatis
- **Description**: 一款便捷的基于切面的mybatis工具,兼容现有任意一款mybatis产品,从此彻底告别xml!
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2018-12-30
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## 查看最新源码请移步[源码地址](https://gitee.com/xiaoyudeguang/easy-develop/tree/master/easy-mybatis) || 查看最新教程请移步[教程地址](https://blog.csdn.net/qq_28802119/article/details/92109435)
# easy-mybatis
#### 介绍
easy-mybatis是一款便捷的mybatis工具。那么,为什么要用easy-mybatis呢,它的便捷性体现在哪里?想想两个问题:
1. mybatis本身存在什么问题?
mybatis本身需要编写大量的xml(不同意?想想多表联查...)。
2. 比较流行的mybatisplus等mybatis产品解决了什么问题?
mybatisplus等mybatis产品解决的是对单表的操作,并没有根治多表联查疯狂配xml的烦恼。
所以,就有了引入easy-mybatis的必要。easy-mybatis是一个基于切面的mybatis产品。所以,它可以和任意的其他类型的mybatis产品兼容,请放心使用!
## 使用说明
### Maven引用(点击 https://search.maven.org/search?q=g:io.github.xiaoyudeguang 查看最新版本)
```
io.github.xiaoyudeguang
easy-mybatis
3.1.8-RELEASE
```
### 实体类编写
```
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zlyx.easycore.annotations.Desc;
import com.zlyx.easymybatis.annotations.ForeignKey;
import com.zlyx.easyweb.web.annotations.TableBean;
@TableBean(todo = { "" }, value = "db_person", alias = "per")
@TableName("db_person")
public class Person {
@Desc(value = { "用户id" })
@TableId("person_id")
private Long id;
@Desc(value = { "用户姓名" })
@TableField(value = "name")
private String name;
@Desc(value = { "部门名称" })
@ForeignKey(key = "name", table = Dept.class)
@TableField(value = "dept_name")
private String deptName;
@Desc(value = "部门实体对象")
private Dept dept;
... 其他部分略(get和set方法)...
}
```
### mapper接口编写
PS:你可以像之前使用mybatis一样声明mapper接口,然后编写普通的mybatis方法;也可以继续像以前一样集成mybatisplus等产品。
```
import com.zlyx.easymybatis.abstraccts.AbstractMapper;
import com.zlyx.easymybatis.annotations.EasySelect;
import com.zlyx.easymybatis.annotations.Condition;
import com.zlyx.easymybatis.annotations.Table;
import com.zlyx.easymybatis.utils.Page;
@Mapper
public interface PersonMapper extends AbstractMapper {
/**
* 一个普通的mybatis方法
*/
@Select(value = { "select * from swh_person" })
public List testMybatis(String name);
/**
* EasyMybatis多表联查方法
*/
@EasySelect(tableName = "db_person", results = { Table.FILED_ALL }, tables = {
@Table(tableName = "db_dept") }, conditions = { @Condition(fields = { "name" }, tableName = "person") })
public List testSelect(String name);
/**
* EasyMybatis多表联查分页方法
*/
@EasySelect(tableName = "db_person", results = {Table.FILED_ALL},
tables = {
@Table(tableName = "db_dept")
},
conditions = {
@Condition(fields = { "name" }, tableName = "db_person")
}
page = true)
public Page testPage(String name, int pageNum, int pageSize);
}
```
> testSelect方法,会被直接翻译为sql语句:
select per.id as per_id, per.name = per_name, per.dept_name as per_deptName.... left join db_dept de on de.dept_name = per.dept_name from db_person per where per.name = "参数值"
> testPage方法,会自动完成sql的生成和分页查询,是不是超简单?
> 这里输入引用文本你不需要关心之后的事情,后续的执行easy-mybatis会自己完成。执行完成后,会返回一个person集合对象,每个person对象中包含有一个dept对象。是的,这一切,都只需要一个@EasySelect注解就可以了。
> 如果你希望得到json类型的返回结果,只需要将方法中的List替换为List即可(fastjson)。