hibernate的自定义主键代码

本文介绍了如何在Hibernate中实现自定义主键的代码细节,包括实体类的设计、util工具类的应用以及配置XML文件中连接数据库的相关设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实体类

package entity;

public class dl {

	private String id;
	private String name;
	public dl() {
		super();
		// TODO Auto-generated constructor stub
	}
	public dl(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

util

package action;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import entity.dl;

public class test {

	Configuration cf = null;
	SessionFactory sf = null;
	Session s = null;
	Transaction tt = null;

	@Before
	public void before() {
//加载hibernate的配置文件
		cf = new Configuration().configure();
//创建SessionFactory
		sf = cf.buildSessionFactory();
////创建Session
		s = sf.openSession();
//开启事务
		tt = s.beginTransaction();

	}
 	@After
	public void after() {
//提交事务
		tt.commit();
//关闭连接
         sf.close();
//关闭连接
		s.close();

	}
//增加的方法
@Test
	public void add() {
//实例化实体类
		dl d = new dl();
//给name赋值
		d.setName("zs");
//使用增加方法
		s.save(d);
	}

}

 

package action;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

public class gen implements IdentifierGenerator {

	@Override
	public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
		String i = "myzs";
//连接数据库
		Connection c = session.connection();
		try {
			PreparedStatement ps = c.prepareStatement(" select uuid() as ss ");
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				String s = rs.getString("ss");
				String code = i + s;
				return code;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}

实体类xml

<?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 2018-9-5 19:53:20 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="entity.dl" table="DL">
        <id name="id" type="java.lang.String">
            <column name="ID" />
            <generator class="action.gen" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
    </class>
</hibernate-mapping>

连接数据库xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.password">sasa</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zx</property>
		<property name="hibernate.connection.username">root</property>

     //打印sql语句的方法
		<property name="show_sql">true</property>
     //对字段的显示进行格式化
		<property name="format_sql">true</property>
     //引用实体类的xml
		<mapping resource="entity/dl.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值