mybatis-flex/readme_zh.md
2023-03-01 17:26:43 +08:00

8.2 KiB
Raw Blame History

Mybatis-Flex 一个优雅的 Mybatis 增强框架

特征

  • 1、很轻量整个框架只依赖 Mybatis 再无其他第三方依赖
  • 2、Entity 类的基本增删改查、以及分页查询
  • 3、Row 通用映射支持,可以无需实体类对数据库进行增删改查
  • 4、支持多种数据库类型自由通过方言持续扩展
  • 5、支持联合主键以及不同的主键内容生成策略
  • 6、极其友好的 SQL 联动查询IDE 自动提示不再担心出错
  • 7、更多小惊喜

hello world

第一步:编写 Entity 实体类


@Table("tb_account")
public class Account {

    @Id()
    private Long id;
    private String userName;
    private Date birthday;
    private int sex;

    //getter setter
}

第二步,编写 Mapper 类,并继承 BaseMapper

public interface AccountMapper extends BaseMapper<Account> {
    //只需定义 Mapper 接口即可,可以无任何内容。
}

第三步:开始查询数据

示例 1查询 1 条数据

class HelloWorld {
    public static void main(String... args) {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        MybatisFlexBootstrap.getInstance()
                .setDatasource(dataSource)
                .addMapper(AccountMapper.class)
                .start();


        //示例1查询 id=100 条数据
        Account account = MybatisFlexBootstrap.getInstance()
                .execute(AccountMapper.class, mapper ->
                        mapper.selectOneById(100)
                );
    }
}

示例2查询列表

class HelloWorld {
    public static void main(String... args) {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        MybatisFlexBootstrap.getInstance()
                .setDatasource(dataSource)
                .addMapper(AccountMapper.class)
                .start();
        
        //示例2通过 QueryWrapper 构建条件查询数据列表
        QueryWrapper query = QueryWrapper.create()
                .select()
                .from(ACCOUNT)
                .where(ACCOUNT.ID.ge(100))
                .and(ACCOUNT.USER_NAME.like("张").or(ACCOUNT.USER_NAME.like("李")));

        // 执行 SQL
        // ELECT * FROM `tb_account`
        // WHERE `tb_account`.`id` >=  100
        // AND (`tb_account`.`user_name` LIKE '%张%' OR `tb_account`.`user_name` LIKE '%李%' )
        List<Account> accounts = MybatisFlexBootstrap.getInstance()
                .execute(AccountMapper.class, mapper ->
                        mapper.selectListByQuery(query)
                );
        
    }
}

示例3分页查询

class HelloWorld {
    public static void main(String... args) {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        MybatisFlexBootstrap.getInstance()
                .setDatasource(dataSource)
                .addMapper(AccountMapper.class)
                .start();

        // 示例3分页查询
        // 查询第 5 页,每页 10 条数据,通过 QueryWrapper 构建条件查询
        QueryWrapper query = QueryWrapper.create()
                .select()
                .from(ACCOUNT)
                .where(ACCOUNT.ID.ge(100))
                .and(ACCOUNT.USER_NAME.like("张").or(ACCOUNT.USER_NAME.like("李")))
                .orderBy(ACCOUNT.ID.desc());

        // 执行 SQL
        // ELECT * FROM `tb_account`
        // WHERE `tb_account`.`id` >=  100
        // AND (`tb_account`.`user_name` LIKE '%张%' OR `tb_account`.`user_name` LIKE '%李%' )
        // ORDER BY `tb_account`.`id` DESC
        // LIMIT 40,10
        Page<Account> accounts = MybatisFlexBootstrap.getInstance()
                .execute(AccountMapper.class, mapper ->
                        mapper.paginate(5, 10, query)
                );

    }
}

QueryWrapper 示例

select *

QueryWrapper query=new QueryWrapper();
        query.select().from(ACCOUNT)

// SQL: 
// SELECT * FROM tb_account

select columns

QueryWrapper query=new QueryWrapper();
        query.select(ACCOUNT.ID,ACCOUNT.USER_NAME).from(ACCOUNT)

// SQL: 
// SELECT tb_account.id, tb_account.user_name 
// FROM tb_account
QueryWrapper query=new QueryWrapper();
        query.select(ACCOUNT.ALL_COLUMNS).from(ACCOUNT)

// SQL: 
// SELECT tb_account.id, tb_account.user_name, tb_account.birthday, 
// tb_account.sex, tb_account.is_normal 
// FROM tb_account

select functions

 QueryWrapper query=new QueryWrapper()
        .select(ACCOUNT.ID
        ,ACCOUNT.USER_NAME
        ,max(ACCOUNT.BIRTHDAY)
        ,avg(ACCOUNT.SEX).as("sex_avg"))
        .from(ACCOUNT);

// SQL: 
// SELECT tb_account.id, tb_account.user_name, 
// MAX(tb_account.birthday), 
// AVG(tb_account.sex) AS sex_avg 
// FROM tb_account

where

  QueryWrapper queryWrapper=QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .where(ACCOUNT.ID.ge(100))
        .and(ACCOUNT.USER_NAME.like("michael"));

// SQL: 
// SELECT * FROM tb_account 
// WHERE tb_account.id >=  ?  
// AND tb_account.user_name LIKE  ? 

exists, not exists

    QueryWrapper queryWrapper=QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .where(ACCOUNT.ID.ge(100))
        .and(
        exist(
        selectOne().from(ARTICLE).where(ARTICLE.ID.ge(100))
        )
        );

// SQL: 
// SELECT * FROM tb_account 
// WHERE tb_account.id >=  ?  
// AND EXIST (
// SELECT 1 FROM tb_article WHERE tb_article.id >=  ? 
// )

and (...) or (...)

    QueryWrapper queryWrapper=QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .where(ACCOUNT.ID.ge(100))
        .and(ACCOUNT.SEX.eq(1).or(ACCOUNT.SEX.eq(2)))
        .or(ACCOUNT.AGE.in(18,19,20).or(ACCOUNT.USER_NAME.like("michael")));

// SQL: 
// SELECT * FROM tb_account 
// WHERE tb_account.id >=  ?  
// AND (tb_account.sex =  ?  OR tb_account.sex =  ? ) 
// OR (tb_account.age IN (?,?,?) OR tb_account.user_name LIKE  ? )

group by

    QueryWrapper queryWrapper=QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .groupBy(ACCOUNT.USER_NAME);

// SQL: 
// SELECT * FROM tb_account 
// GROUP BY tb_account.user_name

having

    QueryWrapper queryWrapper=QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .groupBy(ACCOUNT.USER_NAME)
        .having(ACCOUNT.AGE.between(18,25));

// SQL: 
// SELECT * FROM tb_account 
// GROUP BY tb_account.user_name 
// HAVING tb_account.age BETWEEN  ? AND ?

jion

   QueryWrapper queryWrapper = QueryWrapper.create()
        .select()
        .from(ACCOUNT)
        .leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID))
        .where(ACCOUNT.AGE.ge(10));

// SQL: 
// SELECT * FROM tb_account 
// LEFT JOIN tb_article 
// ON tb_account.id = tb_article.account_id 
// WHERE tb_account.age >=  ? 

可能存在问题

如何通过实体类 Account.java 生成 QueryWrapper 所需要的 "ACCOUNT" 类 ?

答:通过开发工具构建项目(如下图),或者执行 maven 编译命令: mvn clean package

Db 工具类

Db 是一个极度轻量的数据库操作工具类,以下是一些示例:

String sql = "insert into tb_account(id,name) value (?, ?)";
Db.insertBySql(sql,1,"michael");

Row account = new Row();
account.set("id",100);
account.set("name","Michael");
Db.insertRow("tb_account",account);

更多 Db 的方法请查看 Db.java

更多示例