hibernate开发所需要的环境,Myeclispe 的安装、mysql的安装 JDK tomcat hibernate所需的jar包下载前三个网上例子很多就不多说了,hibernate中用到的jar包比较多。hibernate 官网:hibernate官网
hibernat自己开发中整理后的jar包下载地址:自己整理的hibernatejar包
第一步:新建一个普通的Java项目把下载后的包导入到lib目录里添加到path里。
第二步:创建mysql的数据库和表名。
第三步:新建一个xml名称为hibernate.cfg.xml然后把hibernate的配置文件copy进去并且对一些配置文件进行修改。
第四步:编写数据库对应的modle类最好是表名和类名相同并且属性名称和数据库的字段和数据类型一一对应
第五步:创建Configuration的对象进行运行插入数据。
下面我们按照上面的步骤一步一步的实现:
第一步
(1) 新建工程Fiel--->new --->java
(2)创建包名称一个是modle的包和测试用的包包名分别为com.ygc.hibernate.modle com.ygc.hibernate.test
(3)将下载的jar包copy到项目里放到lib目录下面,如果没有lib目录自己新建一个然后添加到path里。
第二步
创建mysql的数据库和表名cmd ----->mysql -uroot -proot .....此处省略了,不会用命令的可以用可视化工具创建。
C:\Documents and Settings\Administrator>mysql -uroot -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
看到上面日志表示mysql界面成功了。
显示以前的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hibernate |
| mysql |
| performance_schema |
| test |
| xqwy |
+--------------------+
6 rows in set (0.03 sec)
然后创建一个数据库我的数据库中已经存在了hibernate我在下面创建一个hiberanates看好了加了个s;
mysql> create database hibernates;
Query OK, 1 row affected (0.00 sec)
使用hiberanates的表
mysql> use hibernates;
Database changed
数据库创建好了开始创建表;在这里我创建一个Person的表 字段有id name age;
mysql> create table Person(id int pr
e int);
Query OK, 0 rows affected (0.09 sec)
验证下数据库是否创建成功看到以下信息表示表创建成功了。
mysql> show tables;
+----------------------+
| Tables_in_hibernates |
+----------------------+
| person |
+----------------------+
1 row in set (0.00 sec)
第三步
将hibernate的配置文件copy到项目的src目录下面。
<?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/ygc/hibernate/modle/Students.hbm.xml"/>
<mapping class="com.ygc.hibernate.modle.Teacher"/>
</session-factory>
</hibernate-configuration>
第四步
@Entity
@Table(name="People")
public class People {
private int id;
private String name;
private int age;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
}
第五步:
@Entity
@Table(name="People")
public class People {
private int id;
private String name;
private int age;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
}
在测试包中新建一个main方法的测试类
public class PersonTest { public static void main(String[] args) { Person people = new Person(); people.setName("张三"); people.setAge(26); Configuration configuration = new AnnotationConfiguration().configure(); SessionFactory sFactory = configuration.buildSessionFactory(); Session session = sFactory.openSession(); session.beginTransaction(); session.save(people); session.getTransaction().commit(); sFactory.close(); session.close(); } }
最后点击运行main方法然后查询数据库看看是不是数据库多了一条数据。