`

herbanate类型转换

 
阅读更多

用了这个必须用hibernate材有效

 

List<ActivityShareAcl> aShareAcls=   baseDao.find("FROM ActivityShareAcl WHERE activityId = ?",activityId)

        List<ActivityShareAcl> aShareAcls = jdbcTemplate.query(sb.toString(), new Long[] { activityId },

                new BeanPropertyRowMapper<ActivityShareAcl>(ActivityShareAcl.class));

 

/** 

 * Copyright (C) 2010 QIHAN Technology Co.,Ltd

 * All Rights Reserved.

 * Development of this software QIHAN Technology Co.,Ltd.

 * Without the formal written consent of the Company, 

 * any other individuals, groups may not use, 

 * copy, modify, or distribute this software

 * @Title: TestType.java 

 * @Package crm.data.test.hibernate 

 * @Description: TODO(用一句话描述该文件做什么) 

 * @author Mac.Lam

 * @date 2010-8-11 下午12:09:32 

 * @version 1.0 

 */

 

package saas.framework.hibernate.type;

 

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Types;

import java.util.Properties;

 

import org.apache.commons.lang.StringUtils;

import org.hibernate.HibernateException;

import org.hibernate.usertype.ParameterizedType;

 

/**

 * @ClassName: TestType

 * @Description: TODO(这里用一句话描述这个类的作用)

 * @author Mac.Lam

 * @date 2010-8-11 下午12:09:32

 */

 

public class BooleanType extends org.hibernate.type.BooleanType implements

ParameterizedType {

 

private static final int BOOLEAN_TRUE = 1;

private static final int BOOLEAN_FALSE = 0;

 

public static final String BOOLEAN_TEXT_TRUE = "Y";

public static final String BOOLEAN_TEXT_FALSE = "N";

 

/**

* @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)

*/

 

private static final long serialVersionUID = 1L;

 

private String name;

 

private String columnName;

 

private int type = 0;

 

public static final int INT_TYPE = 1;

 

public int getType() {

return type;

}

 

public void setType(int type) {

 

this.type = type;

}

 

public void setName(String name) {

 

this.name = name;

}

 

@Override

public Class<Boolean> getReturnedClass() {

return Boolean.class;

}

 

@Override

public String getName() {

return name;

}

 

private Boolean checkValue(boolean data) {

if (data) {

return Boolean.TRUE;

} else {

return Boolean.FALSE;

}

}

 

@Override

public Object get(ResultSet rs, String columnNames) throws HibernateException,

SQLException {

String column=columnNames;

if(StringUtils.isNotBlank(columnName)){

column=columnName;

}

if (type == INT_TYPE) {

int boolText = rs.getInt(column);

if (rs.wasNull()) {

return Boolean.FALSE;

}

return checkValue(boolText == BOOLEAN_TRUE);

} else {

String boolText = rs.getString(column);

if (rs.wasNull()) {

return Boolean.FALSE;

}

return checkValue(BOOLEAN_TEXT_TRUE.equals(boolText));

}

}

 

@Override

public void set(PreparedStatement st, Object value, int index)

throws HibernateException, SQLException {

if (type == INT_TYPE) {

int boolText = Boolean.TRUE.equals(value) ? BOOLEAN_TRUE

: BOOLEAN_FALSE;

st.setInt(index, boolText);

} else {

String boolText = Boolean.TRUE.equals(value) ? BOOLEAN_TEXT_TRUE

: BOOLEAN_TEXT_FALSE;

st.setString(index, boolText);

}

}

 

@Override

public int sqlType() {

if (type == INT_TYPE) {

return Types.INTEGER;

} else {

return Types.VARCHAR;

}

}

 

public String toString(Object value) throws HibernateException {

return value + "";

}

 

public Object fromStringValue(String xml) throws HibernateException {

return null;

}

 

public void setParameterValues(Properties parameters) {

this.name = parameters.getProperty("attributeName");

this.columnName=parameters.getProperty("columnName");

this.type = Integer.parseInt(parameters.getProperty("type", "0"));

}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics