看書上提到的,記下來,加深一下印象。
一、mybatis處理CLOB/BLOB列的類型處理,例如:
CREATE TABLE USER_PICS ( ID INT(11) NOT NULL AUTO_INCREMENT, NAME VARCHAR(50) DEFAULT NULL, PIC BLOB, BIO LONGTEXT, PRIMARY KEY (ID) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=LATIN1;
默認(rèn)情況下,mybatis會將CLOB類型的列映射到j(luò)ava.lang.String類型上,而把BLOB類型的列映射到byte[]類型上
public class UserPic{ private int id; private String name; private byte[] pic; private String bio; //setters & getters }
創(chuàng)建mapper文件代碼如下
<insert id="insertUserPic" parameterType="UserPic"> INSERT INTO USER_PICS(NAME, PIC,BIO) VALUES(#{name},#{pic},#{bio}) </insert> <select id="getUserPic" parameterType="int" resultType="UserPic"> SELECT * FROM USER_PICS WHERE ID=#{id} </select>
下列的insertUserPic()展示了如何將數(shù)據(jù)插入到 CLOB/BLOB 類型的列上:
public void insertUserPic(){ byte[] pic = null; try{ File file = new File("C:\\Images\\UserImg.jpg"); InputStream is = new FileInputStream(file); pic = new byte[is.available()]; is.read(pic); is.close(); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } String name = "UserName"; String bio = "put some lenghty bio here"; UserPic userPic = new UserPic(0, name, pic , bio); SqlSession sqlSession = MyBatisUtil.openSession(); try{ UserPicMapper mapper = sqlSession.getMapper(UserPicMapper.class); mapper.insertUserPic(userPic); sqlSession.commit(); } finally{ sqlSession.close(); } }
下面的 getUserPic()方法展示了怎樣將 CLOB 類型數(shù)據(jù)讀取到 String 類型,BLOB 類型數(shù)據(jù)讀取成 byte[]屬性:
public void getUserPic(){ UserPic userPic = null; SqlSession sqlSession = MyBatisUtil.openSession(); try{ UserPicMapper mapper = sqlSession.getMapper(UserPicMapper.class); userPic = mapper.getUserPic(1); }finally{ sqlSession.close(); } byte[] pic = userPic.getPic(); try{ OutputStream os = new FileOutputStream(new File("C:\\Images\\UserImage_FromDB.jpg")); os.write(pic); os.close(); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } }
二、使用RowBound來進(jìn)行分頁處理
mybatis可以使用RowBound來進(jìn)行分頁處理,RowBound有兩個(gè)參數(shù),offset和limit。offset標(biāo)識開始的位置,limit標(biāo)識要取的記錄的數(shù)目,例如:
<select id="findAllStudents" resultMap="StudentResult"> select * from Students </select>
然后,你可以加載如下加載第一頁數(shù)據(jù)(前 25 條) :
int offset =0 , limit =25; RowBounds rowBounds = new RowBounds(offset, limit); List<Student> = studentMapper.getStudents(rowBounds);
個(gè)人感覺這個(gè)對象可能比較適用于使用反向工程生成的代碼,進(jìn)行單表查詢的時(shí)候使用,與mybatis的分頁插件pageHelper比較像,不知道是不是,大牛有知道的幫忙解釋一下。
三、mybatis-3.2.2 并不支持使用 resultMap 配置將查詢的結(jié)果集映射成一個(gè)屬性為key,而另外屬性為 value 的 HashMap。sqlSession.selectMap()則可以返回 以給定列為 key,記錄對象為 value 的 map。我們不能將其配置成使用其中一個(gè)屬性作為 key,而另外的屬性作為 value。
四、緩存
mybatis對通過映射的select語句加載查詢結(jié)果提供了內(nèi)建的緩存支持。
默認(rèn)情況下,開啟一級緩存,即:如果你使用同一個(gè)sqlSession接口對象條用了同一個(gè)select語句,則直接從緩存中返回結(jié)構(gòu),不會再次查詢數(shù)據(jù)庫。
二級緩存,默認(rèn)是關(guān)閉的,你可以通過在mapper映射文件中加入下面這一行來實(shí)現(xiàn)。
一個(gè)緩存的配置和緩存實(shí)例被綁定到映射器配置文件所在的名空間 (namespace)上,所以在相同名空間內(nèi)的所有語
句被綁定到一個(gè) cache 中。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/> <!-- 一、eviction:定義緩存的移除機(jī)制,主要包括 1、LUR(Least Recently used最近最少使用) 2、FIFO(first in first out,先進(jìn)先出) 3、SOFT(software reference,軟引用(不清楚什么玩意)) 4、WEAK(weak reference,弱引用,不知道什么鬼) 二、flushInterval:緩存刷新間隔,以毫秒計(jì)。默認(rèn)情況下不設(shè)置。所以不使用刷新間隔,緩存 cache 只 有調(diào)用語句的時(shí)候刷新。 三、size:此表示緩存 cache 中能容納的最大元素?cái)?shù)。默認(rèn)值是 1024,你可以設(shè)置成任意的正整數(shù)。 四、readOnly:一個(gè)只讀的緩存 cache 會對所有的調(diào)用者返回被緩存對象的同一個(gè)實(shí)例(實(shí)際返回的是被返回對 象的一份引用)。一個(gè)讀/寫緩存 cache 將會返回被返回對象的一分拷貝(通過序列化) 。默認(rèn)情況下設(shè) 置為 false。可能的值有 false 和 true。 -->
mybatis-config.xml中配置的
<settings> <!-- 該配置影響的所有映射器中配置的緩存的全局開關(guān)。--> <setting name="cacheEnable" value="true"/> </settings>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

iBatis and MyBatis are two mainstream ORM (Object-Relational Mapping) frameworks. They have many similarities in design and use, but also have some subtle differences. This article will compare the similarities and differences between iBatis and MyBatis in detail, and illustrate their characteristics through specific code examples. 1. The history and background of iBatis and MyBatis iBatis is Apache Software Foundat

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values
