oracle中的
clob:clob
blob:blob
mysql中的
clob:longtext
blob:longblobjava
邏輯分頁:將數據所有取出先放到內存中,以後在內存中進行分頁,性能很差。不推薦使用mysql
物理分頁:經過語句進行分頁。sql
MyBatis默認狀況下:MyBatis默認使用一級緩存,即同一個SqlSession調用了相同的select語句,則直接回從緩存中返回結果,而不是再去查詢以便數據庫;數據庫
開發者能夠本身配置二級緩存,二級緩存是全局的;apache
默認狀況下:select使用二級緩存,insert,update,delete不使用二級緩存;緩存
代碼示例:session
mapper接口:mybatis
package com.maya.mappers; import java.util.List; import java.util.Map; import org.apache.ibatis.session.RowBounds; import com.maya.model.Student; public interface StudentMapper { public int insert(Student student);//添加blob,clob public Student findById(String id);//讀取BLOB,CLOB public List<Student> findKeys(String name,String sex);//多個參數處理方式 public List<Student> findByRow(RowBounds RBs);//MyBatis進行分頁,邏輯分頁,性能很差,不常使用 public List<Student> findByRow2(Map<String,Object> param);//MyBatis進行分頁,物理分頁 ,常使用 }
mapper.xml映射文件oracle
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.maya.mappers.StudentMapper"> <!-- 1,size:表示緩存cache中能容納的最大元素數。默認是1024; 2,flushInterval:定義緩存刷新週期,以毫秒計; 3,eviction:定義緩存的移除機制;默認是LRU(least recently userd,最近最少使用),還有FIFO(first in first out,先進先出) 4,readOnly:默認值是false,假如是true的話,緩存只能讀。 --> <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"/> <resultMap type="Student" id="StudentResult"> <id property="snumber" column="son" /> <result property="name" column="sname" /> <result property="sex" column="sex" /> </resultMap> <!-- 添加BLOB,CLOB --> <insert id="insert" parameterType="Student"> insert into student (son,sname,sex,pic,text) values (#{snumber},#{name},#{sex},#{pic},#{text}) </insert> <!-- 讀取BLOB,CLOB --> <select id="findById" parameterType="String" resultMap="StudentResult"> select * from student where son=#{id} </select> <!-- mybatis處理多個參數,類型就要用mybatis的參數,第一個就是param1,第二個就是param2 --> <select id="findKeys" resultMap="StudentResult"> select * from student where sname like #{param1} and sex=#{param2} </select> <!-- 邏輯分頁 --> <select id="findByRow" resultMap="StudentResult"> select * from student </select> <!-- 物理分頁,這是基於oracle的分頁過程,mysql的分頁過程很是簡單 select * from student limit start, size --> <select id="findByRow2" parameterType="Map" resultMap="StudentResult"> <choose> <when test="start!=null and end!=null"> select * from (select a.*, rownum ro from student a) where ro between #{start} and #{end} </when> <otherwise> select * from student </otherwise> </choose> </select> </mapper>
junit測試app
package com.maya.service; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.maya.mappers.StudentMapper; import com.maya.model.Student; import com.maya.util.MyBatisUtil; public class JunitTest2 { private static Logger logger=Logger.getLogger(JunitTest2.class); private SqlSession sqlSession=null; private StudentMapper studentMapper=null; @Before public void setUp() throws Exception { sqlSession=MyBatisUtil.openSession(); studentMapper=sqlSession.getMapper(StudentMapper.class); } @After public void tearDown() throws Exception { sqlSession.close(); } @Test //存儲CLOB,BLOB public void testInsertStudent(){ logger.info("添加學生---存儲CLOB,BLOB"); Student s=new Student(); s.setSnumber("111"); s.setName("正男"); s.setSex("男"); s.setText("很長的文本。。。。。。。。。。。。。。。。"); byte[] pic=null;// try { File f=new File("c://hehe.png");//填寫路徑 InputStream in=new FileInputStream(f);//讀改路徑 pic=new byte[in.available()];//available()長度 in.read(pic);//讀 in.close();//關閉 } catch (Exception e) { e.printStackTrace(); } s.setPic(pic); int i=studentMapper.insert(s); System.out.println(i); sqlSession.commit(); } @Test //讀取BLOB,CLOB public void testReadStudent(){ logger.info("讀取CLOB和BLOB。。。。"); Student s=studentMapper.findById("111"); System.out.println(s); byte[] pic=s.getPic(); try{ File f=new File("e://a2.png");//填寫路徑 OutputStream os=new FileOutputStream(f);//寫入該路徑 os.write(pic);//寫 os.close();//關閉 }catch(Exception e){ e.printStackTrace(); } } @Test //mybatis處理多個參數 public void textKeys(){ logger.info("mybatis處理多個參數"); List<Student> list=studentMapper.findKeys("%正%", "男"); System.out.println(list); } @Test //MyBatis分頁(邏輯分頁,性能很差,不常使用) public void textRow(){ logger.info("mybatis處理多個參數"); int offset=0,limit=3; RowBounds RBs=new RowBounds(offset,limit); List<Student> list=studentMapper.findByRow(RBs); System.out.println(list); } @Test //MyBatis分頁(物理分頁,性能很差,不常使用) public void textRow2(){ int pageStart=2; int pageSize=3; logger.info("mybatis處理多個參數"); Map<String, Object> param=new HashMap<String, Object>(); param.put("start", (pageStart-1)*pageSize+1); param.put("end", (pageStart-1)*pageSize+pageSize); List<Student> list=studentMapper.findByRow2(param); System.out.println(list); } }