Hibernate对应MySQL的Text字段

Hibernate3.6.1,MySQL5InnoDBDialect

  1. 如果hbm文件中property元素的type属性是string,则当不指定column中的length属性时,ddl中默认为varchar(255)。由于varchar(x)中x是与字符集有关系的,最多也就是65535,当指定的length超过65535时,hibernate知道肯定是不能用varchar了,所以自动选择用longtext,在小于等于65535的范围内,hibernate如实生成varchar(length)形式的ddl,能不能执行就看MySQL的了;
  2. 如果hbm文件中property元素的type属性是text,则ddl中直接使用longtext;
  3. 可以使用column的sql-type属性来明确指定类型,这样就可以使用MySQL中的tinytext和text了,当然这就有可能破坏hbm的通用性了
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2011-3-4 22:32:34 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
	<class name="com.wsd.itil.hibernate.TBusiness" table="t_business" catalog="db_issue">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="identity" />
		</id>
		<property name="c1" type="string">
			<column name="c1" not-null="true" default="''" />
		</property>
		<property name="c2" type="string">
			<column name="c2" length="21327" not-null="true" default="''" />
		</property>
		<property name="c3" type="string">
			<column name="c3" length="65536" not-null="true" />
		</property>
		<property name="c4" type="text">
			<column name="c4" not-null="true" />
		</property>
		<property name="c5" type="text">
			<column name="c5" sql-type="text" not-null="true" />
		</property>
	</class>
</hibernate-mapping>

 

CREATE TABLE `t_business` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `c1` varchar(255) NOT NULL DEFAULT '',
 `c2` varchar(21327) NOT NULL DEFAULT '',
 `c3` longtext NOT NULL,
 `c4` longtext NOT NULL,
 `c5` text NOT NULL,
 PRIMARY KEY (`id`)
)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate是一个Java持久化框架,可以连接MySQL数据库。连接MySQL数据库需要以下步骤: 1. 在项目中添加MySQL驱动程序的依赖项。 2. 在Hibernate配置文件中配置MySQL数据库的连接信息,包括数据库URL、用户名、密码等。 3. 在Java代码中使用Hibernate API来连接MySQL数据库,执行SQL语句或者操作数据库。 例如,以下是Hibernate配置文件中连接MySQL数据库的示例: ``` <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration> ``` 其中,`hibernate.connection.driver_class`指定MySQL驱动程序的类名,`hibernate.connection.url`指定数据库的URL,`hibernate.connection.username`和`hibernate.connection.password`指定数据库的用户名和密码,`hibernate.dialect`指定Hibernate使用的MySQL方言,`hibernate.show_sql`指定是否在控制台输出SQL语句。 连接MySQL数据库后,可以使用Hibernate API来操作数据库,例如: ``` Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); // 执行SQL语句或者操作数据库 ... tx.commit(); session.close(); ``` 其中,`sessionFactory`是Hibernate的会话工厂,`session`是Hibernate的会话对象,`tx`是Hibernate的事务对象。在事务中执行SQL语句或者操作数据库,最后提交事务并关闭会话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值