-
Notifications
You must be signed in to change notification settings - Fork 15
GettingStarted
If you use Java6, add the following to the project's pom.xml file:
<dependency>
<groupId>com.github.drinkjava2</groupId>
<artifactId>jsqlbox</artifactId> <!-- Without suffix, this is Java6 version -->
<version>2.0.4</version> <!-- or current Maven highest version -->
</dependency>
If using Java8, you should add in pom.xml:
<dependency>
<groupId>com.github.drinkjava2</groupId>
<artifactId>jsqlbox-java8</artifactId> <!-- with suffix java8, this is Java8 version -->
<version>2.0.4</version> <!-- or current Maven highest version -->
</dependency>
For Java6, Maven will automatically download the two packages jsqlbox-2.0.4.jar and commons-dbutils-1.7.jar. For Java8, Maven will automatically download the four packages jsqlbox-java8-2.0.4, jsqlbox-2.0.4.jar, commons-dbutils-1.7.jar, and jbeanbox-2.4.9. Don't be scared by the Java8 version with so many package dependencies. If you open the jsqlbox-java8 source directory, you will find that its source code has only 3 files. The main purpose of the Java8 version is to demonstrate Lambda and ActiveEntity. An IOC tool (produced by the home), so that a pom dependency can include DAO related functions without introducing Spring.
The following is the architecture diagram of jSqlBox, which also shows the dependencies between modules:
Each box in the above architecture diagram represents a module, which is separately released in the Maven central library. The sub-modules used in jSqlBox such as jDialects, jTransactions, and jDbPro are packaged and released in the form of source code to reduce the number of packages, but They also have separate distributions on the Maven Central Library, which can be downloaded separately.
jSqlBox works in auto-commit mode by default. If you want it to support transactions, please refer to the section "Transaction Configuration" and several demonstration projects "jsqlbox-in-jfinal" and "jsqlbox-in-springboot" in the demo directory. "jsqlbox-in-springboot-mybatis", "jsqlbox-in-actframework", "jsqlbox-xa-atomikos", etc. The last one is a demonstration of distributed transactions in a sub-library environment.
If you are using SpringBoot, its declarative transaction configuration can be simplified to this:
Public class MyAppDemo{
@Autowired
DataSource ds;
Public static void main(String[] args) {
SpringApplication.run(MyAppDemo.class, args);
}
@Bean
Public SqlBoxContext createDefaultSqlBoxContext() {
SqlBoxContext ctx = new SqlBoxContext(ds);
ctx.setConnectionManager(SpringTxConnectionManager.instance());//Transactional Connection Manager
SqlBoxContext.setGlobalSqlBoxContext(ctx);// static global context, for ActiveRecord
Return ctx;
}
}
The code is very succinct, but it's not easy for novices to understand it, because Spring uses the default conventions in the background and does a lot of things (and Spring introduces a lot of bloated libraries). This example is located in jsqlbox-in-springboot in the demo directory.
Add the following dependencies to pom.xml:
<dependency>
<groupId>com.github.drinkjava2</groupId>
<artifactId>jsqlbox-java8</artifactId> <!-- Java8 version of jSqlBox -->
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <!--H2 in-memory database -->
<version>1.3.176</version>
</dependency>
Type the following program in the IDE and run:
Public class HelloWorld implements ActiveEntity<HelloWorld> {
Private String name;
Public String getName() {return name; }
Public void setName(String name) {this.name = name; }
Public static void main(String[] args) {
DataSource ds = JdbcConnectionPool
.create("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0", "sa", "");
SqlBoxContext ctx = new SqlBoxContext(ds);
SqlBoxContext.setGlobalSqlBoxContext(ctx);
String[] ddls = ctx.toCreateDDL(HelloWorld.class);
For (String ddl : ddls)
ctx.nExecute(ddl);
New HelloWorld().putField("name", "Hello jSqlBox").insert();
System.out.println(ctx.iQueryForString("select name from HelloWorld"));
}
}Print out "Hello jSqlBOx", indicating that a record has been inserted in the in-memory database. Bingo! There is no interface, implementation, garbage code like Dao.
The above example, if working in Java6, 7 environment, the java6 version of jSqlBox should be introduced in pom, implements ActiveEntity should be changed to extends ActiveRecord. The toCreateDDL() method in the above example takes advantage of jDialect's ability to convert the annotated configured entity bean into a DDL script corresponding to the database dialect.
Using jSqlBox first to create a SqlBoxContext instance, there are two construction methods: SqlBoxContext() SqlBoxContext(DataSource) When the constructor parameter is empty, it means to create a SqlBoxConext instance with no data source. In this case, because the SqlBoxContext does not know where the data source is, you must add a Connection instance to each method as a parameter to pass it to, for example. Ctx.query(conn, "select * from users where name=?", "tom");
When there is a DataSource instance in the constructor parameter, it means to create a SqlBoxConext instance with a data source. In this case, the Connection instance is not required as a parameter, such as ctx.query("select * from users where name=?", " Tom");
Students familiar with DbUtils may be familiar with the above usage, because SqlBoxContext is a subclass of QueryRunner, so there are also constructors similar to DbUtils' QueryRunner, and inherit all the methods of QueryRunner.
In complex situations, the settings of the SqlBoxContext instance, such as logs, templates, dialects, transactions, interceptor classes, etc., can directly set its various options, such as ctx.setAllowShowSQL (true) will open the log output, see "jSqlBox configuration" for details. "A chapter. (Note: Since version 2.0.4, the SqlBoxContextConfig class is no longer used)
Note: SqlBoxContext instance in addition to the configuration method (ie methods such as setXxx and getXxx), all its other methods are thread-safe, so the entire project can only create a SqlBoxContext instance is enough, and usually This instance will also be configured as a global static instance, which can be statically introduced using static methods in JSQLBOX, greatly simplifying programming. However, if you want to create a lot of instances, it's okay. It's a lightweight object. Creating tens of thousands of instances in a second is not a problem, but it's usually not necessary.
