mybatis3-javaapi

sqlSessionFactoryBuilder->sqlSessionFactory->sqlSession<-rowbound<-resultHandler

 

myBatis uses a Java enumeration wrapper for transaction isolation levels, called TransactionIsolationLevel, but otherwise they work as expected and have the 5 levels supported by JDBC (NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE).sql

 

sqlSession是mybatis使用最有用的實例

Statement Execution Methods(select insert update delete method)數據庫

 

<T> T selectOne(String statement, Object parameter)緩存

<E> List<E> selectList(String statement, Object parameter)session

<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)mybatis

int insert(String statement, Object parameter)app

int update(String statement, Object parameter) int delete(String statement, Object parameter)ide

 

沒有參數 Object parameterui

<T> T selectOne(String statement)spa

<E> List<E> selectList(String statement).net

<K,V> Map<K,V> selectMap(String statement, String mapKey)

int insert(String statement) int update(String statement)

int delete(String statement)

 

操做大的數據集for large dataset

<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)

 

Batch update statement Flush Method

List<BatchResult> flushStatements()

 

Transaction control methods

void commit() void commit(boolean force)

void rollback() void rollback(boolean force)

 

 

LocalCache--second level cache

新的session建立的時候建立一個本地緩存,並綁定,使用這個seesion的相同參數查詢不會去數據庫中查,本地緩存將會清空遇到update ,commit,rollback,close

記得關閉sqlSession

 

try (SqlSession session = sqlSessionFactory.openSession()) { // following 3 lines pseudocode for "doing some work" session.insert(...); session.update(...); session.delete(...); session.commit(); }

 

 

使用selectProvider

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")

List<User> getUsersByName(String name);

class UserSqlBuilder { public static String buildGetUsersByName(final String name) { return new SQL(){{ SELECT("*"); FROM("users"); if (name != null) { WHERE("name like #{value} || '%'"); } ORDER_BY("id"); }}.toString(); } }

 

@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 method

public static 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 used public static String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) { return new SQL(){{ SELECT("*"); FROM("users"); WHERE("name like #{name} || '%'"); ORDER_BY(orderByColumn); }}.toString(); } }

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息