一.Clob与Blob应用.
Student类添加两个属性, byte[] pic, string remark; mybatis_Student表添加两个字段pic longblob, remark longtext(mysql的clob类型)
完成对student表的添加与查询.
1.StudentMapper类添加方法
public void addStudent(Student student);
2.StudentMapper.xml配置
<select id="addStudent" parameterType="Student">
insert into mybatis_Student (name, age, remark, pic,grade_id,address_id)
values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})
</select>
3.测试添加方法
@Test
public void testAddStudent(){
logger.info("添加学生");
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setName("你是谁");
stu.setAge(13);
Address addr = new Address();
addr.setId(1);
stu.setAddress(addr);
Grade grade = new Grade();
grade.setId(1);
stu.setGrade(grade);
stu.setRemark("判明显而易见大煞风景期日进口非城夺在城城在风景点赤胆忠心右吸在");
try {
stu.setPic(FileUtil.readFile("e:/1.jpg"));
mapper.addStudent(stu);
} catch (Exception e) {
e.printStackTrace();
}
}
package com.skymr.mybatis.util;
import java.io.FileInputStream;
import java.io.InputStream;
public class FileUtil {
public static byte[] readFile(String filePath) throws Exception{
InputStream is = null;
try{
is = new FileInputStream(filePath);
byte[] ret = new byte[is.available()];
is.read(ret);
return ret;
}
finally{
if(is != null){
try{
is.close();
}catch(Exception e){}
}
}
}
}
4.测试查询方法
使用以前的方法就可以了
@Test
public void testGetStudent(){
logger.info("获取学生");
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = mapper.getStudent(4);
logger.info(stu.toString());
}
二.Mapper方法的多参数传入.
前面学习的时候Mapper方法的参数要么是一个int类型的id,要么没有,要么传入一个Map,Map应用得多,也可以将多个参数放到Map里传入,也可以使用这种方式:
public Student getStudent(String name, int age);
<select id="getStudent1" resultMap="stuMapWithAddr">
select * from mybatis_Student where name like #{param1} and age=#{param2}
</select>
因为有多个参数,所以不能指定parameterType了