hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 表示一个数据库会话工厂 -->
<session-factory>
<!-- 指定数据库连接参数 -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/newsdb?serverTimezone=UTC</property>
<property name="connection.username">hz</property>
<property name="connection.password">hz</property>
<!-- 指定JDBC连接池 -->
<property name="connection.pool_size">1</property>
<!-- 指定SQL方言,不同的DBMS,有所不同 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 打开Hibernate自动会话上下文管理 -->
<property name="current_session_context_class">thread</property>
<!-- 关闭二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.internal.DisabledCaching</property>
<!-- 指定将所有执行的SQL语句回显到stdout -->
<property name="show_sql">true</property>
<!-- 在启动时对表进行检查 -->
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- 指定映射文件,相对路径,若有多个,使用多个mapping元素指定 -->
<mapping resource="/com/hky/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
实体类
public class User {
private Integer id;
private String username;
private String password;
private String name;
private String gender;
private String birthday;
private String hometown;
private String hobby;
private String introduction;
private Integer casting;
private String registerTime;
private Integer entry;
//代码省略get set toString 方法 自行添加
}
定义映射文件
User.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hky.hibernate">
<!-- 建立类与数据库表的映射 -->
<class name="User" table="user">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="id" column="id">
<generator class="native"/>
</id>
<!--建立类中其他普通属性与表中字段的对应-->
<property name="username" column="username"/>
<property name="password" column="password"/>
<property name="name" column="name"/>
<property name="gender" column="gender"/>
<property name="birthday" column="birthday"/>
<property name="hometown" column="hometown"/>
<property name="hobby" column="hobby"/>
<property name="introduction" column="introduction"/>
<property name="casting" column="casting"/>
<property name="registerTime" column="register_time"/>
<property name="entry" column="entry"/>
</class>
</hibernate-mapping>
测试:
public class Test {
@org.junit.Test
public void demo1() {
//1.加载核心配置文件
Configuration configuration = new Configuration().configure();
//2.创建一个SessionFactory对象:类似于JDBC连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通过SessionFactory获取到Session对象:类似于JDBC中的Connection
Session session = sessionFactory.openSession();
//4.手动开启事务
Transaction transaction = session.beginTransaction();
//5.代码编写
User user = session.get(User.class, 3);
System.out.println(user);
//6.事务提交
transaction.commit();
//7.释放资源
session.close();
}
}
ieda 自动生成
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
User user = session.get(User.class, 3);
System.out.println(user);
} finally {
session.close();
}
}
}