日期:2014-05-16  浏览次数:20698 次

ssh+mysql大字段处理方法
大字段共分两种,一是clob,即长文本。通常用来存储超过一定数量的(如4000)的字符,一是blob,即二进制字节数据,通常就是用户提交的附件
这里假定ssh+mysql的开发环境已配置完成。通过设计一个用户管理程序来举例说明针对mysql的大字段处理方法
一、数据库字段定义

CREATE TABLE `users` (
  `enable` int(11) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `account` varchar(255) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `resume` text,
  `photo` blob,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8

上面的resume字段用来存储长文本,类型定义为text,photo字段用来存储附件,类型定义为blob

二、POJO配置
User.java:
import java.sql.Blob;
import java.sql.Clob;
@Entity
@Table(name="users")
public class User {
	private Integer id;
	private String account;
	private String password;
	private Integer enable;
	private String resume;    //定义为string,对应数据库中resume(text)字段
	private Blob photo;       //定义为blob类型,对应数据库中photo(blob)字段
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getEnable() {
		return enable;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public Blob getResume() {
		return resume;
	}
	public void setResume(Blob resume) {
		this.resume = resume;
	}
	public void setEnable(Integer enable) {
		this.enable = enable;
	}
}


三、配置struts的action
dao和service层没有什么特别的。也不需要特别处理,关键在struts 的action.
a 要配置附件接收属性,如:
private File file;
private String fileFileName;
private String fileContentType;
b 长文本字段由于在POJO里已配置为String,不需要特别处理
privage String resume

c 处理思路是通过hibernate将输入流转换为clob和blob之后再作持久化
UserAction.java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.sql.Blob;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;

import cn.ibeans.ssh.users.model.User;
import cn.ibeans.ssh.users.service.UserManager;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 3345502231566725471L;
	private static Logger logger = Logger.getLogger(UserAction.class);
	
	private User user;
	
	private UserManager userManager;
	//以下三项是struts处理文件上传的约定风格
	private File photo;
	private String photoFileName;
	private String photoContentType;



	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Autowired
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception{
		if(null==user) return this.INPUT;
		logger.debug("开始日志^");

		//以用户提交到服务器端的文件实例为实参构造一个输入流,支持对二进制数据的两种存储方式
		InputStream is = new FileInputStream(photo);

		//1、第一种方式,将二进制文件以blob的方式存储到数据库
		//将输入流转换为hibernate能识别的blob型数据对象
		user.setPhoto(Hibernate.createBlob(is));
		userManager.addUser(user);
		
		//2、第二种方式,将二进制文件以文件方式存储到服务器硬盘
		/*
		//取得附件在服务