Springboot integrates mybatis to serialize objects and store them in the database (blob field) demo

For serialization and deserialization, please refer to: http://www.jiajiajia.club/blog/artical/yjw520/161

Source code download address: http://photo.jiajiajia.club/file/blob.rar

controller layer code:

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 code:

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
/**
* Serialize testPaper and store it in the blob field in the database
*/
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 layer code:

import blob.blob.entity.Stu;
public interface IndexMapper {
void insert(Stu s);
Stu get(int id);
}

mapper.xml configuration file

<?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>
\t
<select id="get" resultType="blob.blob.entity.Stu">
SELECT id,name,test_paper testPaper FROM stu WHERE id=#{id}
</select>
</mapper>

Related entity classes:

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

/**
 * Package return message
 * @author Silicon Valley Explorer (jia)
 */
public class AjaxResult {
private boolean success;
private String msg;
private Object data;
private int code;
private int count;
public AjaxResult(){
}
\t
public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
\t
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);
}
\t
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;
}
}