mybat-大文件的存取
Posted 小拽A
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybat-大文件的存取相关的知识,希望对你有一定的参考价值。
在mybatis中存储大文件可以直接存
取的时候取出来的是二进制
1.在实体类中添加大文本和图片
package com.java1234.model;
public class Student {
private Integer id;
private String name;
private Integer age;
private byte[] pic;
private String remark;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public byte[] getPic() {
return pic;
}
public void setPic(byte[] pic) {
this.pic = pic;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", remark=" + remark + "]";
}
}
2.在dao接口中添加查询方法
package com.java1234.mappers;
import java.util.List;
import java.util.Map;
import com.java1234.model.Student;
public interface StudentMapper {
public List<Student> searchStudents(Map<String,Object> map);
public List<Student> searchStudents2(Map<String,Object> map);
public List<Student> searchStudents3(Map<String,Object> map);
public List<Student> searchStudents4(Map<String,Object> map);
public List<Student> searchStudents5(Map<String,Object> map);
public List<Student> searchStudents6(String name,int age);
public int updateStudent(Student student);
public int insertStudent(Student student);
public Student getStudentById(Integer id);
}
3.在mapper中添加方法
<?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.java1234.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<insert id="insertStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age},#{pic},#{remark});
</insert>
<select id="getStudentById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
<select id="searchStudents6" resultMap="StudentResult">
select * from t_student where name like #{param1} and age=#{param2}
</select>
<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
select * from t_student
where gradeId=#{gradeId}
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</select>
<select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
select * from t_student
<choose>
<when test="searchBy==‘gradeId‘">
where gradeId=#{gradeId}
</when>
<when test="searchBy==‘name‘">
where name like #{name}
</when>
<otherwise>
where age=#{age}
</otherwise>
</choose>
</select>
<select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
select * from t_student
<where>
<if test="gradeId!=null">
gradeId=#{gradeId}
</if>
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</where>
</select>
<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
select * from t_student
<trim prefix="where" prefixOverrides="and|or">
<if test="gradeId!=null">
gradeId=#{gradeId}
</if>
<if test="name!=null">
and name like #{name}
</if>
<if test="age!=nulll">
and age=#{age}
</if>
</trim>
</select>
<select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
select * from t_student
<if test="gradeIds!=null">
<where>
gradeId in
<foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
#{gradeId}
</foreach>
</where>
</if>
</select>
<update id="updateStudent" parameterType="Student">
update t_student
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="age!=null">
age=#{age},
</if>
</set>
where id=#{id}
</update>
</mapper>
4.添加测试:
package com.java1234.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.java1234.mappers.StudentMapper;
import com.java1234.model.Student;
import com.java1234.util.SqlSessionFactoryUtil;
public class StudentTest2 {
private static Logger logger=Logger.getLogger(StudentTest2.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
/**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
/**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testInsertStudent(){
logger.info("添加学生");
Student student=new Student();
student.setName("张三4");
student.setAge(14);
student.setRemark("很长的本文...");
byte []pic=null;
try{
File file=new File("c://boy.jpg");
InputStream inputStream=new FileInputStream(file);
pic=new byte[inputStream.available()];
inputStream.read(pic);
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
student.setPic(pic);
studentMapper.insertStudent(student);
sqlSession.commit();
}
@Test
public void testGetStudentById(){
logger.info("通过ID查找学生");
Student student=studentMapper.getStudentById(4);
System.out.println(student);
byte []pic=student.getPic();
try{
File file=new File("d://boy2.jpg");
OutputStream outputStream=new FileOutputStream(file);
outputStream.write(pic);
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testSearchStudents6() {
logger.info("添加学生(带条件)");
List<Student> studentList=studentMapper.searchStudents6("%3%",12);
for(Student student:studentList){
System.out.println(student);
}
}
}
以上是关于mybat-大文件的存取的主要内容,如果未能解决你的问题,请参考以下文章