preface

Using Mybatis, it is usually necessary to write Entity, Mapper (Java, XML), which is very troublesome for small projects. Is there any way to write Entity only for CURD? The answer is yes.

Our needs

Write only a little code, implement CURD 2, a little configuration, complete like Wrapper 3, seamless compatibility with the original Mapper 4, including general Mapper?Copy the code

Achieve results

The above requirements can be fully met, and only 2 classes are needed to meet the above requirements

1. You only need to define a POJO to complete CURD

@Table(name = "user")
public class User {
    String id;
    String name;
    String phone;
}
Copy the code

2, similar to baseMapper use, direct injection (no need to write UserMapper)

@Resource
BaseMapper<User> userBaseMapper;
Copy the code

3. Simple Wrapper

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

4, to achieve dynamic SQL query

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

Project code

├ ─ ─ Java │ └ ─ ─ com │ └ ─ ─ demo │ ├ ─ ─ SimpleCurdApplication. Java │ ├ ─ ─ the core │ │ ├ ─ ─ BaseMapper. Java │ │ ├ ─ ─ Dal. Java │ │ └ ─ ─ SpringUtil. Java │ └ ─ ─ web │ ├ ─ ─ SimpleCurdController. Java │ └ ─ ─ User. Java └ ─ ─ resources └ ─ ─ application. YmlCopy the code

code

Github.com/scofier/Sim…