Home > Database > Mysql Tutorial > body text

Hibernate使用jdbc只能获取单字符问题

WBOY
Release: 2016-06-07 17:22:31
Original
1106 people have browsed it

在hibernate项目中提供jdbc接口时,查询数据库char字段的数据只能得到第一个字符的问题,解决方案代码如下:

在hibernate项目中提供jdbc接口时,,查询数据库char字段的数据只能得到第一个字符的问题,解决方案代码如下:

package com.newer.common;

import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.dialect.MySQLDialect;


/**
 * 此类用于解决数据库 char字段问题(mysql)

 * 因使用hibernate内置提供的sql语句查询接口会给字符串当char处理,
 * 所以编写此类覆盖内置的MMySQLDialect

 * 给MYSQL的char字段修饰为String
 * @author RSun
 * 2012-2-26下午03:53:55
 */
public class MMySQLDialect extends MySQLDialect{
public MMySQLDialect() {
super();
registerHibernateType(Types.DECIMAL, Hibernate.BIG_INTEGER.getName());
registerHibernateType(Types.CHAR, Hibernate.STRING.getName());
registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());
registerHibernateType(Types.LONGNVARCHAR, Hibernate.STRING.getName());
}
}


总结:
1. 当前是解决mysql的问题,对应其它数据库只需实现相应的接口即可。
2. 配置${dialect}需引用上面的类。

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!