hibernate环境搭建(Hello world 配置文件版)
2016-01-26 15:10
344 查看
1.下载hibernate jar包:hibernate-release-4.3.5.Final,导入必要的jar包,路径为:hibernate-release-4.3.5.Final\lib\required。
包含的jar包有10个。
2.建立新的java项目。
3.学习自己建立User Library:
(a)项目右键——build path——configure build path——add library.
(b)选择User-library,在其中新建library,命名为hibernate。
(c)在library中加入hibernate所需要的jar包(路径为:hibernate-release-4.3.5.Final\lib\required),hello world就够了,其他的还要加。
4.引入数据库的jdbc驱动。我用的mysql:mysql-connector-java-5.1.7-bin.jar
(a)创建数据库:create database hibernate;
(b)切换数据库:use hibernate;
(c)创建Student表:create table Student(id int primary key, name varchar(20),age int);
5.建立hibernate的配置文件hibernate.cfg.xml,强烈建议在hibernate-release-4.3.5.Final\documentation\manual\en-US\html_single路径下的帮助文档中copy。
地点:1.1.4.
Hibernate configuration。 内容修改后:
[html] view
plain copy
<?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">XXX</property>
<property name="connection.password">XXXX</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/huxing/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
建立Student类:
[java] view
plain copy
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的映射文件:Student.hbm.xml
[html] view
plain copy
<?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.huxing.hibernate.model">
<class name="Student" table="student">
<id name="id" column="id">
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="int" column="age"/>
</class>
</hibernate-mapping>
最后测试:
[html] view
plain copy
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.huxing.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student a = new Student();
a.setId(123);
a.setAge(32);
a.setName("hello hibernate!");
Configuration cfg = new Configuration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(a);
session.getTransaction().commit();
session.close();
cf.close();
}
}
包含的jar包有10个。
2.建立新的java项目。
3.学习自己建立User Library:
(a)项目右键——build path——configure build path——add library.
(b)选择User-library,在其中新建library,命名为hibernate。
(c)在library中加入hibernate所需要的jar包(路径为:hibernate-release-4.3.5.Final\lib\required),hello world就够了,其他的还要加。
4.引入数据库的jdbc驱动。我用的mysql:mysql-connector-java-5.1.7-bin.jar
(a)创建数据库:create database hibernate;
(b)切换数据库:use hibernate;
(c)创建Student表:create table Student(id int primary key, name varchar(20),age int);
5.建立hibernate的配置文件hibernate.cfg.xml,强烈建议在hibernate-release-4.3.5.Final\documentation\manual\en-US\html_single路径下的帮助文档中copy。
地点:1.1.4.
Hibernate configuration。 内容修改后:
[html] view
plain copy
<?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">XXX</property>
<property name="connection.password">XXXX</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/huxing/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
建立Student类:
[java] view
plain copy
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的映射文件:Student.hbm.xml
[html] view
plain copy
<?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.huxing.hibernate.model">
<class name="Student" table="student">
<id name="id" column="id">
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="int" column="age"/>
</class>
</hibernate-mapping>
最后测试:
[html] view
plain copy
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.huxing.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
Student a = new Student();
a.setId(123);
a.setAge(32);
a.setName("hello hibernate!");
Configuration cfg = new Configuration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(a);
session.getTransaction().commit();
session.close();
cf.close();
}
}
相关文章推荐
- class 之 ___repr__
- mybatis拦截器的执行顺序
- lvs中dr模式配置脚本
- Java RMI 远程方法调用
- C语言 文件操作4--文件结构体FILE的理解以及缓冲区再讲
- android Xutils Http模块分析
- java邮件(带附件发送)
- javascript 1: 变量 内存 作用域简单描述
- JBOSS启动报错Failed to start service jboss.web.deployment.default-host.
- 面试题
- skb-6
- Linux上编译opencv程序
- XSS攻击简介
- oracle表空间不足扩容的方法
- 使用xib方式创建UITableViewCell,设置Label自动换行注意事项
- java面向对象学习笔记
- Node.js 学习(五)Node.js 事件循环
- UIStatusBarStyle PreferredStatusBarStyle does not work
- Linux 最常用的操作命令
- Linux 最常用的操作命令