迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:hydrogen-dao开源软件地址:https://gitee.com/yidinghe/hydrogen-dao开源软件介绍:hydrogen-daohydrogen-dao 是一个 Java 的轻量级的数据库访问库,依赖标准的 JDBC 接口。主要功能有:
使用方法参考源码下的 当前分支的版本为 添加依赖关系请在 pom.xml 的 <dependency> <groupId>com.github.yiding-he</groupId> <artifactId>hydrogen-dao</artifactId> <version>${hydrogen-dao.version}</version></dependency> 示例初始化// 初始化 DataSource 对象DataSource dataSource = new com.zaxxer.hikari.HikariDataSource();...// 将 DataSource 对象注册到 DataSources。com.hyd.dao.DataSources dataSources = DataSources.getInstance();dataSources.setDataSource("default", dataSource);// 获得针对某个数据源的 DAO 对象DAO dao = new DAO("default"); Spring Boot 自动初始化如果你的项目是基于 Spring Boot,那么可以简化上面的过程。首先添加下面的依赖关系: <dependency> <groupId>com.github.yiding-he</groupId> <artifactId>spring-boot-starter-hydrogen-dao</artifactId> <version>${hydrogen-dao.version}</version></dependency> 然后在 spring.datasource.url = [JDBC URL]spring.datasource.driver-class-name = [JDBC Driver]spring.datasource.username = [USERNAME]spring.datasource.password = [PASSWORD] 这样就可以在代码中直接获取 DAO 对象了,例如: @Controllerpublic class HomeController { @Autowired private DAO dao; // 直接获取 DAO 对象} 查询记录List<User> userList = dao.query( User.class, // 包装类(可选) "select * from USER where NAME like ? and ROLE=?", // 语句 "admin%", 3); // 参数(可选)userList.forEach(user -> { System.out.println("user name: " + user.getName());}); 执行带参数名的 SQLMappedCommand cmd = new MappedCommand("update USERS set ROLE=#role# where ID in(#userid#)") .setParam("role", "admin") .setParam("userid", 1, 2, 3, 4); // 数组或 List 都可以dao.execute(cmd);
构造动态查询条件不用写恶心的 dao.query(SQL .Select("ID", "NAME", "DESCRIPTION") .From("USERS") .Where("ID in ?", 10, 22, 135) // 会自动扩展为 "ID in (?,?,?)"。也可以用 List 作为参数 .And(disabled != null, "DISABLED=?", disabled) // 仅当变量 disabled 值不为 null 时才会加入该查询条件 .AndIfNotEmpty("DISABLED=?", disabled) // 效果同上); 执行事务final DAO dao = getDAO();DAO.runTransaction(() -> { // 所有事务都以 Runnable 的方式执行,简单明了 dao.execute("insert into USER(id,name) values(?,?)", 1, "user1"); throw new RuntimeException(); // 之前的 insert 将会回滚,同时抛出该异常}); 更新2021-10-19
2021-10-09
2020-10-31
2020-07-25
2019-10-11
2019-10-02
2019-10-01
2019-08-12
2019-03-11
2019-01-17
2018-07-09
2018-04-21
2018-04-07
2017-12-22:
2017-12-20:
2017-04-12:
2017-01-01:
2016-10-19:
2015-12-27:
2015-05-28:
2015-03-22:
2015-03-20:
文档具体的文档都在源代码 docs 目录下。 ![]() |
请发表评论