MyBatis學習筆記(七)Clob與Blob數據類型與多參數傳入


一.Clob與Blob應用.

Student類添加兩個屬性, byte[] pic, string remark; mybatis_Student表添加兩個字段pic longblob, remark longtext(MySQL的clob類型)

完成對student表的添加與查詢.

1.StudentMapper類添加方法

[html] view plain copy
  1. public void addStudent(Student student);  


2.StudentMapper.xml配置

[html] view plain copy
  1. <select id="addStudent" parameterType="Student">  
  2.     insert into mybatis_Student (name, age, remark, pic,grade_id,address_id)   
  3.     values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})  
  4. </select>  

3.測試添加方法

[html] view plain copy
  1. @Test  
  2. public void testAddStudent(){  
  3.     logger.info("添加學生");  
  4.     StudentMapper mapper = session.getMapper(StudentMapper.class);  
  5.     Student stu = new Student();  
  6.     stu.setName("你是誰");  
  7.     stu.setAge(13);  
  8.     Address addr = new Address();  
  9.     addr.setId(1);  
  10.     stu.setAddress(addr);  
  11.     Grade grade = new Grade();  
  12.     grade.setId(1);  
  13.     stu.setGrade(grade);  
  14.     stu.setRemark("判明顯而易見大煞風景期日進口非城奪在城城在風景點赤膽忠心右吸在");  
  15.     try {  
  16.         stu.setPic(FileUtil.readFile("e:/1.jpg"));  
  17.         mapper.addStudent(stu);  
  18.     } catch (Exception e) {  
  19.         e.printStackTrace();  
  20.     }  
  21. }  
[html] view plain copy
  1. package com.skymr.mybatis.util;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.InputStream;  
  5.   
  6. public class FileUtil {  
  7.   
  8.     public static byte[] readFile(String filePath) throws Exception{  
  9.         InputStream is = null;  
  10.         try{  
  11.             is = new FileInputStream(filePath);  
  12.             byte[] ret = new byte[is.available()];  
  13.             is.read(ret);  
  14.             return ret;  
  15.         }  
  16.         finally{  
  17.             if(is != null){  
  18.                 try{  
  19.                     is.close();  
  20.                 }catch(Exception e){}  
  21.             }  
  22.         }  
  23.     }  
  24. }  


4.測試查詢方法

使用以前的方法就可以了

[html] view plain copy
  1. @Test  
  2. public void testGetStudent(){  
  3.     logger.info("獲取學生");  
  4.     StudentMapper mapper = session.getMapper(StudentMapper.class);  
  5.     Student stu = mapper.getStudent(4);  
  6.     logger.info(stu.toString());  
  7. }  


二.Mapper方法的多參數傳入.

前面學習的時候Mapper方法的參數要么是一個int類型的id,要么沒有,要么傳入一個Map,Map應用得多,也可以將多個參數放到Map里傳入,也可以使用這種方式:

[html] view plain copy
  1. public Student getStudent(String name, int age);  


[html] view plain copy
  1. <select id="getStudent1" resultMap="stuMapWithAddr">  
  2.     select * from mybatis_Student where name like #{param1} and age=#{param2}  
  3. </select>  


因為有多個參數,所以不能指定parameterType了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM