实体类
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>