Skip to content

Instantly share code, notes, and snippets.

@scofier
Last active April 25, 2020 08:17
Show Gist options
  • Save scofier/0c263f9c84b6ba0e01d03067208b54ad to your computer and use it in GitHub Desktop.
Save scofier/0c263f9c84b6ba0e01d03067208b54ad to your computer and use it in GitHub Desktop.
Mybatis通用Dao,类ActiveRcord,无需写Mapper

前言

使用Mybatis,通常需要写Entity,Mapper(java、xml),对于小项目很麻烦,有没有只写Entity就可以进行CURD呢?

我们的需求

1、只写少量代码,实现CURD
2、少量配置,完成类似ActiveRecord
3、无缝兼容原有的Mapper
4、再包含通用Mapper?

实现结果

上述需求可以完全满足,而且只需要2个类就搞定上面的需求

1、只需要定义一个 pojo ,就可以完成CURD

@Entity
@Table(name = "user")
public class User {
    String id;
    String name;
    String status;
    String phone;
    String password;
}

2、类似baseMapper使用,直接注入(无需写UserMapper)

@Resource
BaseMapper<User> userBaseMapper;

3、类似ActiveRecord

User user = new User();
Dal.with(User.class).select(user);

4、实现动态SQL查询

User user = new User();
Dal.with(User.class).query(sql -> sql.SELECT("id,name").WHERE("name=#{name}"), user);

代码

https://github.com/scofier/SimpleCurd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment