本站发布的所有文件/源码/文档/软件等均提供免费下载。
但本站带宽较低、流量有限,为防止恶意下载、盗刷流量,所以只能登陆网站后才能下载!
若给您带来不便请见谅~
为了方便,您可以通过qq授权登陆,也可以通过钉钉授权登陆。也可以通过邮箱注册后登陆。
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;
}
}
猜你喜欢
框架
4286
springboot请求json数据不返回对象的指定字段在实体类的字段上加上注解importcom.fasterxml.jackson.annotation.JsonIgnore;例
java基础
2931
java网络(socket)中传输序列化对象Person类,注意(两个项目中的Person类字节码要相同)packagesocket;importjava.io.Serializable
java项目
1394
springboot+mybatis配置多数据源并利用aop实现自动切换(demo)
blog
java序列化和反序列化
java基础
3712
传递性。序列化是把对象转换成有序字节流,以便在网络上传输或者保存在本地文件中。序列化后的字节流保存了Java对象的状态以及相关的描述信息。序列化机制的核心作用就是对象状态的保存与重建。反序列化:客户端从
weblog
1120
ALTERTABLEdatabaseName.tableNameADDUNIQUE(columnName);databaseName:数据库名tableName:表明columnName:字段名
blog
Oracle中将Clob字段转换成字符串
数据库基础
5230
个值时,会报错。2.获取Clob对象,在Java中通过对流处理获取字段内容,该方式没有长度限制selectcontentfromNEWS//将字CLOB转成STRING类型publicStringClo
java虚拟机(jvm)
2376
分为三块区域:对象头(Header)、实例数据(InstanceData)和对齐填充(Padding)。1.MarkWord(标记字段):对象的MarkWord部分占4个字节,其内容是一系列的标记位,比
java 数据库
2609
的规则使用了springboot中的Cron表达式,如图2。图1图2来源数据库sql中,如果两个数据库的字段不一样,应该用数据库中的as关键字使来原数据库查询返回的列和目标数据库的字段相同。