代码如下:
User.java
package cn.itcast.entity;
public class User {
/*Hibernate要求实体类有一个属性且值唯一*/
private int uid;
private String username;
private String password;
private String address;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/*使用hibernate的时候,不需要手动创建表,他会帮我们自动创建*/
}
HibernateDemo.java
package cn.itcast.hibernatetest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import cn.itcast.entity.User;
public class HibernateDemo {
@Test
public void testAdd() {
//第一步 加载hibernate核心配置文件
//到src下面找到名称是hibernate.cfg.xml
//在hibernate里面封装对象
Configuration cfg = new Configuration();
cfg.configure();
//第二步 创建SessionFactory对象
//读取hibernate核心配置文件内容,创建sessionFactory
//在过程中,根据映射关系,在配置数据库里面把表创建
SessionFactory sessionFactory = cfg.buildSessionFactory();
//第三步 使用SessionFactory创建session对象
//类似于连接
Session session = sessionFactory.openSession();
//第四步 开启事务
Transaction tx = session.beginTransaction();
//第五步 写具体逻辑 crud操作
//添加功能
User user = new User();
user.setUsername("小王");
user.setPassword("250");
user.setAddress("日本");
//调用session的方法实现添加
session.save(user);
//第六步 提交事务
tx.commit();
//第七步 关闭资源
session.close();
sessionFactory.close();
}
}
配置文件代码如下:
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置文件中首先引入XML约束 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 1配置类和表对应
class标签
name属性:实体类全路径
table属性:数据表名称
-->
<class name = "cn.itcast.entity.User" table = "t_user">
<!-- 2配置实体类ID和表ID对应
hibernate要求实体类有一个属性且值唯一
hibernate要求表有字段作为唯一值
-->
<!-- id标签
name属性:实体类里面id属性名称
column属性 :生成的表字段名称
-->
<id name = "uid" column = "uid">
<!-- 设置数据库表id增长策略
native:生成表id值就是主键自动增长
-->
<generator class = "native"></generator>
</id>
<!-- 配置其他属性和表字段对应
name属性:实体类属性名
column属性:生成表的字段名
-->
<property name = "username" column = "username"></property>
<property name = "password" column = "password"></property>
<property name = "address" column = "address"></property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置文件中首先引入XML约束 -->
<!DOCTYPE hibernate-conciguration 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.url">jdbc:mysql:///hibernate_day01</property>
<property name= "hibernate.connection.username">root</property>
<property name= "hibernate.connection.password">123456</property>
<!-- 第二部分:配置hibernate信息 可选的 -->
<!-- 输出低层sql语句 -->
<property name= "hibernate.show_sql">true</property>
<!-- 输出低层sql语句格式 -->
<property name= "hibernate.format_sql">true</property>
<!-- hibernate帮创建表,需要配置之后 -->
<property name= "hibernate.hbm2ddl.auto"></property>
<!-- 第三部分:把映射文件放到核心配置文件中 -->
<mapping resource="cn/itcast/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
文件目录结构如图:
运行结果如图:
