1.处理CLOB,BLOB数据
oracle中的
clob:clob
blob:blob
mysql中的
clob:longtext
blob:longblob
2.传入多个输入参数,mybatis自带的param属性(但是不经常用,我们用map就足够了)
3.MyBatis分页
逻辑分页:将数据全部取出先放到内存中,之后在内存中进行分页,性能不好。不推荐使用
物理分页:通过语句进行分页。
4.MyBatis缓存
MyBatis默认情况下:MyBatis默认使用一级缓存,即同一个SqlSession调用了相同的select语句,则直接回从缓存中返回结果,而不是再去查询以便数据库;
开发者可以自己配置二级缓存,二级缓存是全局的;
默认情况下:select使用二级缓存,insert,update,delete不使用二级缓存;
代码示例:
mapper接口:
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映射文件
<?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 (select * from student) a) where ro between #{start} and #{end} </when> <otherwise> select * from student </otherwise> </choose> </select> </mapper>
junit测试
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); } }
时间: 2024-10-13 22:22:28