introduce
HasorDB is a full-featured database access tool that provides object mapping, rich type handling, dynamic SQL, stored procedures, 20+ built-in paging dialects, support for nested transactions, multiple data sources, conditional constructors, INSERT strategies, multiple statements/multiple result. And compatible with Spring and MyBatis usage. It does not depend on any other framework, so it can be easily integrated with any framework.
Features
-
familiar way
- JdbcTemplate interface (highly compatible with Spring JDBC)
- Mapper file method (highly compatible with MyBatis)
- LambdaTemplate (highly close to MyBatis Plus, jOOQ and BeetlSQL)
- @Insert, @Update, @Delete, @Query, @Callable annotations (similar to JPA)
-
Transaction support
- Supports 5 transaction isolation levels, 7 transaction propagation behaviors (same as Spring tx)
- Provides TransactionTemplate, TransactionManager interface mode declarative transaction control capabilities (same usage as Spring)
-
Features and advantages
- Supports paginated queries and provides multiple database dialects (20+)
- Support for INSERT strategies (INTO, UPDATE, IGNORE)
- Richer TypeHandler (MyBatis 40+, HasorDB 60+)
- Mapper XML supports multiple statements and multiple results
- Provide a unique rule expansion mechanism to make dynamic SQL easier
@{xxx, expr , xxxxx }
- Support for stored procedures
- Supports time types in JDBC 4.2 and Java8
- Support for multiple data sources
Release.Node
- New feature, Lambda support for pure Map is more friendly, and LambdaTemplate can be used in an entityless way at present
- Official documentation site: http://www.hasor.cn , https://www.hasordb.net
import dependencies
The latest version of HasorDB so far is: 4.3.2
- The latest version can also be found at https://mvnrepository.com/artifact/net.hasor/hasor-db
<dependency>
<groupId>net.hasor</groupId>
<artifactId>hasor-db</artifactId>
<version>4.3.2</version>
</dependency>
Then introduce the database driver. Take MySQL and Maven as an example:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
You can use HasorDB without relying on database connection pools, but database connection pools are standard in most projects. Here we use Alibaba's Druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
Finally prepare a database table and initialize some data ( CreateDB.sql
files)
drop table if exists `test_user`;
create table `test_user` (
`id` int(11) auto_increment,
`name` varchar(255),
`age` int,
`create_time` datetime,
primary key (`id`)
);
insert into `test_user` values (1, 'mali', 26, now());
insert into `test_user` values (2, 'dative', 32, now());
insert into `test_user` values (3, 'jon wes', 41, now());
insert into `test_user` values (4, 'mary', 66, now());
insert into `test_user` values (5, 'matt', 25, now());
execute SQL
Use SQL to read data, PrintUtils
and two tool classes can be found in the example project DsUtils
// 创建数据源 DataSource dataSource = DsUtils.dsMySql();
LambdaTemplate lambdaTemplate = new LambdaTemplate(dataSource);// 新增
Map<String, Object> newValue = new HashMap<>(); newValue.put("id", 20); newValue.put("name", "new name"); newValue.put("age", 88); newValue.put("create_time", new Date()); InsertOperation<Map<String, Object>> insert = lambdaTemplate.lambdaInsert("test_user"); int result = insert.applyMap(newValue).executeSumResult()// 更新
Map<String, Object> updateValue = new HashMap<>(); updateValue.put("name", "new name"); updateValue.put("age", 88); MapUpdateOperation update = lambdaTemplate.lambdaUpdate("test_user"); int result = update.eq("id", 1).updateByMap(updateValue).doUpdate();// 查询 List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from test_user"); // 打印测试数据 PrintUtils.printMapList(mapList)