看书上提到的,记下来,加深一下印象。
一、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;
默认情况下,mybatis会将clob类型的列映射到java.lang.string类型上,而把blob类型的列映射到byte[]类型上
public class userpic{
private int id;
private string name;
private byte[] pic;
private string bio;
//setters & getters
}
创建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()展示了如何将数据插入到 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 类型数据读取到 string 类型,blob 类型数据读取成 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来进行分页处理
mybatis可以使用rowbound来进行分页处理,rowbound有两个参数,offset和limit。offset标识开始的位置,limit标识要取的记录的数目,例如:
<select id="findallstudents" resultmap="studentresult">
select * from students
</select>
然后,你可以加载如下加载第一页数据(前 25 条) :
int offset =0 , limit =25;
rowbounds rowbounds = new rowbounds(offset, limit);
list<student> = studentmapper.getstudents(rowbounds);
个人感觉这个对象可能比较适用于使用反向工程生成的代码,进行单表查询的时候使用,与mybatis的分页插件pagehelper比较像,不知道是不是,大牛有知道的帮忙解释一下。
三、mybatis-3.2.2 并不支持使用 resultmap 配置将查询的结果集映射成一个属性为key,而另外属性为 value 的 hashmap。sqlsession.selectmap()则可以返回 以给定列为 key,记录对象为 value 的 map。我们不能将其配置成使用其中一个属性作为 key,而另外的属性作为 value。
四、缓存
mybatis对通过映射的select语句加载查询结果提供了内建的缓存支持。
默认情况下,开启一级缓存,即:如果你使用同一个sqlsession接口对象条用了同一个select语句,则直接从缓存中返回结构,不会再次查询数据库。
二级缓存,默认是关闭的,你可以通过在mapper映射文件中加入下面这一行来实现。
一个缓存的配置和缓存实例被绑定到映射器配置文件所在的名空间 (namespace)上,所以在相同名空间内的所有语
句被绑定到一个 cache 中。
<cache eviction="fifo" flushinterval="60000" size="512" readonly="true"/>
<!--
一、eviction:定义缓存的移除机制,主要包括
1、lur(least recently used最近最少使用)
2、fifo(first in first out,先进先出)
3、soft(software reference,软引用(不清楚什么玩意))
4、weak(weak reference,弱引用,不知道什么鬼)
二、flushinterval:缓存刷新间隔,以毫秒计。默认情况下不设置。所以不使用刷新间隔,缓存 cache 只
有调用语句的时候刷新。
三、size:此表示缓存 cache 中能容纳的最大元素数。默认值是 1024,你可以设置成任意的正整数。
四、readonly:一个只读的缓存 cache 会对所有的调用者返回被缓存对象的同一个实例(实际返回的是被返回对
象的一份引用)。一个读/写缓存 cache 将会返回被返回对象的一分拷贝(通过序列化) 。默认情况下设
置为 false。可能的值有 false 和 true。
-->
mybatis-config.xml中配置的
<settings>
<!-- 该配置影响的所有映射器中配置的缓存的全局开关。-->
<setting name="cacheenable" value="true"/>
</settings>
