一.Clob与Blob应用.

Student类添加两个属性, byte[] pic, string remark; mybatis_Student表添加两个字段pic longblob, remark longtext(MySQL的clob类型)

完成对student表的添加与查询.

1.StudentMapper类添加方法

  1. public void addStudent(Student student);  


2.StudentMapper.xml配置

  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.测试添加方法

  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. }  
  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.测试查询方法

使用以前的方法就可以了

  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里传入,也可以使用这种方式:

  1. public Student getStudent(String name, int age);  


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


因为有多个参数,所以不能指定parameterType了

posted on 2017-01-17 09:30  劳资的睡姿决定发型  阅读(2274)  评论(0编辑  收藏  举报