springboot集成mybatis将对象序列化储存数据库(blob字段)demo
序列化和反序列化请参考: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;
}
}
fixed
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。