public class Sql2o extends Object
An Sql2o instance represents a way of connecting to one specific database.
To create a new instance, one need to specify either jdbc-url, username and password for the database or a data source.
Internally the Sql2o instance uses a data source to create jdbc connections to the database. If url, username and password was specified in the constructor, a simple data source is created, which works as a simple wrapper around the jdbc driver.
Some jdbc implementations have quirks, therefore it may be necessary to use a constructor with the quirks parameter. When quirks are specified, Sql2o will use workarounds to avoid these quirks.
| 构造器和说明 |
|---|
Sql2o(DataSource dataSource)
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.
|
Sql2o(DataSource dataSource,
Quirks quirks)
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.
|
Sql2o(String jndiLookup) |
Sql2o(String url,
String user,
String pass)
Creates a new instance of the Sql2o class.
|
Sql2o(String url,
String user,
String pass,
Quirks quirks)
Created a new instance of the Sql2o class.
|
| 限定符和类型 | 方法和说明 |
|---|---|
Connection |
beginTransaction()
Begins a transaction with isolation level
Connection.TRANSACTION_READ_COMMITTED. |
Connection |
beginTransaction(ConnectionSource connectionSource)
Begins a transaction with isolation level
Connection.TRANSACTION_READ_COMMITTED. |
Connection |
beginTransaction(ConnectionSource connectionSource,
int isolationLevel)
Begins a transaction with the given isolation level.
|
Connection |
beginTransaction(int isolationLevel)
Begins a transaction with the given isolation level.
|
Query |
createQuery(String query)
已过时。
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name).executeAndFetch(Pojo.class);
}
|
Query |
createQuery(String query,
boolean returnGeneratedKeys)
已过时。
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
|
ConnectionSource |
getConnectionSource()
Gets the
ConnectionSource that Sql2o uses internally to acquire database connections. |
DataSource |
getDataSource()
已过时。
use
getConnectionSource() as more general connection provider |
Map<String,String> |
getDefaultColumnMappings()
Gets the default column mappings Map. column mappings added to this Map are always available when Sql2o attempts
to map between result sets and object instances.
|
Quirks |
getQuirks() |
boolean |
isDefaultCaseSensitive()
Gets value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property
names.
|
Connection |
open()
Opens a connection to the database
|
Connection |
open(ConnectionSource connectionSource)
Opens a connection to the database
|
void |
runInTransaction(StatementRunnable runnable)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
void |
runInTransaction(StatementRunnable runnable,
Object argument)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
void |
runInTransaction(StatementRunnable runnable,
Object argument,
int isolationLevel)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
<V> V |
runInTransaction(StatementRunnableWithResult<V> runnableWithResult) |
<V> V |
runInTransaction(StatementRunnableWithResult<V> runnableWithResult,
Object argument) |
<V> V |
runInTransaction(StatementRunnableWithResult<V> runnableWithResult,
Object argument,
int isolationLevel) |
void |
setConnectionSource(ConnectionSource connectionSource)
Sets the
ConnectionSource that Sql2o uses internally to acquire database connections. |
void |
setDefaultCaseSensitive(boolean defaultCaseSensitive)
Sets a value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property
names.
|
void |
setDefaultColumnMappings(Map<String,String> defaultColumnMappings)
Sets the default column mappings Map.
|
void |
withConnection(StatementRunnable runnable)
Invokes the run method on the
StatementRunnableWithResult instance. |
void |
withConnection(StatementRunnable runnable,
Object argument)
Invokes the run method on the
StatementRunnableWithResult instance. |
<V> V |
withConnection(StatementRunnableWithResult<V> runnable)
Invokes the run method on the
StatementRunnableWithResult instance. |
<V> V |
withConnection(StatementRunnableWithResult<V> runnable,
Object argument)
Invokes the run method on the
StatementRunnableWithResult instance. |
public Sql2o(String jndiLookup)
public Sql2o(String url, String user, String pass)
GenericDatasource,
and call the Sql2o(javax.sql.DataSource) constructor which takes a DataSource as parameter.url - JDBC database urluser - database usernamepass - database passwordpublic Sql2o(String url, String user, String pass, Quirks quirks)
GenericDatasource,
and call the Sql2o(javax.sql.DataSource) constructor which takes a DataSource as parameter.url - JDBC database urluser - database usernamepass - database passwordquirks - Quirks allows sql2o to work around known quirks and issues in different JDBC drivers.public Sql2o(DataSource dataSource)
dataSource - The DataSource Sql2o uses to acquire connections to the database.public Sql2o(DataSource dataSource, Quirks quirks)
dataSource - The DataSource Sql2o uses to acquire connections to the database.quirks - Quirks allows sql2o to work around known quirks and issues in different JDBC drivers.public Quirks getQuirks()
@Deprecated public DataSource getDataSource()
getConnectionSource() as more general connection providerpublic ConnectionSource getConnectionSource()
ConnectionSource that Sql2o uses internally to acquire database connections.public void setConnectionSource(ConnectionSource connectionSource)
ConnectionSource that Sql2o uses internally to acquire database connections.connectionSource - the ConnectionSource instance to usepublic Map<String,String> getDefaultColumnMappings()
Map instance, which Sql2o internally uses to map column names with property
names.public void setDefaultColumnMappings(Map<String,String> defaultColumnMappings)
defaultColumnMappings - A Map instance Sql2o uses internally to map between column names and
property names.public boolean isDefaultCaseSensitive()
public void setDefaultCaseSensitive(boolean defaultCaseSensitive)
defaultCaseSensitive - @Deprecated public Query createQuery(String query, boolean returnGeneratedKeys)
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
Queryquery - the sql query stringreturnGeneratedKeys - boolean value indicating if the database should return any generated keys.Query instance@Deprecated public Query createQuery(String query)
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name).executeAndFetch(Pojo.class);
}
Queryquery - the sql query stringQuery instancepublic Connection open(ConnectionSource connectionSource)
connectionSource - the ConnectionSource implementation substitution,
that will be used instead of one from Sql2o instance.Connection class.public Connection open()
Connection class.public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument)
StatementRunnableWithResult instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.V - runnable - argument - public <V> V withConnection(StatementRunnableWithResult<V> runnable)
StatementRunnableWithResult instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.V - runnable - public void withConnection(StatementRunnable runnable)
StatementRunnableWithResult instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.runnable - public void withConnection(StatementRunnable runnable, Object argument)
StatementRunnableWithResult instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.runnable - argument - public Connection beginTransaction(int isolationLevel)
Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback() method to close the transaction. Use proper try-catch logic.isolationLevel - the isolation level of the transactionConnection instance to use to run statements in the transaction.public Connection beginTransaction(ConnectionSource connectionSource, int isolationLevel)
Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback() method to close the transaction. Use proper try-catch logic.connectionSource - the ConnectionSource implementation substitution,
that will be used instead of one from Sql2o instance.isolationLevel - the isolation level of the transactionConnection instance to use to run statements in the transaction.public Connection beginTransaction()
Connection.TRANSACTION_READ_COMMITTED. Every statement executed on the return Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback() method to close the transaction. Use proper try-catch logic.Connection instance to use to run statements in the transaction.public Connection beginTransaction(ConnectionSource connectionSource)
Connection.TRANSACTION_READ_COMMITTED. Every statement executed on the return Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback() method to close the transaction. Use proper try-catch logic.connectionSource - the ConnectionSource implementation substitution,
that will be used instead of one from Sql2o instance.Connection instance to use to run statements in the transaction.public void runInTransaction(StatementRunnable runnable)
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. All statements
run on the Connection instance in the run method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run method,
the transaction will automatically be rolled back.
The isolation level of the transaction will be set to Connection.TRANSACTION_READ_COMMITTED
runnable - The StatementRunnable instance.public void runInTransaction(StatementRunnable runnable, Object argument)
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. All statements
run on the Connection instance in the run method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run method,
the transaction will automatically be rolled back.
The isolation level of the transaction will be set to Connection.TRANSACTION_READ_COMMITTED
runnable - The StatementRunnable instance.argument - An argument which will be forwarded to the run methodpublic void runInTransaction(StatementRunnable runnable, Object argument, int isolationLevel)
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. All statements
run on the Connection instance in the run method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run method,
the transaction will automatically be rolled back.runnable - The StatementRunnable instance.argument - An argument which will be forwarded to the run methodisolationLevel - The isolation level of the transactionpublic <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult)
public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument)
public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel)
Copyright © 2021. All rights reserved.