今天是接触hibernate的第二天,用来练习的是hibernate最新的版本hibernate-release-4.3.5.Final
要使用hibernate,则先要引入它的jar包,要引入的jar包所在位置为解压后的目录下的\lib\required即可,还要引入mysql的驱动,具体见我的数据库的博客,mysql入门。
刚刚开始写HelloWorld就出了问题,找到问题的所在是hibernate的配置文件hibernate.cfg.xml配置不对,而且是头文件不对,出的错误名字是InvalidMappingException,出现问题的原因是对hibernate的官方文档不熟悉,从它上面copy的时候弄错了地方,所以才导致的这个问题,现在把对的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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/jll/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
下面的方式是传统的用配置文件的方式让hibernate工作,它的实体类和实体的配置文件要放在同一个包的下面,并且名字前面的部门要一致,而hibernate.cfg.xml则放在src目录下,这次实验用的实体类的名字叫做Student(参考马士兵老师的视频),它的配置文件叫做Student.hbm.xml,代码如下所示:
Student.java
package com.jll.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student.hbm.xml代码:
<?xml version="1.0"?>
<!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.jll.model">
<class name="Student" table="STUDENT">
<id name="id" type="java.lang.Integer" column="id"/>
<property name="name" type="java.lang.String" column="name" />
<property name="age" type="java.lang.Integer" column="age"/>
</class>
<!-- <query name="userNameIn"><![CDATA[from User where person.name in (:nameList) or userName in (:nameList)]]></query> -->
</hibernate-mapping>
从hibernater的配置文件可以看出用的数据库是mysql,数据库的名字叫做hibernate,如下是在这个数据库里面创建要用到的数据表
create table student
(
id int primary key,
name varchar(20),
age int
);
测试类如下:
package com.jll.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.jll.model.Teacher;
public class TestStudent {
public static void main(String[] args) {
Student t = new student();
t.setname("jll");
t.setId(1);
t.setAge("22");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
SessionFactory sf = configuration.buildSessionFactory(builder.build());
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
测试的时候因为hibernate的配置文件
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>(自动显示生成的sql语句)
的关系,会在后台打印出来如下语句,查看数据库的表格如下:
今天是接触hibernate的第二天,用来练习的是hibernate最新的版本hibernate-release-4.3.5.Final
刚刚开始写HelloWorld就出了问题,找到问题的所在是hibernate的配置文件hibernate.cfg.xml配置不对,而且是头文件不对,出的错误名字是InvalidMappingException,出现问题的原因是对hibernate的官方文档不熟悉,从它上面copy的时候弄错了地方,所以才导致的这个问题,现在把对的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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/jll/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
上面的方式是传统的用配置文件的方式让hibernate工作,它的实体类和实体的配置文件要放在同一个包的下面,并且名字前面的部门要一致,而hibernate.cfg.xml则放在src目录下,这次实验用的实体类的名字叫做Student(参考马士兵老师的视频),它的配置文件叫做Student.hbm.xml,代码如下所示:
Student.java
package com.jll.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student.hbm.xml代码:
<?xml version="1.0"?>
<!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.jll.model">
<class name="Student" table="STUDENT">
<id name="id" type="java.lang.Integer" column="id"/>
<property name="name" type="java.lang.String" column="name" />
<property name="age" type="java.lang.Integer" column="age"/>
</class>
<!-- <query name="userNameIn"><![CDATA[from User where person.name in (:nameList) or userName in (:nameList)]]></query> -->
</hibernate-mapping>
从hibernater的配置文件可以看出用的数据库是mysql,数据库的名字叫做hibernate,如下是在这个数据库里面创建要用到的数据表
create table student
(
id int primary key,
name varchar(20),
age int
);
测试类如下:
package com.jll.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.jll.model.Teacher;
public class TestStudent {
public static void main(String[] args) {
Student t = new student();
t.setname("jll");
t.setId(1);
t.setAge("22");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
SessionFactory sf = configuration.buildSessionFactory(builder.build());
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
测试的时候因为hibernate的配置文件
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>(自动显示生成的sql语句)
的关系,会在后台打印出来如下语句
Hibernate: insert into Student (name, age, id) values (?, ?, ?)
查看数据库的表格如下:
接下来介绍通过注解的方式让hibernate工作,在数据库里新创建一个数据表,名字叫做Teacher,表结构如下
再在com.jll.model里面新建一个实体类,在这个类里面用到了注解,代码如下:
package com.jll.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
在加入注解引入包时请注意,引入的是sun公司的javax.persistence.Entity和javax.persistence.Id,而不是引入的是hibernate的注解类,创建好这个类后,在hibernate的配置文件hibernate.cfg.xml里,<mapping source=”com/jll/model/Student.hbm.xml”>的下面加上后面这句话
<mapping class="com.jll.model.Teacher"/>
测试类如下:
package com.jll.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.jll.model.Teacher;
public class TestStudent {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setTitle("prefessor");
t.setId(1);
t.setName("jll");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
SessionFactory sf = configuration.buildSessionFactory(builder.build());
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
运行结果后台打印:
Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)
查看数据库是否加入进来数据:
测试结束!