1 创建一个maven项目,在pom.xml文件中添加相关依赖
< dependencies>
< dependency>
< groupId> org.hibernate</ groupId>
< artifactId> hibernate-core</ artifactId>
< version> 3.6.10.Final</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.26</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.6</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.13.2</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> org.slf4j</ groupId>
< artifactId> slf4j-nop</ artifactId>
< version> 1.6.4</ version>
</ dependency>
< dependency>
< groupId> org.javassist</ groupId>
< artifactId> javassist</ artifactId>
< version> 3.21.0-GA</ version>
</ dependency>
</ dependencies>
2 创建与数据表,我的数据库是mytest,表名是student
3 创建与数据库表对应的实体类
package com. it. pojo ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import lombok. ToString ;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String sid;
private String name ;
private String class1;
}
4 配置实体类与数据库表的映射关系
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
< hibernate-mapping>
< class name = " com.it.pojo.Student" table = " student" >
< id name = " sid" column = " sid" >
< generator class = " uuid" > </ generator>
</ id>
< property name = " name" column = " name" />
< property name = " class1" column = " class" />
</ class>
</ hibernate-mapping>
5 配置hibernate核心配置文件
<! 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.cj.jdbc.Driver</ property>
< property name = " hibernate.connection.url" > jdbc:mysql:///mytest</ property>
< property name = " hibernate.connection.username" > root</ property>
< property name = " hibernate.connection.password" > 123456</ property>
< property name = " hibernate.show_sql" > true</ property>
< property name = " hibernate.format_sql" > true</ property>
< property name = " hibernate.hbm2ddl.auto" > update</ property>
< property name = " hibernate.dialect" > org.hibernate.dialect.MySQLDialect</ property>
< mapping resource = " mapping/Student.hbn.xml" />
</ session-factory>
</ hibernate-configuration>
6 工具类
package com. it. util ;
import org. hibernate. Session ;
import org. hibernate. SessionFactory ;
import org. hibernate. cfg. Configuration ;
public class HibernateUtil {
private static SessionFactory factory;
public static Session getFactory ( ) {
factory = new Configuration ( ) . configure ( ) . buildSessionFactory ( ) ;
Session session = factory. openSession ( ) ;
return session;
}
}
7创建数据操作类
package com. it. dao ;
import com. it. pojo. Student ;
import com. it. util. HibernateUtil ;
import org. hibernate. HibernateException ;
import org. hibernate. Session ;
import org. hibernate. Transaction ;
import java. util. Iterator ;
import java. util. List ;
public class StudentDao {
public void findAll ( ) {
Session session = HibernateUtil . getFactory ( ) ;
Transaction tx = null ;
try {
tx = session. beginTransaction ( ) ;
List list = session. createQuery ( "FROM Student " ) . list ( ) ;
for ( Iterator iterator = list. iterator ( ) ; iterator. hasNext ( ) ; ) {
Student tmp = ( Student ) iterator. next ( ) ;
System . out. println ( tmp. getName ( ) ) ;
}
tx. commit ( ) ;
} catch ( HibernateException e) {
if ( tx != null )
tx. rollback ( ) ;
e. printStackTrace ( ) ;
} finally {
session. close ( ) ;
}
}
public Integer save ( ) {
Transaction tx = null ;
Student student = new Student ( "" , "dadd" , "aaa" ) ;
Session session = HibernateUtil . getFactory ( ) ;
try {
tx = session. beginTransaction ( ) ;
Object save = session. save ( student) ;
tx. commit ( ) ;
System . out. println ( save. toString ( ) ) ;
System . out. println ( "插入数据成功..." ) ;
} catch ( HibernateException e) {
if ( tx!= null ) {
tx. rollback ( ) ;
}
} finally {
session. close ( ) ;
}
return 1 ;
}
public void delete ( ) {
Student student = new Student ( "" , "dadd" , "aaa" ) ;
Transaction tx = null ;
Session session = HibernateUtil . getFactory ( ) ;
try {
tx = session. beginTransaction ( ) ;
Query query = session. createQuery ( "delete from Student where name=:name" ) ;
query. setString ( "name" , student. getName ( ) ) ;
query. executeUpdate ( ) ;
tx. commit ( ) ;
System . out. println ( "删除成功..." ) ;
} catch ( HibernateException e) {
if ( tx!= null ) {
tx. rollback ( ) ;
System . out. println ( "删除失败" ) ;
}
} finally {
session. close ( ) ;
System . out. println ( "结束运行" ) ;
}
}
public static void main ( String [ ] args) throws Exception {
StudentDao studentDao = new StudentDao ( ) ;
studentDao. delete ( ) ;
}
}