springboot集成mybatis将对象序列化储存数据库(blob字段)demo

weblog 2789 1 2

序列化和反序列化请参考:http://www.jiajiajia.club/blog/artical/yjw520/161

源码下载地址:http://photo.jiajiajia.club/file/blob.rar

controller层代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import blob.blob.entity.AjaxResult;
import blob.blob.service.IndexService;
@RestController
public class IndexController {
	@Autowired
	private IndexService indexService;
	@RequestMapping("test")
	public AjaxResult insert() {
		indexService.insert();
		return AjaxResult.success("ok");
	}
	@RequestMapping("get")
	public AjaxResult get(Integer id) {
		return AjaxResult.success(indexService.get(id));
	}
}
service代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import blob.blob.entity.Stu;
import blob.blob.entity.Topic;
import blob.blob.mapper.IndexMapper;
@Service
public class IndexService {
	@Autowired
	private IndexMapper indexMapper;
	public void insert() {
		// TODO Auto-generated method stub
		/**
		 * 	将 testPaper 序列化储存在 数据库中 blob字段
		 */
		List<Topic> testPaper=new ArrayList<Topic>();
		testPaper.add(new Topic(1,"java"));
		testPaper.add(new Topic(2,"c"));
		testPaper.add(new Topic(3,"c++"));
		testPaper.add(new Topic(1,"php"));
		testPaper.add(new Topic(1,"javac"));
		testPaper.add(new Topic(1,"javah"));
		ObjectOutputStream oos = null;
		ByteArrayOutputStream bos=null;
		Stu s=null;
        try {
            oos = new ObjectOutputStream(bos=new ByteArrayOutputStream());
            oos.writeObject(testPaper);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        	s=new Stu(1,"jiajia",bos.toByteArray());
        	try {
				oos.close();
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
    	indexMapper.insert(s);
	}
	public Stu get(int id) {
		// TODO Auto-generated method stub
		Stu s=indexMapper.get(id);
		if(s!=null) {
			ObjectInputStream ois = null;
			ByteArrayInputStream bas=null;
	        try {
	            ois = new ObjectInputStream(bas=new ByteArrayInputStream((byte[])s.getTestPaper()));
	            @SuppressWarnings("unchecked")
				List<Topic> p = (List<Topic>) ois.readObject();
	            s.setTestPaperObj(p);
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	            	ois.close();
	            	bas.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
		}
		return s;
	}
}
dao层代码:
import blob.blob.entity.Stu;
public interface IndexMapper {
	void insert(Stu s);
	Stu get(int id);
}
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="blob.blob.mapper.IndexMapper" >
	<insert id="insert">
		INSERT INTO stu(name,test_paper) VALUES (#{name},#{testPaper})
	</insert>
	
	<select id="get" resultType="blob.blob.entity.Stu">
		SELECT id,name,test_paper testPaper FROM stu WHERE id=#{id}
	</select>
</mapper>
相关实体类:

1.stu

public class Stu {
	private Integer id;
	private String name;
	private Object testPaper;
	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 void setTestPaper(byte[] testPaper) {
		this.testPaper = testPaper;
	}
	public Stu(Integer id, String name, byte[] testPaper) {
		super();
		this.id = id;
		this.name = name;
		this.setTestPaper(testPaper);
	}
	public Stu() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Object getTestPaper() {
		return testPaper;
	}
	public void setTestPaperObj(Object obj) {
		this.testPaper = obj;
	}
}

2.Topic

import java.io.Serializable;

public class Topic implements Serializable{
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String topic;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTopic() {
		return topic;
	}
	public void setTopic(String topic) {
		this.topic = topic;
	}
	@Override
	public String toString() {
		return "Topic [id=" + id + ", topic=" + topic + "]";
	}
	public Topic(Integer id, String topic) {
		super();
		this.id = id;
		this.topic = topic;
	}
	public Topic() {
		super();
		// TODO Auto-generated constructor stub
	}
}

3.AjaxResult

/**
 *	封装返回消息
 * @author 硅谷探秘者(jia)
 */
public class AjaxResult {
	private boolean success;
	private String msg;
	private Object data;
	private int code;
	private int count;
	public AjaxResult(){
	}
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public AjaxResult setTotal(int count) {
		this.count = count;
		return this;
	}

	public AjaxResult(boolean success,int code, String msg, Object data){
		this.success = success;
		this.code=code;
		this.msg = msg;
		this.data = data;
	}
	public AjaxResult(boolean success,int code, String msg, Object data,int count){
		this.success = success;
		this.code=code;
		this.msg = msg;
		this.data = data;
		this.count=count;
	}
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static AjaxResult success(Object data){
		return new AjaxResult(true,200, null, data);
	}

	public static AjaxResult success(Object data, String msg){
		return new AjaxResult(true,200, msg, data);
	}

	public static AjaxResult success(String msg){
		return new AjaxResult(true,200, msg, null);
	}
	
	public static AjaxResult success(int code,String msg){
		return new AjaxResult(true,code, msg, null);
	}

    public static AjaxResult fail(String msg){
        return new AjaxResult(false,300, msg, null);
    }
    public static AjaxResult fail(String msg,int code){
        return new AjaxResult(false,code, msg, null);
    }
    
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
}

 

猜你喜欢
框架 3903 springboot请求json不返回的指定在实体类的上加上注解importcom.fasterxml.jackson.annotation.JsonIgnore;例
java基础 2708 java网络(socket)中传输Person类,注意(两个项目中的Person类节码要相同)packagesocket;importjava.io.Serializable
java项目 1394 springboot+mybatis配置多源并利用aop实现自动切换(demo)
java基础 3537 传递性。是把转换节流,以便在网络上传输或者保在本地文件中。后的节流保了Java的状态以及相关的描述信息。机制的核心作用就是状态的保与重建。反:客户端从
weblog 986 ALTERTABLEdatabaseName.tableNameADDUNIQUE(columnName);databaseName:名tableName:表明columnName:
数据库基础 4967 个值时,会报错。2.获取Clob,在Java中通过流处理获取内容,该方式没有长度限制selectcontentfromNEWS//CLOB转STRING类型publicStringClo
java虚拟机(jvm) 2191 分为三块区域:头(Header)、实例(InstanceData)和齐填充(Padding)。1.MarkWord(标记):的MarkWord部分占4个节,其内容是一系的标记位,比
java 数据库 2609 的规则使用了springboot中的Cron表达式,如图2。图1图2来源sql中,如果两个不一样,应该用中的as关键使来原查询返回的和目标相同。
目录