5.3 数据库操作

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

    当然也可以使用连接池,只要你提供一个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;
    1. @Table(value = "t_user")
    2. @Data
    3. public class User extends ActiveRecord {
    4. private Integer id;
    5. private String username;
    6. private Integer age;
    7. private String realName;
    8. }

    这里使用了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 (?, ?, ?, ?, ?)
    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. // 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. List<User> users = user.where("id", "<", 2).findAll();
    2. System.out.println(users);

    排序

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

    查询记录数

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

    使用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. });
    1. ActiveRecord activeRecord = new ActiveRecord();
    2. activeRecord.execute("update t_user set age = 22 where age < 20");