欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义主键生成策略

程序员文章站 2022-07-13 10:30:53
...

自定义主键生成策略

Worker实体类:

package com.liuyongqi.MavenHibernateDemo2.entity;

import java.io.Serializable;

/**
 * worker工人实体类
 * @author Administrator
 * @data   2018年8月1日
 * @time   下午4:17:34
 */
public class Worker implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1110648352223617488L;
	private String wid;
	private String wname;
	
	public Worker() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Worker(String wid, String wname) {
		super();
		this.wid = wid;
		this.wname = wname;
	}
	public String getWid() {
		return wid;
	}
	public void setWid(String wid) {
		this.wid = wid;
	}
	public String getWname() {
		return wname;
	}
	public void setWname(String wname) {
		this.wname = wname;
	}
	@Override
	public String toString() {
		return "Worker [wid=" + wid + ", wname=" + wname + "]";
	}
	
	
	
}

实体类映射文件Worker.hbm.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-8-1 16:22:52 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.liuyongqi.MavenHibernateDemo2.entity.Worker" table="t_worker">
        <id name="wid" type="java.lang.String">
            <column name="WID" />
            <!--uuid: 是由容器自动生成的一个32位的字符串,.hex代表的是十六进制)
   32位的字符串,无需赋值, -->
            <generator class="com.liuyongqi.MavenHibernateDemo2.id.CustomPrimary" />
        </id>
        <property name="wname" type="java.lang.String">
            <column name="WNAME" />
        </property>
    </class>
</hibernate-mapping>

dao方法:

package com.liuyongqi.MavenHibernateDemo2.dao;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.liuyongqi.MavenHibernateDemo2.entity.Student;
import com.liuyongqi.MavenHibernateDemo2.entity.Worker;
import com.liuyongqi.MavenHibernateDemo2.util.DBHelper;
import com.liuyongqi.MavenHibernateDemo2.util.SessionFactoryUtil;

/**
 * 工人实体类doa方法
 * @author Administrator
 * @data   2018年8月1日
 * @time   下午4:29:29
 */
public class WorkerDao {
	public String PrimaryStrategies() {
		String sql="{call pro_primaryStrategies(?)}";
		Connection con = DBHelper.getcon();
		CallableStatement cs;
		String str="";
		try {
			cs = con.prepareCall(sql);
			cs.registerOutParameter(1, Types.VARCHAR);
			cs.execute();
			str=cs.getString(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return str;
		
	}

}

 自定义主键生成策略CustomPrimary类:

package com.liuyongqi.MavenHibernateDemo2.id;

import java.io.Serializable;

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

import com.liuyongqi.MavenHibernateDemo2.dao.WorkerDao;

/**
 * 自定义主键生成策略
 * @author Administrator
 * @data   2018年8月3日
 * @time   下午2:38:04
 */
public class CustomPrimary implements IdentifierGenerator{

	public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
		// TODO Auto-generated method stub
		String str = new WorkerDao().PrimaryStrategies();
		System.out.println(str);
		return str;
	}

}

数据库:

create table t_worker
(
  wid varchar(32) primary key,
  wname varchar(50) not null
);
select * from t_worker;
delete from t_worker;

存储过程:

create procedure pro_primaryStrategies(out str varchar(100))
begin	
	set @date=(select date_format(SYSDATE(), '%Y%m%d%H%i%s'));
	set aaa@qq.com;
end

核心配置文件hibernate.cfg.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">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_a?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 显示sql语句 -->
        <property name="show_sql" >true</property>
        <!-- 格式化sql语句 -->
        <property name="format_sql" >true</property>
        
        <mapping resource="com/liuyongqi/MavenHibernateDemo2/entity/News.hbm.xml"/>
        <mapping resource="com/liuyongqi/MavenHibernateDemo2/entity/Student.hbm.xml"/>  
       <!--  <mapping resource="com/liuyongqi/MavenHibernateDemo2/entity/Student2.hbm.xml"/>  -->
        <mapping resource="com/liuyongqi/MavenHibernateDemo2/entity/Worker.hbm.xml"/>       
    </session-factory>
</hibernate-configuration>

运行前数据库的结果:

自定义主键生成策略

运行后数据库的结果:

自定义主键生成策略

控制台的结果:

自定义主键生成策略

 操作成功

如果想浏览我的下一篇新文章,请留言?