官方文档
地址:
依赖引入
以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