第5章 数据库 - 5.3 数据库操作

优质
小牛编辑
124浏览
2023-12-01

这里我们介绍Blade-JDBC这款数据库支持框架的使用方法,为Blade而生,
不能再简单了!

这是一款Java版的ActiveRecord框架,很大程度上让你快速开发,目前只支持MYSQL语法。

BladeJdbc版本 0.2.0+

配置

  1. Base.open("jdbc:mysql://localhost:3306/demo", "root", "123456");

当然也可以使用连接池,只要你提供一个DataSource即可:

  1. InputStream in = new FileInputStream(new File("druid.properties"));
  2. Properties props = new Properties();
  3. props.load(in);
  4. DataSource dataSource = DruidDataSourceFactory.createDataSource(props);
  5. Base.open(dataSource);

创建一个表结构

  1. CREATE TABLE `t_user` (
  2. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3. `username` varchar(20) DEFAULT NULL,
  4. `password` varchar(50) DEFAULT NULL,
  5. `real_name` varchar(20) DEFAULT NULL,
  6. `age` int(10) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Model

  1. @Table(value = "t_user")
  2. @Data
  3. public class User extends ActiveRecord {
  4. private Integer id;
  5. private String username;
  6. private String password;
  7. private Integer age;
  8. private String realName;
  9. }

这里使用了lombok插件,帮你自动生成GetterSetter

插入

  1. User user = new User();
  2. user.setUsername("jack");
  3. user.setPassword("123556");
  4. user.setRealName("杰克");
  5. user.setAge(20);
  6. // insert into t_user (id, username, password, age, real_name) VALUES (NULL, ?, ?, ?, ?)
  7. user.save();

更新

根据主键更新

  1. User user = new User();
  2. user.setUsername("jack_up");
  3. // update t_user set username = ? where id = ?
  4. user.where("id", 43).update();
  1. User user = new User();
  2. user.setAge(19);
  3. // update t_user set age = ? where id = ?
  4. user.update(43);

根据条件更新

  1. User user = new User();
  2. user.setAge(32);
  3. // update t_user set age = ? where age < ?
  4. user.where("age", "<", 20).update();

删除

  1. User user = new User();
  2. user.where("id", 44).delete();
  3. // delete from t_user where id = ?
  4. user.delete("id", 44);
  5. user.delete(44);

查询

根据主键查询

  1. User user = new User();
  2. // select * from t_user where id = ?
  3. User u1 = user.find(1);

查询所有

  1. User user = new User();
  2. List<User> users = user.findAll();
  3. System.out.println(users);

按条件查询

  1. User user = new User();
  2. user.setId(1);
  3. List<User> users = user.findAll();
  4. System.out.println(users);
  1. User user = new User();
  2. List<User> users = user.where("id", 2).findAll();
  3. System.out.println(users);
  1. User user = new User();
  2. List<User> users = user.where("id", "<", 2).findAll();
  3. System.out.println(users);

排序

  1. User user = new User();
  2. List<User> users = user.findAll(OrderBy.desc("id"));

查询记录数

  1. User user = new User();
  2. long count = user.count();
  3. System.out.println(count);

使用SQL查询单条

  1. User user = new User();
  2. System.out.println(user.query("select * from t_user order by id desc"));
  3. System.out.println(user.query("select * from t_user where id = ?", 1));

使用SQL查询列表

  1. User user = new User();
  2. System.out.println(user.queryAll("select * from t_user"));
  3. System.out.println(user.queryAll("select * from t_user where id = ?", 1));

Like Or

  1. User user = new User();
  2. user.where("username", "jack").or("real_name", "jack");
  3. long count = user.count();
  4. System.out.println("count=" + count);
  5. List<User> users = user.like("username", "%jac%").and("age", ">", 18).findAll();
  6. System.out.println(users);

分页查询

  1. User user = new User();
  2. Page<User> page = user.page(1, 3);
  3. System.out.println(page);
  1. User user = new User();
  2. Page<User> page = user.page(new PageRow(1, 2));
  3. System.out.println(page);

转换分页结果

  1. User user = new User();
  2. Page<User> page = user.page(1, 10);
  3. Page<String> userNamePage = page.map(u -> u.getUsername());
  4. System.out.println(userNamePage);

事务

  1. Base.atomic(() -> {
  2. User user = new User();
  3. user.setPassword("999");
  4. user.update(42);
  5. // int a = 1 / 0;
  6. System.out.println("aasdasd");
  7. return true;
  8. });

执行任意SQL

  1. ActiveRecord activeRecord = new ActiveRecord();
  2. activeRecord.execute("update t_user set age = 22 where age < 20");