六,MyBatis枚举,CLOB/BLOB数据类型处理,分页,缓存
来源:互联网 发布:儿童英语网络培训机构 编辑:程序博客网 时间:2024/06/01 07:52
1,枚举类型处理
<mapper namespace="com.mybatis.mappers.EmployeeMapper"> <resultMap type="Employee" id="EmployeeResult"> <id property="employeeId" column="employee_id"> <result property="name" column="employee_name"> <result property="password" column="employee_password"> <result property="email" column="employee_email"> <result property="gender" column="employee_gender"> </resultMap> <insert id="insertEmployee" parameterType="Employee" useGeneratedKeys="true" keyProperty="employeeId"> insert into employee(employee_name,employee_password,employee_email,employee_gender) values(#{name},#{password},#{email},#{gender}); </insert></mapper>
枚举类代码
package com.mybatis.enums;public enum Gender { MALE(0), FEMALE(1); private Integer gender; private Gender(Integer gender) { this.gender = gender; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; }}
在配置文件(不是映射配置文件)中加入:
<typeHandlers > <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mybatis.enums.Gender"/></typeHandlers>
java测试代码
@Test public void insertImployee() { SqlSession session = MyBatisUtil.openSession(); Employee employee = new Employee("Tom", "123456", "tom@email.com", Gender.MALE); EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); employeeMapper.insertEmployee(employee); session.commit(); session.close(); }
2,CLOB/BLOB数据类型处理(代码来自《Java Persistence with MyBatis3》)
创建USER_PICS表
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;
UserPic实体类
public class UserPic{ private int id; private String name; private byte[] pic; private String bio; //setters & getters}
UserPicMapper.xml文件
<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>
java代码
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(); }}
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(newFile("C:\\Images\\UserImage_FromDB.jpg"));os.write(pic);os.close();}catch (FileNotFoundException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}}
3,RowBounds分页
MyBatis 可以使用 RowBounds 逐页加载表数据。 RowBounds 对象可以使用 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);
4,缓存(摘录自《Java Persistence with MyBatis3》)
将从数据库中加载的数据缓存到内存中,是很多应用程序为了提高性能而采取的一贯做法。 MyBatis 对通过映射的SELECT 语句加载的查询结果提供了内建的缓存支持。默认情况下,启用一级缓存;即,如果你使用同一个 SqlSession接口对象调用了相同的 SELECT 语句,则直接会从缓存中返回结果,而不是再查询一次数据库。
我们可以在 SQL 映射器 XML 配置文件中使用元素添加全局二级缓存。
当你加入了<cache />元素,将会出现以下情况:
- 所有的在映射语句文件定义的<select>语句的查询结果都会被缓存
- 所有的在映射语句文件定义的<insert>,<update> 和<delete>语句将会刷新缓存
- 缓存根据最近最少被使用(Least Recently Used,LRU)算法管理
- 缓存不会被任何形式的基于时间表的刷新(没有刷新时间间隔) ,即不支持定时刷新机制
- 缓存将存储 1024 个 查询方法返回的列表或者对象的引用
- 缓存会被当作一个读/写缓存。 这是指检索出的对象不会被共享,并且可以被调用者安全地修改,不会其他潜在的调用者或者线程的潜在修改干扰。(即,缓存是线程安全的)
通过复写默认属性来自定义缓存的行为,如下所示:
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
以下是对上述属性的描述:
- eviction:此处定义缓存的移除机制。默认值是 LRU,其可能的值有:LRU(least recently used,最近最少使用),FIFO(first in first out,先进先出),SOFT(soft reference,软引用),WEAK(weakreference,弱引用) 。
- flushInterval:定义缓存刷新间隔,以毫秒计。默认情况下不设置。所以不使用刷新间隔,缓存 cache 只有调用语句的时候刷新。
- size:此表示缓存 cache 中能容纳的最大元素数。默认值是 1024,你可以设置成任意的正整数。
- readOnly:一个只读的缓存 cache 会对所有的调用者返回被缓存对象的同一个实例(实际返回的是被返回对象的一份引用)。一个读/写缓存 cache 将会返回被返回对象的一分拷贝(通过序列化) 。默认情况下设置为 false。 可能的值有 false 和 true。
一个缓存的配置和缓存实例被绑定到映射器配置文件所在的名空间(namespace)上,所以在相同名空间内的所有语
句被绑定到一个 cache 中。
默认的映射语句的 cache 配置如下:
<select ... flushCache="false" useCache="true"/><insert ... flushCache="true"/><update ... flushCache="true"/><delete ... flushCache="true"/>
可以为任意特定的映射语句复写默认的 cache 行为;例如,对一个 select 语句不使用缓存,可以设置 useCache=“false”。除了内建的缓存支持,MyBatis 也提供了与第三方缓存类库如 Ehcache,OSCache,Hazelcast 的集成支持。
- 六,MyBatis枚举,CLOB/BLOB数据类型处理,分页,缓存
- mybatis blob与clob处理
- 处理CLOB,BLOB,Date,Timestamp数据类型的方法
- 处理lob(blob, clob)
- hibernate clob/blob的处理
- MyBatis学习笔记(七)Clob与Blob数据类型与多参数传入
- oracle存储大数据类型(Clob/Blob)
- plsql 查询blob数据和clob数据类型
- mybatis插入与取出 blob clob字段
- java处理Blob、Clob大对象
- Java处理大对象Clob、Blob
- java处理Blob、Clob大对象
- Java处理大对象Clob、Blob
- 处理blob和clob的通用类
- Oracle的clob,blob字段处理
- 利用spring的jdbcTemplate处理blob、clob
- spirng 对clob blob数据的处理
- ibatis处理blob字段和clob字段
- lintcode-落单的数 II
- IOS 成员变量,全局变量,局部变量定义,static与extern的区别
- Android源码剖析之Framwork层后记篇
- RocketMQ与Kafka对比(18项差异)
- 【解决方案Genymotion】Eclipse模拟器启动报错INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
- 六,MyBatis枚举,CLOB/BLOB数据类型处理,分页,缓存
- Cocos2d-JS 贝塞尔曲线根据时间设定即时坐标位置 C++到JS 绑定实现(联网游戏客户端Bezier同步功能实现)
- JAVA gc垃圾回收机制
- python中调用 imread 报错: ImportError: cannot import name imread
- C++中虚函数和默认实参问题
- Xcode菜单及常用快捷键大全
- ReactNative调试
- 关于eclipse的android的环境配置问题
- 管理者需要具备的素质