hibernate第一次学习-----对数据库进行基本操控
程序员文章站
2022-06-12 19:30:11
...
开发环境:
jdk:1.8
mysql:5.7.22
hibernate:5.2.16 (基础包10个)
配置文件: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.url">jdbc:mysql://localhost/bing</property>
<!-- 数据库登陆名称 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库登陆密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- 方言: 为数据库提供相应的适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property><!-- 显示sql语句 -->
<property name="hibernate.format_sql">true</property><!-- 格式sql语句 -->
<!--
create:先删除,再创建
update:如果表不存在就创建,不一样就更新,一样就什么都不做。
create-drop:初始化时创建表,SessionFactory执行close()时删除表。
validate:验证表结构是否一致,如果不一致,就抛异常。
-->
<property name="hbm2ddl.auto">update</property>
<!-- 导入映射文件 -->
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建实体类:User
package entity;
/*
* 时间:2018.9.9
* 作用:实体类
*/
public class User {
//定义一个人的属性:姓名,性别,职业,爱好,年龄,序号
private String name;
private String sex;
private String profession;
private String hobby;
private int age;
private int id;
//封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//构造函数
public User(String name, String sex, String profession, String hobby, int age, int id) {
super();
this.name = name;
this.sex = sex;
this.profession = profession;
this.hobby = hobby;
this.age = age;
this.id = id;
}
public User() {
super();
}
}
实体映射文件:User.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??9??9?? ????9:37:11 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="entity.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" />
</property>
<property name="profession" type="java.lang.String">
<column name="PROFESSION" />
</property>
<property name="hobby" type="java.lang.String">
<column name="HOBBY" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
</class>
</hibernate-mapping>
测试类:test1(第一种对类持久化操作方法)
package control;
/*
* 2018.9.10
* 对实体完成持久化操作-----测试测试1
*
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import entity.User;
public class test1 {
public static void main(String[] args) {
//创建类
User u=new User();
u.setName("刘x");
u.setSex("女");
u.setAge(21);
u.setHobby("音乐、学习");
u.setProfession("翻译员");
//读取配置文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory sf=conf.buildSessionFactory();
//打开Session
Session session= sf.openSession();
//初始化事物
Transaction tx=null;
try{
//开始一个事物
tx=session.beginTransaction();
//持续化操作
session.save(u);
//提交事务
tx.commit();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//关闭session
session.close();
System.out.println("运行完了");
}
}
}
测试类2:test2(第二种对类持久化操作方法)
package control;
/*
* 2018.9.10
* 对实体完成持久化操作-----测试方法2
*
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import entity.User;
public class test2 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
SessionFactory sf = new MetadataSources( registry )
.buildMetadata().buildSessionFactory();
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
User u = new User();
u.setName("李x");
u.setSex("男");
u.setAge(21);
u.setHobby("游戏、音乐");
u.setProfession("程序员");
session.save(u);
transaction.commit();
session.close();
sf.close();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy( registry );
e.printStackTrace();
}
}
}