mybatis_user_guide(6) Java API
【1】SqlSessions 1)intro: 我們知道, SqlSessionFactoryBuilder 創建?SqlSessionFactory ,?SqlSessionFactory 創建?SqlSession,然后?SqlSession 獲得映射器,而映射器可以執行 sql 語句映射的方法,從而達到與數據庫交互的目的;
【1.1】SqlSessionFactoryBuilder 1)SqlSessionFactoryBuilder 有五個 build()方法,每一種都允許你從不同的資源中創建一個 SqlSession 實例。 SqlSessionFactory build(InputStream inputStream) SqlSessionFactory build(InputStream inputStream, String environment) SqlSessionFactory build(InputStream inputStream, Properties properties) SqlSessionFactory build(InputStream inputStream, String env, Properties props) SqlSessionFactory build(Configuration config) 對以上代碼的分析(Analysis): Environment 決定加載哪種環境,包括數據源和事務管理器。比如:
<environments default="development"><environment id="development"><transactionManager type="JDBC">...<dataSource type="POOLED">...</environment><environment id="production"><transactionManager type="MANAGED">...<dataSource type="JNDI">...</environment> </environments> 2)?如果一個屬性存在于這些位置,那么 MyBatis 將會按找下面的順序來加載它們:
- 在 properties 元素體中指定的屬性首先被讀取,
- 從 properties 元素的類路徑 resource 或 url 指定的屬性第二個被讀取, 可以覆蓋已經 指定的重復屬性,
- 作為方法參 數傳遞 的屬性最 后被讀 取,可以 覆蓋已 經從 properties 元 素體和 resource/url 屬性中加載的任意重復屬性。
? ? ? 因此,最高優先級的屬性是通過方法參數傳遞的,之后是 resource/url 屬性指定的,最 后是在 properties 元素體中指定的屬性。(干貨——屬性設置的優先級)
Conclusion) ?總結一下,前四個方法很大程度上是相同的,但是由于可以覆蓋,就允許你可選地指定 environment 和/或 properties。 這里給出一個從 mybatis-config.xml 文件創建 SqlSessionFactory 的示例:(干貨——從 mybatis-config.xml 文件創建 SqlSessionFactory 的示例)
String resource = "org/mybatis/builder/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); 3)以上代碼用到了Resources 工具類,這個類在 org.mybatis.io 包中。Resources 類正 如其名,會幫助你從類路徑下,文件系統或一個 web URL 加載資源文件。其方法列表如下: URL getResourceURL(String resource) URL getResourceURL(ClassLoader loader, String resource) InputStream getResourceAsStream(String resource) InputStream getResourceAsStream(ClassLoader loader, String resource) Properties getResourceAsProperties(String resource) Properties getResourceAsProperties(ClassLoader loader, String resource) Reader getResourceAsReader(String resource) Reader getResourceAsReader(ClassLoader loader, String resource) File getResourceAsFile(String resource) File getResourceAsFile(ClassLoader loader, String resource) InputStream getUrlAsStream(String urlString) Reader getUrlAsReader(String urlString) Properties getUrlAsProperties(String urlString) Class classForName(String className) 4)看個荔枝:?如何手動配置 configuration 實例,然后將它傳遞給 build()方法來創建 SqlSessionFactory DataSource dataSource = BaseDataTest.createBlogDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);Configuration configuration = new Configuration(environment); configuration.setLazyLoadingEnabled(true); configuration.setEnhancementEnabled(true); configuration.getTypeAliasRegistry().registerAlias(Blog.class); configuration.getTypeAliasRegistry().registerAlias(Post.class); configuration.getTypeAliasRegistry().registerAlias(Author.class); configuration.addMapper(BoundBlogMapper.class); configuration.addMapper(BoundAuthorMapper.class);SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(configuration);【1.2】SqlSessionFactory 1)intro:?SqlSessionFactory 有六個方法可以用來創建 SqlSession 實例。通常來說,如何決定是你 選擇下面這些方法時:
- Transaction (事務):你想為 session 使用事務或者使用自動提交(通常意味著很多 數據庫和/或 JDBC 驅動沒有事務)?
- Connection (連接):你想 MyBatis 獲得來自配置的數據源的連接還是提供你自己
- Execution (執行):你想 MyBatis 復用預處理語句和/或批量更新語句(包括插入和 刪除)?
2)?重載的 openSession()方法簽名設置允許你選擇這些可選中的任何一個組合。 SqlSession openSession() SqlSession openSession(boolean autoCommit) SqlSession openSession(Connection connection) SqlSession openSession(TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level) SqlSession openSession(ExecutorType execType) SqlSession openSession(ExecutorType execType, boolean autoCommit) SqlSession openSession(ExecutorType execType, Connection connection) Configuration getConfiguration(); 對以上代碼的分析(Analysis):
A1)默認的 openSession()方法沒有參數,它會創建有如下特性的 SqlSession:
- 會開啟一個事務(也就是不自動提交)
- 連接對象會從由活動環境配置的數據源實例中得到。
- 事務隔離級別將會使用驅動或數據源的默認設置。
- 預處理語句不會被復用,也不會批量處理更新。
- ExecutorType.SIMPLE: 這個執行器類型不做特殊的事情。它為每個語句的執行創建一個新的預處理語句。
- ExecutorType.REUSE: 這個執行器類型會復用預處理語句。
- ExecutorType.BATCH: 這個執行器會批量執行所有更新語句,如果 SELECT 在它們中間執行還會標定它們是 必須的,來保證一個簡單并易于理解的行為。
【1.3】SqlSession 1)intro:?在 SqlSession 類中有超過 20 個方法,所以將它們分開成易于理解的組合。
【1.3.1】語句執行方法 1)intro:?這些方法被用來執行定義在 SQL 映射的 XML 文件中的 SELECT,INSERT,UPDA E T 和 DELETE 語句。它們都會自行解釋,每一句都使用語句的 ID 屬性和參數對象,參數可以 是原生類型(自動裝箱或包裝類) ,JavaBean,POJO 或 Map。(干貨——sql 語句需要參數的case) <T> T selectOne(String statement, Object parameter) <E> List<E> selectList(String statement, Object parameter) <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey) int insert(String statement, Object parameter) int update(String statement, Object parameter) int delete(String statement, Object parameter) 對以上代碼的分析(Analysis):?selectOne 和 selectList 的不同僅僅是 selectOne 必須返回一個對象。 如果多余一個, 或者 沒有返回 (或返回了 null) 那么就會拋出異常。 , 如果你不知道需要多少對象, 使用 selectList。
2)如果你想檢查一個對象是否存在,那么最好返回統計數(0 或 1) 。因為并不是所有語句都需 要參數,這些方法都是有不同重載版本的,它們可以不需要參數對象。(干貨——sql 語句不需要參數的case) <T> T selectOne(String statement) <E> List<E> selectList(String statement) <K,V> Map<K,V> selectMap(String statement, String mapKey) int insert(String statement) int update(String statement) int delete(String statement) 3)最后,還有查詢方法的三個高級版本:它們允許你限制返回行數的范圍,或者提供自定 義結果控制邏輯,這通常用于大量的數據集合。 <E> List<E> selectList (String statement, Object parameter, RowBounds rowBounds) <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowbounds) void select (String statement, Object parameter, ResultHandler<T> handler) void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler<T> handler) 對以上代碼的分析(Analysis): A1)RowBounds 參數會告訴 MyBatis 略過指定數量的記錄,還有限制返回結果的數量。 RowBounds 類有一個構造方法來接收 offset 和 limit,否則是不可改變的。(干貨——引入 RowBounds 參數) int offset = 100; int limit = 25; RowBounds rowBounds = new RowBounds(offset, limit); A2)?ResultHandler 參數允許你按你喜歡的方式處理每一行。你可以將它添加到 List 中,創 建 Map, 或拋出每個結果而不是只保留總計。 Set 你可以使用 ResultHandler 做很多漂亮的事, 那就是 MyBatis 內部創建結果集列表。它的接口很簡單。 package org.apache.ibatis.session; public interface ResultHandler<T> {void handleResult(ResultContext<? extends T> context); }
【1.3.2】Batch update statement Flush Method
1)intro: There is method for flushing(executing) batch update statements that stored in a JDBC driver class at any timing. This method can be used when you use the?ExecutorType.BATCH?as?ExecutorType.
List<BatchResult> flushStatements()【1.3.3.】?事務控制方法
1)intro: 控制事務范圍有四個方法。 當然, 如果你已經選擇了自動提交或你正在使用外部事務管 理器,這就沒有任何效果了。然而,如果你正在使用 JDBC 事務管理員,由 Connection 實 例來控制,那么這四個方法就會派上用場:
void commit() void commit(boolean force) void rollback() void rollback(boolean force) Attention)?默認情況下 MyBatis 不會自動提交事務, 除非它偵測到有插入, 更新或刪除操作改變了 數據庫。如果你已經做出了一些改變而沒有使用這些方法,那么你可以傳遞 true 到 commit 和 rollback 方法來保證它會被提交(注意,你不能在自動提交模式下強制 session,或者使用 了外部事務管理器時) 。很多時候你不用調用 rollback(),因為如果你沒有調用 commit 時 MyBatis 會替你完成。然而,如果你需要更多對多提交和回滾都可能的 session 的細粒度控 制,你可以使用回滾選擇來使它成為可能。(干貨——?默認情況下 MyBatis 不會自動提交事務, 除非它偵測到有插入, 更新或刪除操作改變了 數據庫。)【1.3.4】清理 Session 級的緩存 void clearCache() 對以上代碼的分析(Analysis):?SqlSession 實例有一個本地緩存在執行 update,commit,rollback 和 close 時被清理。要 明確地關閉它(獲取打算做更多的工作) ,你可以調用 clearCache()。
【1.3.5】?確保 SqlSession 被關閉 1)intro: void close()
2)你必須保證的最重要的事情是: 你要關閉所打開的任何 session。保證做到這點的最佳方 式是下面的工作模式:(干貨——關閉打開的任何 Session)
SqlSession session = sqlSessionFactory.openSession(); try {// following 3 lines pseudocod for "doing some work"session.insert(...);session.update(...);session.delete(...);session.commit(); } finally {session.close(); }2.1)還有,如果你正在使用jdk 1.7以上的版本還有MyBatis 3.2以上的版本,你可以使用try-with-resources語句:(干貨——使用帶資源的 try 語句)
try (SqlSession session = sqlSessionFactory.openSession()) {// following 3 lines pseudocode for "doing some work"session.insert(...);session.update(...);session.delete(...);session.commit(); }Attention)就像 SqlSessionFactory,你可以通過調用 getConfiguration()方法獲得 SqlSession 使用的 Configuration 實例
Configuration getConfiguration()【1.4】使用映射器(推薦使用映射器和數據庫交互) <T> T getMapper(Class<T> type)
1)intro:上述的各個 insert,update,delete 和 select 方法都很強大,但也有些繁瑣,沒有類型安 全,對于你的 IDE 也沒有幫助,還有可能的單元測試。在上面的入門章節中我們已經看到 了一個使用映射器的示例。
2)因此, 一個更通用的方式來執行映射語句是使用映射器類。 一個映射器類就是一個簡單 的接口,其中的方法定義匹配于 SqlSession 方法。下面的示例展示了一些方法簽名和它們是 如何映射到 SqlSession 的。
public interface AuthorMapper {// (Author) selectOne("selectAuthor",5);Author selectAuthor(int id); // (List<Author>) selectList(“selectAuthors”)List<Author> selectAuthors();// (Map<Integer,Author>) selectMap("selectAuthors", "id")@MapKey("id")Map<Integer, Author> selectAuthors();// insert("insertAuthor", author)int insertAuthor(Author author);// updateAuthor("updateAuthor", author)int updateAuthor(Author author);// delete("deleteAuthor",5)int deleteAuthor(int id); }【1.4.1】映射器注解 1)intro:?注解提供了一種簡單的方式來實現簡單映射語句,而 不會引入大量的開銷;
【1.4.2】 映射聲明樣例 ?荔枝1)intro:?這個例子展示了如何使用 @SelectKey 注解來在插入前讀取數據庫序列的值: @Insert("insert into table3 (id, name) values(#{nameId}, #{name})") @SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class) int insertTable3(Name name); 荔枝2)這個例子展示了如何使用 @SelectKey 注解來在插入后讀取數據庫識別列的值: @Insert("insert into table2 (name) values(#{name})") @SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class) int insertTable2(Name name);
荔枝3)This example shows using the?@Flush?annotation to call the?SqlSession#flushStatements():
@Flush List<BatchResult> flush();荔枝4)These examples show how to name a ResultMap by specifying id attribute of @Results annotation.
@Results(id = "userResult", value = {@Result(property = "id", column = "uid", id = true),@Result(property = "firstName", column = "first_name"),@Result(property = "lastName", column = "last_name") }) @Select("select * from users where id = #{id}") User getUserById(Integer id);@Results(id = "companyResults") @ConstructorArgs({@Arg(property = "id", column = "cid", id = true),@Arg(property = "name", column = "name") }) @Select("select * from company where id = #{id}") Company getCompanyById(Integer id);荔枝5)This example shows solo parameter using the Sql Provider annotation:
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") List<User> getUsersByName(String name);class UserSqlBuilder {public String buildGetUsersByName(final String name) {return new SQL(){{SELECT("*");FROM("users");if (name != null) {WHERE("name like #{value} || '%'");}ORDER_BY("id");}}.toString();} }荔枝6)This example shows multiple parameters using the Sql Provider annotation:
@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") List<User> getUsersByName(@Param("name") String name, @Param("orderByColumn") String orderByColumn);class UserSqlBuilder {// If not use @Param, you should be define same arguments with mapper methodpublic String buildGetUsersByName(final String name, final String orderByColumn) {return new SQL(){{SELECT("*");FROM("users");WHERE("name like #{name} || '%'");ORDER_BY(orderByColumn);}}.toString();}// If use @Param, you can define only arguments to be usedpublic String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {return new SQL(){{SELECT("*");FROM("users");WHERE("name like #{name} || '%'");ORDER_BY(orderByColumn);}}.toString();} }總結
以上是生活随笔為你收集整理的mybatis_user_guide(6) Java API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魔方高级玩法 这样玩真的是高级还原了
- 下一篇: java美元兑换,(Java实现) 美元