Beelt初次使用

简介: Beelt初次使用

官方文档


地址:

BeetlSQL 2.12中文文档

Beetl 3中文文档

官网地址


依赖引入


以maven方式 导入pom.xml文件

    <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>3.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetlsql</artifactId>
            <version>2.13.0.RELEASE</version>
        </dependency>


数据库demo

CREATE TABLE `user` (
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `name` VARCHAR(64) DEFAULT NULL,
      `age` INT(4) DEFAULT NULL,
      `create_date` DATETIME NULL DEFAULT NULL,
      PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;


工具类


辅助类生成


生成pojo类代码

import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;

public class Assist {
    public static void main(String[] args) throws Exception {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/ph?serverTimezone=UTC";
        String userName = "root";
        String password = "123456";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new MySqlStyle();
// sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
// 数据库命名跟java命名一样,所以采用DefaultNameConversion,还有一个是UnderlinedNameConversion,下划线风格的,
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});
        sqlManager.genPojoCodeToConsole("user");
        sqlManager.genSQLTemplateToConsole("user");
    }
}

控制台输出结果:

BeetlSQL 运行在 product=false,md charset=UTF-8
package com.test;
import java.math.*;
import java.util.Date;
import java.sql.Timestamp;
import org.beetl.sql.core.annotatoin.Table;
/* 
* 
* gen by beetlsql 2021-11-30
*/
@Table(name="ph.user")
public class User   {
  
  // alias
  public static final String ALIAS_id = "id";
  public static final String ALIAS_age = "age";
  public static final String ALIAS_name = "name";
  public static final String ALIAS_create_date = "create_date";
  
  private Integer id ;
  private Integer age ;
  private String name ;
  private Date createDate ;
  
  public User() {
  }
  
  public Integer getId(){
    return  id;
  }
  public void setId(Integer id ){
    this.id = id;
  }
  
  public Integer getAge(){
    return  age;
  }
  public void setAge(Integer age ){
    this.age = age;
  }
  
  public String getName(){
    return  name;
  }
  public void setName(String name ){
    this.name = name;
  }
  
  public Date getCreateDate(){
    return  createDate;
  }
  public void setCreateDate(Date createDate ){
    this.createDate = createDate;
  }
}



sample
===
* 注释

  select #use("cols")# from user  where  #use("condition")#

cols
===
  id,name,age,create_date

updateSample
===
  
  id=#id#,name=#name#,age=#age#,create_date=#createDate#

condition
===

  1 = 1  
  @if(!isEmpty(id)){
   and id=#id#
  @}
  @if(!isEmpty(name)){
   and name=#name#
  @}
  @if(!isEmpty(age)){
   and age=#age#
  @}
  @if(!isEmpty(createDate)){
   and create_date=#createDate#
  @}
  
  
Process finished with exit code 0

Tools


有些报错我不知道怎么解决,便注释掉了

执行一些基础的sql语句

import com.example.dao.UserDao;
import com.example.pojo.User;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.query.Query;
import org.beetl.sql.ext.DebugInterceptor;

import java.util.List;

public class Tools {
    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/ph?serverTimezone=UTC";
        String userName = "root";
        String password = "985464";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new MySqlStyle();
// sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
// 数据库命名跟java命名一样,所以采用DefaultNameConversion,还有一个是UnderlinedNameConversion,下划线风格的,
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
// 最后,创建一个SQLManager,DebugInterceptor 不是必须的,但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});


//使用内置的生成的sql 新增用户,如果需要获取主键,可以传入KeyHolder
        User user = new User();
        user.setAge(19);
        user.setName("xiandafu");
        sqlManager.insert(user);

//使用内置sql查询用户
        int id = 1;
        user = sqlManager.unique(User.class, id);

//模板更新,仅仅根据id更新值不为null的列
        User newUser = new User();
        newUser.setId(1);
        newUser.setAge(20);
        sqlManager.updateTemplateById(newUser);

//模板查询
        User query = new User();
        query.setName("xiandafu");
        List<User> list = sqlManager.template(query);
        System.out.println(list);

//Query查询
//        Query userQuery = sqlManager.getQuery(User.class);
//        List<User> users = userQuery.lambda().andEq(User::getName, "xiandafy").select();

//使用user.md 文件里的select语句,参考下一节。
        User query2 = new User();
        query.setName("xiandafu");
        List<User> list2 = sqlManager.select("user.select", User.class, query2);
        System.out.println(list2);

// 这一部分需要参考mapper一章
//        UserDao dao = sqlManager.getMapper(UserDao.class);
//        List<User> list3 = dao.select(query2);
    }
}

控制台输出:

BeetlSQL 运行在 product=false,md charset=UTF-8
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
┏━━━━━ Debug [user._gen_insert] ━━━
┣ SQL:   insert into `user` (`name`,`age`,`create_date`) VALUES (?,?,?)
┣ 参数:  [xiandafu, 19, null]
┣ 位置:  com.example.utils.Tools.main(Tools.java:33)
┣ 时间:  23ms
┣ 更新:  [1]
┗━━━━━ Debug [user._gen_insert] ━━━

┏━━━━━ Debug [user._gen_selectById] ━━━
┣ SQL:   select * from `user` where `id` = ?
┣ 参数:  [1]
┣ 位置:  com.example.utils.Tools.main(Tools.java:37)
┣ 时间:  5ms
┣ 结果:  [User(id=1, age=20, name=xiandafu, createDate=null)]
┗━━━━━ Debug [user._gen_selectById] ━━━

┏━━━━━ Debug [user._gen_updateTemplateById] ━━━
┣ SQL:   update `user` set `age`=? where `id` = ?
┣ 参数:  [20, 1]
┣ 位置:  com.example.utils.Tools.main(Tools.java:43)
┣ 时间:  4ms
┣ 更新:  [1]
┗━━━━━ Debug [user._gen_updateTemplateById] ━━━

┏━━━━━ Debug [user._gen_selectByTemplate] ━━━
┣ SQL:   select * from `user` where 1=1 and `name`=? 
┣ 参数:  [xiandafu]
┣ 位置:  com.example.utils.Tools.main(Tools.java:48)
┣ 时间:  3ms
┣ 结果:  [2]
┗━━━━━ Debug [user._gen_selectByTemplate] ━━━

[User(id=1, age=20, name=xiandafu, createDate=null), User(id=2, age=19, name=xiandafu, createDate=null)]
┏━━━━━ Debug [user.select] ━━━
┣ SQL:   select * from user where 1=1 
┣ 参数:  []
┣ 位置:  com.example.utils.Tools.main(Tools.java:58)
┣ 时间:  4ms
┣ 结果:  [2]
┗━━━━━ Debug [user.select] ━━━

[User(id=1, age=20, name=xiandafu, createDate=null), User(id=2, age=19, name=xiandafu, createDate=null)]

Process finished with exit code 0
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
41288 184
|
XML JSON API
如何在 Postman 中上传文件和 JSON 数据
如果你想在 Postman 中同时上传文件和 JSON 数据,本文将带你一步一步地了解整个过程,包括最佳实践和技巧,让你的工作更轻松。
|
存储 索引
antd中table组件选中单行换样式(比如背景颜色)
在Ant Design (antd)的Table组件中,可以通过设置`onRow`属性来定义行点击事件,从而改变被点击行的样式,如背景颜色。`onRow`是一个函数,返回一个对象,对象包含事件处理函数,如`onClick`。同时,使用`rowClassName`属性来指定行的类名,结合状态管理,可以实现点击某行后改变其背景颜色的效果。具体实现时,需要在组件状态中保存当前被点击行的索引,然后通过`rowClassName`判断并返回相应的类名。
2115 2
antd中table组件选中单行换样式(比如背景颜色)
|
人工智能 监控 Java
面向切面编程(AOP)介绍--这是我见过最易理解的文章
这是我见过的最容易理解的文章,由浅入深介绍AOP面向切面编程,用科普版和专家版分别解说,有概念,有代码,有总结。
|
负载均衡 算法 Linux
LVS+Keepalived:实现高效软负载均衡的利器
本文介绍了如何使用LVS(Linux Virtual Server)和Keepalived搭建高可用负载均衡集群。LVS通过不同调度算法将请求转发给后端服务器,而Keepalived基于VRRP协议实现服务高可用,避免IP单点故障。具体步骤包括环境准备、安装配置ipvsadm和Keepalived、启动服务及测试。文中还详细解释了配置文件中的关键参数,并提供了故障转移测试方法。最后,文章简要对比了软件、硬件和云负载均衡方案的特点,帮助读者选择合适的负载均衡策略。
2447 4
|
程序员
程序员必知:微信报错40029微信模板推送消息报错问题
程序员必知:微信报错40029微信模板推送消息报错问题
865 0
|
前端开发 JavaScript API
2025年前端框架是该选vue还是react?有了大模型-例如通义灵码辅助编码,就不用纠结了!vue用的多选react,react用的多选vue
本文比较了Vue和React两大前端框架,从状态管理、数据流、依赖注入、组件管理等方面进行了详细对比。当前版本和下载量数据显示React更为流行,但Vue在国内用户量增长迅速。Vue 3通过组合式API提供了更灵活的状态管理和组件逻辑复用,适合中小型项目;React则更适合大型项目和复杂交互逻辑。文章还给出了选型建议,强调了多框架学习的重要性,认为技术问题已不再是选型的关键,熟悉各框架的最佳实践更为重要。
12477 1
|
开发工具 Android开发 git
解决Idea报错出现Git is not installed
解决Idea报错出现Git is not installed
7162 2
|
开发工具
适合个人建站的免费开源网站主题汇总(hexo&jekyll&hugo&wordpress&typecho)
适合个人建站的免费开源网站主题汇总(hexo&jekyll&hugo&wordpress&typecho)
2241 0