看书上提到的,记下来,加深一下印象。
一、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 INTO USER_PICS(NAME, PIC,BIO) VALUES(#{name},#{pic},#{bio})
下列的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标识要取的记录的数目,例如:
然后,你可以加载如下加载第一页数据(前 25 条) :
int offset =0 , limit =25; RowBounds rowBounds = new RowBounds(offset, limit); List= studentMapper.getStudents(rowBounds);
个人感觉这个对象可能比较适用于使用反向工程生成的代码,进行单表查询的时候使用,与mybatis的分页插件pageHelper比较像,不知道是不是,大牛有知道的帮忙解释一下。
采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压
三、mybatis-3.2.2 并不支持使用 resultMap 配置将查询的结果集映射成一个属性为key,而另外属性为 value 的 HashMap。sqlSession.selectMap()则可以返回 以给定列为 key,记录对象为 value 的 map。我们不能将其配置成使用其中一个属性作为 key,而另外的属性作为 value。
四、缓存
mybatis对通过映射的select语句加载查询结果提供了内建的缓存支持。
默认情况下,开启一级缓存,即:如果你使用同一个sqlSession接口对象条用了同一个select语句,则直接从缓存中返回结构,不会再次查询数据库。
二级缓存,默认是关闭的,你可以通过在mapper映射文件中加入下面这一行来实现。
一个缓存的配置和缓存实例被绑定到映射器配置文件所在的名空间 (namespace)上,所以在相同名空间内的所有语
句被绑定到一个 cache 中。
mybatis-config.xml中配置的









