1,实体类与数据库表映射关系
1>可以采用注解的方式;
2>可以使用配置文件配置映射关系;
2,配置文件
1>hibenate.properties;
2>hibernate.cfg.xml(其他命名方式);
3>不使用配置文件;
3,核心代码加载加载配置文件
1>直接加载配置文件;
2>使用addAnnotatedClass()方法;
3>使用addPackage()方法;
以上这些方式组合使用,有的组合可以正常,有的不可使用,下面仅提供可用例子,未提供的组合均未测试成功
实体类
注:hibernate.properties和无配置文件可以混合使用。
1>可以采用注解的方式;
2>可以使用配置文件配置映射关系;
2,配置文件
1>hibenate.properties;
2>hibernate.cfg.xml(其他命名方式);
3>不使用配置文件;
3,核心代码加载加载配置文件
1>直接加载配置文件;
2>使用addAnnotatedClass()方法;
3>使用addPackage()方法;
以上这些方式组合使用,有的组合可以正常,有的不可使用,下面仅提供可用例子,未提供的组合均未测试成功
例子测试:
1.注解+hibernate.cfg.xml+直接加载配置文件
实体类
package com.anlw.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
//主键的类型最好使用基本类型的包装类型
private Integer id;
private String name;
private Integer age;
public Student(){}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
配置文件
<?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="show_sql">true</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookshop</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.anlw.entity.Student"/>
</session-factory>
</hibernate-configuration>
核心测试代码
package com.anlw.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class Test {
public static void main(String[] args) {
//如果调用configure(),则默认加载根目录下的hibernate.cfg.cml,如果添加参数
//.configure("com/anlw/test/aa.xml);则是在对应的路径下寻找该名称的配置文件
Configuration conf = new Configuration().configure();
ServiceRegistry st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(st);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
Student s1 = new Student(1, "Name1", 19);
sess.save(s1);
tx.commit();
sess.close();
sf.close();
}
}
2.映射文件+hibernate.cfg.xml+直接加载配置文件实体类
package com.anlw.entity;
public class Student {
private Integer id;
private String name;
private String sex;
public Student() {
}
public Student(Integer id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = 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;
}
}
实体类与关系数据表的映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.anlw.entity.Student" table="student" catalog="bookshop">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="255" />
</property>
</class>
</hibernate-mapping>
配置文件
<?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="show_sql">true</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookshop</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="Student.xml"/><!-- 这里是注解与配置文件设置映射关系的区别处 -->
</session-factory>
</hibernate-configuration>
核心代码
package com.anlw.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class Test {
public static void main(String[] args) {
//如果调用configure(),则默认加载根目录下的hibernate.cfg.cml,如果添加参数
//.configure("com/anlw/test/aa.xml);则是在对应的路径下寻找该名称的配置文件
Configuration conf = new Configuration().configure();
ServiceRegistry st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(st);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
Student s1 = new Student(1, "Name2", "boy");
sess.save(s1);
tx.commit();
sess.close();
sf.close();
}
}
3.注解+无配置文件+addAnnotatedClass()
实体类
package com.anlw.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
//public Student(){}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
核心代码
package com.anlw.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Test {
public static void main(String[] args) {
Configuration conf = new Configuration().addAnnotatedClass(Student.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/bookshop")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "123456")
.setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry se = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(se);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
Student st = new Student(1, "anlw", 25);
sess.save(st);
tx.commit();
sess.close();
sf.close();
}
}
4.注解+hibenate.properties+addAnnotatedClass()
hibenate.properties必须放src根目录下
实体类
package com.anlw.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
//public Student(){}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
hibenate.properties
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url:jdbc:mysql://localhost:3306/bookshop
hibernate.connection.username root
hibernate.connection.password=123456
hibernate.hbm2ddl.auto create
hibernate.show_sql=true
hibernate.hibernate.format_sql true
注意:properties文件的连接符可以是空格,也可以是=,也可以是:。
核心代码
package com.anlw.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Test {
public static void main(String[] args) {
Configuration conf = new Configuration().addAnnotatedClass(Student.class);
ServiceRegistry se = new StandardServiceRegistryBuilder()
.applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(se);
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
Student st = new Student(1, "anlw", 22277);
sess.save(st);
tx.commit();
sess.close();
sf.close();
}
}
注:hibernate.properties和无配置文件可以混合使用。